Click here to Skip to main content
15,911,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A created a MS Access file with fields and designed a C# form to insert data into these fields. This is the block of code written to insert a textbox field to the database.
C#
private void button1_Click(object sender, EventArgs e)
        {
            string insertCommand = "INSERT into tblContact (First Name) " + " VALUES( + fntxtbx.Text + )";

            OleDbConnection datacon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\CROSSLINK\\Documents\\Visual Studio 2008\\Projects\\contact1.mdb");

            OleDbCommand dbcommand = new OleDbCommand( insertCommand, datacon );
                       
            dbcommand.Connection = datacon;
            datacon.Open();
            
            OleDbDataAdapter b = new OleDbDataAdapter();

            DataSet dset = new DataSet();

            b.Fill(dset);
            datacon.Close();
        }

When I tried to running the application the following "unhandled Exception" error appeared...
Unhandled exception has occurred in your application. If you click....
The SelectCommand property has not been initialized before calling "Fill"

Please how can I resolve this issue? Do I have to keep the access file running before this can work or what?
Posted
Updated 23-Nov-10 21:52pm
v2

The problem is that you have created the OleDbDataAdapter but you haven't assigned the OleDbCommand to it.
After;
C#
OleDbDataAdapter b = new OleDbDataAdapter();

Put;
C#
b.SelectCommand = dbcommand ;

or you can write it as;
C#
b.SelectCommand = new OleDbCommand(insertCommand, datacon );


And change your query to;
C#
string insertCommand = "INSERT into tblContact (First Name) VALUES('" + fntxtbx.Text.Replace("'","''") + "')";

Otherwise any names with an apostrophe will cause errors and open things up for a Sql injection attack.
 
Share this answer
 
v3
Hi,

try this:

private void button1_Click(object sender, EventArgs e)
{
  string insertCommand = "INSERT into tblContact (First Name) " + " VALUES( + fntxtbx.Text + )";

  OleDbConnection datacon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\CROSSLINK\\Documents\\Visual Studio 2008\\Projects\\contact1.mdb");

  OleDbCommand dbcommand = new OleDbCommand( insertCommand, datacon );

  dbcommand.Connection = datacon;
  datacon.Open();

  OleDbDataAdapter b = new OleDbDataAdapter();
  b.SelectCommand = dbcommand;
  DataSet dset = new DataSet();

  b.Fill(dset);
  datacon.Close();
}


You forget to set the "SelectCommand" that you had already createdd
 
Share this answer
 
v2
Comments
Dalek Dave 24-Nov-10 4:20am    
Good Answer.
HubSnippets 24-Nov-10 10:15am    
I tried the code but still got this error:

"Syntax error in INSERT INTO statement"
Please what else is the problem?
JF2015 24-Nov-10 10:25am    
If you still get an error, then you should check if your command string is correct - we can't do this for you.

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