Solved

preventing a method call

Posted on 2006-06-16
6
216 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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…

809 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