System.Data.SqlClient.SqlException (0x80131904) when trying to send data to Database

-1

Hello everyone this is the code i wrote in order to get the data from a simple form and import it into my DataBase:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        SqlConnection connection = new SqlConnection("Data Source=ICSD-DB\\ICSDMSSQLSRV;Initial Catalog=icsd15005;Integrated Security=True");
        //1os tropos
        String sqlString = "INSERT INTO pr_foititis VALUES(@arithmos_taut,@onoma,@eponymo, @imerominia_proslipsis,@imerominia_gennisis,@misthos)";
        //2os tropos mh asfales, Sql injection, gia na to ektelesoume afairoume apo kato tis entoles pou ksekinoun me command.Parameters.Add 
        //String sqlString = "INSERT INTO pr_foititis VALUES('"+id_Tb.Text+"','"+name_Tb.Text;+"','"+surname_Tb.Text;+"', "+age_Tb.Text;+")";  

        try
        {
            connection.Open();
            SqlCommand command = new SqlCommand(sqlString, connection);
            command.Parameters.Add("@arithmos_taut", SqlDbType.Int).Value = TextBox1.Text;
            command.Parameters.Add("@onoma", SqlDbType.VarChar).Value = TextBox2.Text;
            command.Parameters.Add("@eponymo", SqlDbType.VarChar).Value = TextBox3.Text;
            command.Parameters.Add("@imerominia_proslipsis", SqlDbType.Date).Value = TextBox4.Text;
            command.Parameters.Add("@imerominia_gennisis", SqlDbType.Date).Value = TextBox5.Text;
            command.Parameters.Add("@misthos", SqlDbType.Float).Value = TextBox6.Text;
            command.ExecuteNonQuery();
            connection.Close();
            resultLabel.Text = "All Good!";
        }
        catch (Exception ex)
        {
            resultLabel.Text = ex.ToString();
        }
    }
}

Here is the Exception:

System.Data.SqlClient.SqlException (0x80131904): Column name or number of supplied values does not match table definition. (line 35)

c#
sql-server
ado.net
asked on Stack Overflow Jun 1, 2017 by Phill • edited Aug 1, 2019 by Phill

2 Answers

2

Unless the number (and order) of the VALUES you have in your INSERT statement match the number and order of columns in your table exactly, you need to define the columns.

String sqlString = "INSERT INTO pr_foititis (ColumnA, ColumnB, ColumnC, ColumnD, ColumnE, ColumnF) VALUES (...)"
answered on Stack Overflow Jun 1, 2017 by Jacob H
0

You are getting this error because your table contents more columns than your supplied values.

Just cross check your table columns or use below code.

String sqlQuery = "INSERT INTO pr_foititis (Column1, Column2, Column3, Column4) VALUES (Value1,Value2,Value3,Value4)"

Hope this will help you.

Thanks!

answered on Stack Overflow Jun 2, 2017 by Asif Ghanchi

User contributions licensed under CC BY-SA 3.0