Click here to Skip to main content
16,004,041 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
this is code

con = new SqlConnection(str);
            da = new SqlDataAdapter();
            da.InsertCommand = new SqlCommand("INSERT INTO customers VALUES (@name,@address,@insurance_cost,@record_date)", con);
            da.InsertCommand.Parameters.Add("@name", SqlDbType.NVarChar).Value = textBox1.Text;
            da.InsertCommand.Parameters.Add("@address", SqlDbType.NVarChar).Value = textBox2.Text;
            da.InsertCommand.Parameters.Add("@insurance_cost", SqlDbType.Money).Value = textBox3.Text;
            da.InsertCommand.Parameters.Add("@record_date", SqlDbType.DateTime).Value = dateTimePicker1.Value;
            con.Open();
            da.InsertCommand.ExecuteNonQuery();
            con.Close();
Posted
Updated 3-Dec-11 4:23am
v3

How many columns do you have in the table? If it isn't four, and they aren't in the right order, you will get an error, even if the "extra" columns are marked as "nulls allowed". Try reworking the insert statement to list teh fields in teh order they will be set:
SQL
INSERT INTO customers (name, address, insurance_cost, record_date) VALUES (@name,@address,@insurance_cost,@record_date)


BTW: You shouldn't use the Add method - it was depreciated years ago, and may not be supported in future versions. Use AddWithValue instead:
SQL
da.InsertCommand = new SqlCommand("INSERT INTO customers (name, address, insurance_cost, record_date) VALUES (@name,@address,@insurance_cost,@record_date)", con);
da.InsertCommand.Parameters.AddWithValue("@name", textBox1.Text);
da.InsertCommand.Parameters.AddWithValue("@address", textBox2.Text);
da.InsertCommand.Parameters.AddWithValue("@insurance_cost", textBox3.Text);
da.InsertCommand.Parameters.AddWithValue("@record_date", dateTimePicker1.Value);
 
Share this answer
 
Please recheck your query it's sytax is wrong.The correct Syntax is as follows :
da.InsertCommand = new SqlCommand("INSERT INTO customers (name,address,insurance_cost,record_date) VALUES (@name,@address,@insurance_cost,@record_date)", con);
            da.InsertCommand.Parameters.Add("@name", SqlDbType.NVarChar).Value = textBox1.Text;
            da.InsertCommand.Parameters.Add("@address", SqlDbType.NVarChar).Value = textBox2.Text;
            da.InsertCommand.Parameters.Add("@insurance_cost", SqlDbType.Money).Value = textBox3.Text;
            da.InsertCommand.Parameters.Add("@record_date", SqlDbType.DateTime).Value = dateTimePicker1.Value;
            con.Open();
            da.InsertCommand.ExecuteNonQuery();
            con.Close();

Also checkout the no of columns present in your table and no of columns you wrote in the query and their Names are proper or not. :)

- MKB
 
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