[Solved] There are more columns in the INSERT statement than values specified in the VALUES clause [closed]


Your problem is that you are attempting to insert 3 values:

values('{0}','{1}',N'{2}')

Into 4 columns:

(nokar,modeledokht,tozihat,username) 

I believe you meant to do this:

values('{0}','{1}',N'{2}','{3}')

Side note:

Always use Command.Parameters instead of parsing your command as string! When parsing the command as a string, you are subjected to SQL injections and errors like the one you are having. Using Command.Parameters makes it safer and easier.

Example:

SqlCommand Command = new SqlCommand();
Command.CommandText = "insert into tbl (col) values (@val)";
Command.Parameters.Add(new SqlParameter("val", valueVariable));
Command.ExecuteNonQuery();

Or, in your case:

SqlCommand Command = new SqlCommand();

Command.CommandText = @"insert into Ordertb 
                        (nokar,modeledokht,tozihat,username) 
                        values 
                        (@nokar,@modeledokht,@tozihat,@username)";

Command.Parameters.Add(new SqlParameter("nokar", DropDownList1.Text));
Command.Parameters.Add(new SqlParameter("modeledokht", DropDownList2.Text));
Command.Parameters.Add(new SqlParameter("tozihat", tozihtxt.Text));
Command.Parameters.Add(new SqlParameter("username", Convert.ToString(Session["username1"])));

Command.ExecuteNonQuery();

4

solved There are more columns in the INSERT statement than values specified in the VALUES clause [closed]