Search code examples
c#oledbdatareader

Use of unassigned local variable c# using OleDbDataReader


I am having a problem and have been banging my head aginst a wall... I keep getting a "Use of unassigned local variable" when I call "dbReader = dbCommand.ExecuteReader();" it says the Use of unassigned local variable 'dbCommand'. Would someone please take a look at this and tell me where and what I am doing wrong? Thank you in advanced.

    public void computerList()
    {
        //Create SQL strings
        string sql = "SELECT Computers FROM [Sheet1$]";

        //Create the instances
        OleDbConnection dbConnection;
        OleDbDataAdapter dbAdapter;
        OleDbCommand dbCommand;
        OleDbDataReader dbReader; 
        DataTable dataTable;

        //Call the instance
        dbConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.4.0;Data Source=Y:\\\\serverName\\Share\\document.xls;Extended Properties='Excel 12.0 Xml;HDR=YES'");
        dbAdapter = new OleDbDataAdapter(sql, dbConnection);
        dataTable = new DataTable();
        dbConnection.Open();
        dbReader = dbCommand.ExecuteReader();

        while (dbReader.Read())
        {
            int iRow = dataTable.Rows.Count;
            //MessageBox.Show("Count " + iRow.ToString());
            //MessageBox.Show(dbReader.ToString());
            for (int i = 0; i < iRow; i++)
            {
                int loopID = i;
                string rowData = dataTable.TableName;
                MessageBox.Show("Count" + loopID);
                MessageBox.Show(dbReader.GetString(iRow));
            }

        }
        //Close Connections 
        dbReader.Close();
        dbConnection.Close();
    }

Solution

  • You are missing

    OleDbCommand oCommand = new OleDbCommand (sql , dbConnection)
    

    you are initializing dbAdapter = new OleDbDataAdapter(sql, dbConnection);

    instead..

    also You should use the using Statements for the Dispose and close of the connections and readers instead of

    dbReader.Close();
    dbConnection.Close();
    

    I would rewrite it like this...

    public void computerList()
        {
            //Create SQL strings
            string sql = "SELECT Computers FROM [Sheet1$]";       
    
            using (OleDbConnection dbConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.4.0;Data Source=Y:\\\\serverName\\Share\\document.xls;Extended Properties='Excel 12.0 Xml;HDR=YES'"))
            {
               //dbAdapter = new OleDbDataAdapter(sql, dbConnection); //You dont need it
               //dataTable = new DataTable(); //don't need it
               dbConnection.Open();
               using(OleDbCommand oCommand = new OleDbCommand (sql , dbConnection))
               {
                  using(OleDbDataReader dbReader = dbCommand.ExecuteReader())
                  {
                       while (dbReader.Read())
                       {
                          //int iRow = dataTable.Rows.Count; //always zero you never used the datable
                          //MessageBox.Show("Count " + iRow.ToString());
                          //MessageBox.Show(dbReader.ToString());
                          for (int i = 0; i < dbReader.FieldCount; i++)
                          {
                              //int loopID = i; //dont need it
                              //string rowData = dataTable.TableName; //Dont need it
                              MessageBox.Show("Count" + i);
                              MessageBox.Show(dbReader.GetString(i));
                           }
    
                        }
                   } //reader closed and disposed          
               }//command disposed
            } //connection closed and disposed
      }