Click here to Skip to main content
15,904,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting run time error of "executereader:connection property has not been initialized", while running this code
C#
 private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
             try
            {
                string sconnection;
                sconnection = "provider=microsoft.jet.oledb.4.0;" + "data source=intuitive.mdb";
                OleDbConnection dbconn;
                dbconn = new OleDbConnection(sconnection);
                dbconn.Open();


                OleDbCommand cmd = new OleDbCommand();
                OleDbDataReader dr = null;
                dr = cmd.ExecuteReader();
                 
                cmd.CommandText = "select * from table";

              //  dr = cmd.ExecuteReader();

                string userText = textBox1.Text;
                string passText = textBox2.Text;
             

                while (dr.Read())
                {
                   if (this.CompareStrings(dr["username"].ToString(), userText) &&
                        this.CompareStrings(dr["password"].ToString(), passText))
         
                    {
                        MessageBox.Show("OK");
                    }
                    else
                    {
                        MessageBox.Show("Error");
                    }

                }

                dr.Close();

                dbconn.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
} 

Please help me out of this problem.
Posted
Updated 29-Nov-11 20:36pm
v2
Comments
RaisKazi 30-Nov-11 2:36am    
Edited: 1) Formatting 2) Added "pre" tag.

Interchange the following codes:

FROM:
C#
dr = cmd.ExecuteReader();
cmd.CommandText = "select * from table";


TO:
C#
cmd.CommandText = "select * from table";
dr = cmd.ExecuteReader();


Regards,
Eduard
 
Share this answer
 
There is problem in your connection string, try following.
C#
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\intuitive.mdb;User Id=UserID;Password=PWD;
 
Share this answer
 
You have to initialize Connection Property of OleDbCommand object before executing any query.Write this code before executing your query.
C++
cmd.Connection = dbconn

and after that still there will be error then please check Connection String.
Check out following link :Connection Strings :)
 
Share this answer
 
you only opened the connection but never assigned it to the command.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900