Solved

preventing a method call

Posted on 2006-06-16
6
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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