Solved

preventing a method call

Posted on 2006-06-16
6
209 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
Comment Utility
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
Comment Utility
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
Comment Utility
sorry ignore my last post, didnt read question properly..
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 18

Expert Comment

by:Ravi Singh
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now