none
OleDBDataReader Problem . RRS feed

  • Question

  • Please tell me whats wrong this code . reader show error 

    {"ExecuteReader: Connection property has not been initialized."}

     OleDbDataReader reader= command.ExecuteReader();

     private void Form1_Load(object sender, EventArgs e)
            {
                
    
                    connection.Open();
                    OleDbCommand command = new OleDbCommand();
                    string query = "select * From PatientData";
                    command.CommandText = query;
                    OleDbDataReader reader= command.ExecuteReader();
                    while(reader.Read())
                    {
                        comboBox1.Items.Add(reader["PatientID"].ToString());
                    }
                    connection.Close();
                }


    • Edited by saqsaqPK Friday, March 30, 2018 8:57 AM update
    Friday, March 30, 2018 8:54 AM

Answers

  • You need to set the connection property of the command to your open connection.

    command.Connection = connection;


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by CoolDadTxModerator Friday, March 30, 2018 2:05 PM
    • Marked as answer by saqsaqPK Friday, March 30, 2018 2:06 PM
    Friday, March 30, 2018 9:22 AM
    Moderator

All replies

  • You need to set the connection property of the command to your open connection.

    command.Connection = connection;


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by CoolDadTxModerator Friday, March 30, 2018 2:05 PM
    • Marked as answer by saqsaqPK Friday, March 30, 2018 2:06 PM
    Friday, March 30, 2018 9:22 AM
    Moderator
  • Thank you Problem is solved 
    Friday, March 30, 2018 2:06 PM