Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8118567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:31:42+00:00 2026-06-06T04:31:42+00:00

I am having a problem and have been banging my head aginst a wall…

  • 0

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();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:31:47+00:00Added an answer on June 6, 2026 at 4:31 am

    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
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT I have finally figured out the problem i have been having on my
I have been having this annoying problem when trying to implement a picture gallery
I have been having the following problem, i think it's probably due to the
Im having a small problem with my code. I have been trying to read
Good day everyone. I have been having the same problem all day at work
I've been having a really weird problem. I have a mutable array that is
I have been having some problem with the stringByAddingPercentEscapesUsingEncoding: method. Here's what happens: When
I have been having this problem for some time now, I dont exactly know
I have been having a problem where after my mongodb connection to mongohq via
I'm experiencing the strangest problem and have been having a terrible time debugging it.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.