-6

I am trying to add some data into my database. I have 5 columns which all allow null values and all have datatype of varchar. But in my program I am passing only 4 values and fifth column should be empty. When I do this shows error

Number of supplied values does not match in the number of databse columns

How can I add NULL values into database?

My button click

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        string str=mas.empdetedit(ddid.SelectedItem.Text,txtname.Text,txtcon.text,txtval.text);
    }

My class is

public string empdetedit(string id, string name, string con, string val)
{
    string str=insert into table values('"+id+"','"+name+"','"+con+"','"+val+"'");
    conn.nonquery(str);
}
6
  • 5
    Please provide at least compiling code. Commented Sep 26, 2014 at 10:57
  • Soner Gönül, How you edited my question like this??I cant type question like this format Commented Sep 26, 2014 at 10:57
  • 7
    possible sql injection.. Commented Sep 26, 2014 at 10:57
  • 3
    possible duplicate of How to insert null value in Database through parameterized query Commented Sep 26, 2014 at 10:59
  • 1
    Please take a tutorial with database record manipulation. Now you asking questions step after step: how insert null, date, boolean... Commented Sep 26, 2014 at 11:01

1 Answer 1

2

The syntax to insert into only specific columns of a table is:

INSERT INTO table_name (column1,column2,column3) VALUES (value1,value2,value3)

You are not explicitly specifying the column names, so the database thinks you are giving values for all of them.

Sign up to request clarification or add additional context in comments.

1 Comment

And also: use a parametrized query to do the insert to avoid SQL injection attacks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.