Home > front end >  When I try to insert form data into database, calling ExecuteNonQuery results in an error
When I try to insert form data into database, calling ExecuteNonQuery results in an error

Time:08-28

When I click on insert button, calling ExecuteNonquery throws the error shown. Why does this error happen, and how to solve it?

This the code of insert button:

protected void Insert_Click(object sender, EventArgs e)
{
    Console.WriteLine("Test"   conn.ConnectionString);
    conn.Open();

    SqlCommand cmd = new SqlCommand("insert into BasicInfo values('"   Name.Text   "','"   F_Name.Text   "','"   Email.Text   "','"   Address.Text   "') ", conn);

    var c = cmd.ExecuteNonQuery();

    if (c > 0)
        Message.Text = "Data inserted";
    else
        Message.Text = "Not inserted";

    conn.Close();

    GridView1.DataBind();

    Name.Text = "";
    F_Name.Text = "";
    Email.Text = "";
    Address.Text = "";
}

This is error:

enter image description here

CodePudding user response:

The following is a conceptual example to add a new record to a database table, in this case using .NET Core.

Rather than performing work in a click event, place code into a class method.

In the following code Customer is a class with various properties which you would create and populate from form controls.

public class DataOperations
{

    public static (bool success, Exception exception) InsertCustomer(Customer customer)
    {
        string statement =
        @"INSERT INTO dbo.Customer (CompanyName,ContactName,ContactTypeIdentifier,GenderIdentifier) 
          VALUES (@CompanyName,@ContactName,@ContactTypeIdentifier,@GenderIdentifier);
          SELECT CAST(scope_identity() AS int);";

        using var cn = new SqlConnection("Your connection string");
        using var cmd = new SqlCommand { Connection = cn, CommandText = statement };

        cmd.Parameters.Add("@CompanyName", SqlDbType.NChar).Value = 
            customer.CompanyName;
        cmd.Parameters.Add("@ContactName", SqlDbType.NChar).Value = 
            customer.ContactName;
        cmd.Parameters.Add("@ContactTypeIdentifier", SqlDbType.Int).Value = 
            customer.ContactTypeIdentifier;
        cmd.Parameters.Add("@GenderIdentifier", SqlDbType.Int).Value = 
            customer.GenderIdentifier;

        try
        {
            cn.Open();
            customer.Identifier = Convert.ToInt32(cmd.ExecuteScalar());
            return (true, null);
        }
        catch (Exception localException)
        {
            return (false, localException);
        }

    }

}

Usage: On success the customer object will have the primary key value set

protected void Insert_Click(object sender, EventArgs e)
{
    // here no properties are set for a real app the
    // properties would be set from form controls
    Customer customer = new Customer();
    var (success, exception) = DataOperations.InsertCustomer(customer);
    if (success)
    {
        // record was added
    }
    else
    {
        // use 'exception to log/or inspect error
    }
}

CodePudding user response:

It seems like you don't have BasicInfo table in database. Also Check the connection string if you are providing correct database name if you think you have created the table.

  • Related