Solved

preventing a method call

Posted on 2006-06-16
6
215 Views
Last Modified: 2010-08-05










Hi experts,
I can’t get my head round this.

I want to stop this code from calling this method
this.displayBenefitGrid(sqlDataReader); if any error is found

thanks


private void btnpayrollLink_Click(object sender, System.EventArgs e)
            {
                  
                  
                  OleDbConnection payrollDb;
                  SqlConnection adcDb;
                  bool errorsFound = false;
                  bool recordInserted= false;
                  payrollDb = null;
                  adcDb = null;
                  try
                  {

                        //payrolldb uses OLedb
                       payrollDb =        new OleDbConnection(ConfigurationSettings.AppSettings[this.ddEmployers.SelectedValue]);
                        //adcDb uses connection
                        adcDb = new SqlConnection(ConfigurationSettings.AppSettings["connectLocal"]);
                  
                  }
                  catch(Exception ex)
                  {
                        this.lblErrorDisplay.Text = ex.Message;
                  }
                  System.Collections.Hashtable taxPaidList = new Hashtable();                  
                  System.Data.SqlClient.SqlDataReader sqlDataReader;
                  
                  try
                  {
                        if (adcDb == null) this.lblErrorDisplay.Text = "Cannot access the ADC database.  Contact the system administrator";
                        else
                        {
                              if (payrollDb == null) this.lblErrorDisplay.Text = "The payroll file was not found";
                                    
                              else
                              {                                                      
                                    errorsFound = insertCompensationItems(adcDb,payrollDb);      
                                    errorsFound = insertTaxPaid(adcDb,payrollDb);
                                    
                              //      this.DataGrid1.Visible=false;
                                 /////////////////////////////////////////////insert flag here so that if nothing is inserted then no need
                                 ///to show grid.  
                                 ///
                                    sqlDataReader = this.getBenefitInfo();
                                    if ( sqlDataReader != null && sqlDataReader.HasRows)
                                          this.displayBenefitGrid(sqlDataReader);
                              
                              }
                        } //end else adcDb
                  }
                  catch(SqlException sex)
                  {
                        
                        this.lblErrorDisplay.Text = "Database not found.  Please contact the system administrator";
                  }

                  }
#      endregion

0
Comment
Question by:SirReadAlot
  • 4
  • 2
6 Comments
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 16921520
Hi, try modifying your if condition to:

if ( sqlDataReader != null && !sqlDataReader.HasRows)

You were displaying the grid if the reader had rows.
0
 

Author Comment

by:SirReadAlot
ID: 16921534
will try this what about this

private void btnpayrollLink_Click(object sender, System.EventArgs e)
            {
                  
                  
                  OleDbConnection payrollDb;
                  SqlConnection adcDb;
                  bool InsertRecord = false;
                  bool errorsFound =false;
                  payrollDb = null;
                  adcDb = null;
                  try
                  {

                        //payrolldb uses OLedb
                       payrollDb =        new OleDbConnection(ConfigurationSettings.AppSettings[this.ddEmployers.SelectedValue]);
                        //adcDb uses connection
                        adcDb = new SqlConnection(ConfigurationSettings.AppSettings["connectLocal"]);
                  
                  }
                  catch(Exception ex)
                  {
                        this.lblErrorDisplay.Text = ex.Message;
                  }
                  System.Collections.Hashtable taxPaidList = new Hashtable();                  
                  System.Data.SqlClient.SqlDataReader sqlDataReader;
                  
                  try
                  {
                        if (adcDb == null) this.lblErrorDisplay.Text = "Cannot access the ADC database.  Contact the system administrator";
                        else
                        {
                              if (payrollDb == null) this.lblErrorDisplay.Text = "The payroll file was not found";
                                    
                              else
                              {                                                      
                                    errorsFound = insertCompensationItems(adcDb,payrollDb);      
                                    errorsFound = insertTaxPaid(adcDb,payrollDb);
                                    

                                    if (InsertRecord)
                                    {
                              
                                          sqlDataReader = this.getBenefitInfo();
                                          if ( sqlDataReader != null && sqlDataReader.HasRows)
                                          this.displayBenefitGrid(sqlDataReader);
                                    }
                                    else
                                    {
                                          this.displayBenefitGrid(null);
                                    }
                              }
                        } //end else adcDb
                  }
                  catch(SqlException sex)
                  {
                        
                        this.lblErrorDisplay.Text = "Database not found.  Please contact the system administrator";
                  }

                  }
#      endregion
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 16921538
sorry ignore my last post, didnt read question properly..
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 18

Expert Comment

by:Ravi Singh
ID: 16921567
Hi, that'll work provided that you've checked for a null input in the displayBenefitGrid() method, i.e. if the parameter is null dont display anything otherwise display.
0
 

Author Comment

by:SirReadAlot
ID: 16921593
hi,
i am try to stop the displayBenefitGrid from being called if an error occurs

""Hi, that'll work provided that you've checked for a null input in the displayBenefitGrid() method, i.e. if the parameter is null dont display anything otherwise display.""

private void displayBenefitGrid(SqlDataReader dataReader)
            {
                  try
                  {
                        if (dataReader.HasRows)
                        {
                              
                              this.lblInfo.Text = "The following items of compensation were added to the database.  <a target='_blank' href='exceptionpage.aspx?Employer=" + employerId +"'>Click here to view the error log</a>";
                              this.DataGrid1.DataSource = dataReader;
                              this.DataGrid1.DataBind();                              
                              this.btnExcelExport.Visible = true;
                        } else
                              this.lblInfo.Text = "Unable to add compensation items or no compensation items found";
                        
                  }
                  catch
                  {
                  }

            }
#endregion
0
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 500 total points
ID: 16921703
When you say error, are you refering to the boolean variable "errorsFound"? Should you be assigning to it twice here:

 errorsFound = insertCompensationItems(adcDb,payrollDb);    
 errorsFound = insertTaxPaid(adcDb,payrollDb);

?

Also, your testing if the "InsertRecord" variable is true, but never really assigning a value to InsertRecord (only when declaring it), should InsertRecord be used in one of the above two assignments?

If InsertRecord is not true, your sending a null into the method displayBenefitGrid() but not really handling it (apart from the exception handling), If you dont want to call the displayBenefitGrid() method if the data reader is null then rather than calling it with null, just have:

if (InsertRecord)
{
        sqlDataReader = this.getBenefitInfo();
        if ( sqlDataReader != null && sqlDataReader.HasRows)
              this.displayBenefitGrid(sqlDataReader);
}

Finally, if  by "error" you mean the "errorsFound" variable, then try the following instead:

if (InsertRecord)
{
        sqlDataReader = this.getBenefitInfo();
        if ( sqlDataReader != null && sqlDataReader.HasRows && !errorsFound)
              this.displayBenefitGrid(sqlDataReader);
}

The above will only call the displayBenefitGrid() method if the data reader is not null, the data reader has rows and there are no errors found.

Hope this helps.








0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question