none
can"t insert value in to sql TB with ado.net RRS feed

  • Question

  • Hi.

    why i can't insert data to sql table

    get this error

     

    SqlCommand insCmd = new SqlCommand(string.Format("insert into [dbo].[CUSTOM2] values('{0}', '{1}', null,null )", textBox1.Text, comboBox2.Text), con);

     

    SqlDataAdapter sqlDa = new SqlDataAdapter();

    sqlDa.InsertCommand = insCmd;

     

    if (con.State != ConnectionState.Open)

    con.Open();

    sqlDa.InsertCommand.ExecuteNonQuery();

    Insert Error: Column name or number of supplied values does not match table definition.

     

    error

    • Moved by Bob Beauchemin Tuesday, August 31, 2010 2:40 PM Moved to a more relevent forum (From:.NET Framework inside SQL Server)
    Tuesday, August 31, 2010 12:09 PM

Answers

  • You should provide list of the column names inside of your INSERT SQL statement. Your statement should looke like

    SqlCommand insCmd = new SqlCommand(string.Format("insert into [dbo].[CUSTOM2] ([CoumnName1], [ColumnName2], [ColumnName3], [ColumnName4]) values('{0}', '{1}', null,null )", textBox1.Text, comboBox2.Text), con);

    Also, in general, it is good idea to use parameters to pass values instead of usinf formatted string, otherwise you may introduce SQL injection vulnerability to your application


    Val Mazur (MVP) http://www.xporttools.net
    Thursday, September 2, 2010 10:30 AM
    Moderator