Link to home
Start Free TrialLog in
Avatar of SirReadAlot
SirReadAlot

asked on

preventing a method call











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

Avatar of Ravi Singh
Ravi Singh
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi, try modifying your if condition to:

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

You were displaying the grid if the reader had rows.
Avatar of SirReadAlot
SirReadAlot

ASKER

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
sorry ignore my last post, didnt read question properly..
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.
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
ASKER CERTIFIED SOLUTION
Avatar of Ravi Singh
Ravi Singh
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial