user3834541
user3834541

Reputation: 161

AutoNumber Column insertion in MS Access Database

here is my Database Structure

My table name is employee

First Column is "f_name" datatype "Short Text"
Second       is "l_name" datatype "Short Text"
Third        is "id"     datatype "AutoNumber" Primary Key

C# Code :

OleDbCommand com1 = new OleDbCommand("insert into employee values(@f_name,@l_name)", con);

com1.Parameters.AddWithValue("@f_name", TextBox1.Text);
com1.Parameters.AddWithValue("@l_name", TextBox2.Text);

com1.ExecuteNonQuery();

this code gives an Error "Number of query values and destination fields are not the same"

I am using MS Access 2013 Database and ASP.Net C#.

Upvotes: 0

Views: 1084

Answers (1)

Habib
Habib

Reputation: 223217

You have to explicitly specify the columns in INSERT statement, otherwise it will treat all columns in INSERT statement and hence the error.

OleDbCommand com1 = new OleDbCommand("insert into employee (f_name, l_name) values(@f_name,@l_name)", con);

Your table has a column id which is AutoNumber, but since you didn't specify any columns explicitly in your query it would be like:

insert into(f_name,l_name,id) employee values(@f_name,@l_name)

Therefore you are getting the error.

Upvotes: 2

Related Questions