views:

52

answers:

3

Hello,

I'm trying to insert in an Access Database, from visual C#. But i got this error: error

What am I doing wrong in code? The values are correct, they comes from the input boxes.

Thanks!

+3  A: 

From the picture it looks like the adapter.InsertCommand property is null.
Instead of

adapter.Insertcommand.CommandText = ...

use

insertCommand.CommandText = ...
adapter.InsertCommand = insertCommand;
Itay
+1  A: 

You're creating the OleDbDataAdapter adapter ok, and you're creating a (stand-alone) OleDbCommand insertCommand as well - but you're NOT creating an instance for adapter.InsertCommand - that variable will be NULL !

You need to do:

OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.InsertCommand = new OleDbCommand();

Create the adapter.InsertCommand instead of a stand-alone instance.

marc_s
Thanks! But why the data is not saved in the database? I have set this: connectionString = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=Admitere.mdb"; , and Admitere.mdb is in my local c# project folder..
qwerty
I would post a new question for that.
Adam
@qwerty: if your `adapter.InsertCommand` is NULL and not present - how should the adapter be able to insert data???
marc_s
A: 

At bottom left of picture I see "adapter.InserCommand" is null while the error occurred after you created a new "oleDbCommand" and it's the source of exception. Why ? Because you created one "oleDbCommand" and didn't assign it to your adapter.

Anyway, the way you deal with sql is not recommended and the code is prone to sql injection attacks. Also putting a large amount of code inside a "Try" block is not recommended since you cannot find the source of problem later.

Xaqron