Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using three separate radiobutton and three dropdownbox,when i try to submit the query it return error as Column name or number of supplied values does not match table
what should i do.what should i give default value for three radiobutton.help me

What I have tried:

SqlCommand cmd=new SqlCommand("insert into Order_TB values('"+TxtDescription.Text+"','"+DdlWeek.Text+"','"+Ddlmonth.Text+"','"+DropDownList4.Text+"')",con);
cmd.ExecuteNonQuery();
con.Close();
Posted
Updated 24-May-16 23:25pm

always use parameterised query to avoid SQL Injection[^] attacks

try like this,
C#
SqlCommand cmd = new SqlCommand("insert into Order_TB (column1Name,column2Name,column3Name,column4Name) values(@value1,@value2,@value3,@value4)", con);
           cmd.Parameters.Add("@value1", TxtDescription.Text);
           cmd.Parameters.Add("@value2", DdlWeek.Text);
           cmd.Parameters.Add("@value3", Ddlmonth.Text);
           cmd.Parameters.Add("@value4", DropDownList4.Text);
           cmd.ExecuteNonQuery();
           con.Close();


Your sql statement is not working because the no of columns is not matching with the no of values you are trying to insert. your code will work if it has only 4 columns.
 
Share this answer
 
Firstly check to make sure that the table has only the 4 columns that you have provided, if not then specify them by

C#
SqlCommand cmd = new SqlCOmmand("insert into Order_Tb (Columnname1, Columnname2, Columnname3, Columnname4) values ('"+TxtDescription.Text+"','"+DdlWeek.Text+"','"+Ddlmonth.Text+"','"+DropDownList4.Text+"')",con);


But make sure that you replace Columnname1, Columnname2 etc with the actual column names from the table.

ALSO it is best practise not to concatenate strings together as this will open you to SQL Injection. Have a look at SQL Parameters, simple example in the following link.
http://www.dotnetperls.com/sqlparameter[^]
 
Share this answer
 
The error clearly says that You are not providing all the columns needed in table. If you want to insert some columns only you need to define those column explicitly.

Eg.
SQL
SqlCommand cmd=new SqlCommand("insert into Order_TB (Column1, Column2, Column3, Column4)values('"+TxtDescription.Text+"','"+DdlWeek.Text+"','"+Ddlmonth.Text+"','"+DropDownList4.Text+"')",con);
cmd.ExecuteNonQuery();
con.Close();


And one very important thing. You should never pass your text box or any other value to the insert script directly in code. It just opens door for sql injection. Alwaysuse sql parameter.


SqlParameter Class (System.Data.SqlClient)[^]
 
Share this answer
 
Never use concatenated strings to create your SQL command.

Use parameterised queries. Not only will that help to protect you from SQL Injection attacks, it makes it easier to construct the queries and helps to avoid some errors.

For example:
C#
SqlCommand cmd=new SqlCommand("insert into Order_TB values(@Description,@Week,@month, @List4)",con);
 cmd.Parameters.AddWithValue("@Description", TxtDescription.Text);
 cmd.Parameters.AddWithValue("@Week", DdlWeek.Text);
 cmd.Parameters.AddWithValue("@month", Ddlmonth.Text);
 cmd.Parameters.AddWithValue("@List4", DropDownList4.Text);
 cmd.ExecuteNonQuery();
 con.Close(); 


Now look at your database table. What columns does it have ... Description, Week, month, List4 and nothing else (other than possibly an IDENTITY column).

If there are more columns on the table than that, then you need to do one of two things.

1. Give a list of the columns which you are going to provide. E.g.
C#
SqlCommand cmd=new SqlCommand("insert into Order_TB (Description, Week, Month, List4) values(@Description,@Week,@month, @List4)",con);
(Note I have just guessed the column names - use your actual column names not the ones I have put in here)

2. Provide values for all of the columns in the table
C#
SqlCommand cmd=new SqlCommand("insert into Order_TB values(@Description,@Week,@month, @List4, @column5, @column6)",con);
... etc
 
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