i am having error in insert statement? [Resolved]

Posted by Sudheep.grandhe under C# on 11/2/2011 | Points: 10 | Views : 2160 | Status : [Member] | Replies : 4
Hi,

i am having error in insert statement?.can you please rectify this problem.I am underlying the error.please find below.

Error:-There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
private void btn_submit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
SqlCommand cmd = new SqlCommand("insert into tbl_customer(custname,custaddress,custcity,custstate,custstdcode,custphoneno,custmobileno)values(@custname,@custaddress,@custcity,@custstate,@custstdcode,@custmobileno)", con);
cmd.Parameters.AddWithValue("@custname", txt_customername.Text);
cmd.Parameters.AddWithValue("@custaddress", txt_address.Text);
cmd.Parameters.AddWithValue("@custcity", txt_customercity.Text);
cmd.Parameters.AddWithValue("@custstate", txt_customerstate.Text);
cmd.Parameters.AddWithValue("@custstdcode", txt_stdcode.Text);
cmd.Parameters.AddWithValue("@custphoneno", txt_phoneno.Text);
cmd.Parameters.AddWithValue("@custmobileno", txt_mobileno.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("customer added successfully..");
con.Close();
txt_customername.Text = "";
txt_address.Text = "";
txt_customercity.Text = "";
txt_customerstate.Text = "";
txt_stdcode.Text = "";
txt_phoneno.Text = "";
txt_mobileno.Text = "";
}

Best,
Sudheep.



Responses

Posted by: Vuyiswamb on: 11/2/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 50

Up
0
Down

Resolved
The Error means you have more Columns than the Insert Values, They must be equal they must match.

Thank you for posting at Dotnetfunda
[Administrator]

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ulhasmorbale on: 11/2/2011 [Member] Starter | Points: 25

Up
0
Down
SqlCommand("insert into tbl_customer(custname,custaddress,custcity,custstate,custstdcode,custphoneno,custmobileno)values(@custname,@custaddress,@custcity,@custstate,@custstdcode,@custmobileno)"

Customer Phone no is not passed in parameter list, please check.

Regards,
Ulhas Morbale

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: NTurnbullJr on: 11/5/2011 [Member] Starter | Points: 25

Up
0
Down
Change SqlCommand cmd = new SqlCommand("insert into tbl_customer(custname,custaddress,custcity,custstate,custstdcode,custphoneno,custmobileno)values(@custname,@custaddress,@custcity,@custstate,@custstdcode,@custmobileno)", con);

to
SqlCommand cmd = new SqlCommand("insert into tbl_customer(custname,custaddress,custcity,custstate,custstdcode,custphoneno,custmobileno)values(@custname,@custaddress,@custcity,@custstate,@custstdcode,@custphoneno,@custmobileno)", con);

this adds ,@custphoneno to the values.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 11/6/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Vuyiswamb helped me a lot.he will take over teamviewer help and he solve the what ever problems i am having in my project.keep doing helping to others.he helped me a lot i never forgot such kind of helping person.

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response