-2

I have the below code which used to work until i recently updated the function to return true or false.But suddenly the object objReader stopped being accessible outside the function.I have declared in the beginning of the class as private static oledbdatareader = null; So that i can access it in any method in the current class.

 string strProvider = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + strCurWBPath + ";;Mode=ReadWrite" + @";Extended Properties=""Excel 8.0;HDR=Yes;""";

            using ( objConn = new OleDbConnection(strProvider))
             {
                 objConn.Open();                    
                using ( objCmd = new OleDbCommand(strQuery, objConn))
                 {
                     objCmd.CommandType = CommandType.Text;
                     objCmd.ExecuteNonQuery();
                     objReader = objCmd.ExecuteReader(CommandBehavior.SequentialAccess);

                    // No point reading/writing data if there are no rows.
                     if (objReader.HasRows)
                     {

                             if (!objReader.IsClosed)
                             {
                                 return true;
                             }
                             else
                                 return false;                        


                     }
                     else
                     {
                         MessageBox.Show("There are no Rows to process. ");
                     }

                }//end of using1

            }//end of using2

any suggestions ?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
krrishna
  • 2,050
  • 4
  • 47
  • 101

1 Answers1

1

Correct me if I'm wrong (and confused on what OP said -not accessible outside the function in C#.net code) but here you cannot use OleDbDataReader object outside the method/function because its associate connection is disposed (You've using block).

If you wish that the result from database should be used in another method(s) then I suggest you to populate DataTable using OleDbDataAdapter.Fill method.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186