2

I have a database that contains a Customer table with the following columns : CustID, CustName, ICNumber, ContactNumber and Address. It is a service-based database.

string localdb = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;

SqlConnection con = new SqlConnection(localdb);
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO Customer(CustID,CustName,ICNumber,ContactNumber,Address)values(@CustID,@CustName,@ICNumber,@ContactNumber,@Address)", con);

                        cmd.Parameters.AddWithValue("@CustID", txtCustID.Text);
                        cmd.Parameters.AddWithValue("@CustName", txtCustName.Text);
                        cmd.Parameters.AddWithValue("@ICNumber", txtICNum.Text);
                        cmd.Parameters.AddWithValue("@ContactNumber", txtContact.Text);
                        cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
                        cmd.ExecuteNonQuery();
                        con.Close();

The code compiles and runs. The problem I am having is that the record is not added into the table after cmd.ExecuteNonQuery(); is called.

Why is the record not showing up in the database table?

3
  • 1
    What value does ExecuteNonQuery return? Also, you should wrap your SqlConnection and SqlCommand in using blocks. Commented Nov 2, 2015 at 12:44
  • Your code appears correct so this is most likely a database issue. Is your primary key for the table CustID? If it is you cannot enter it like you are trying to do. Commented Nov 2, 2015 at 12:45
  • Are you using a local database file? If so, are you certain you're looking in the right file? Your project config may be making a copy of the local DB file and inserting into that. Commented Nov 2, 2015 at 16:13

1 Answer 1

1

You forgot to close the quotes " on the insert command. It is nice to use try/catch to avoid problems with your insert, for sample:

string localdb = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;

try
{
    SqlConnection con = new SqlConnection(localdb))


    con.Open();

    SqlCommand cmd = new SqlCommand("INSERT INTO Customer(CustID, CustName, ICNumber, ContactNumber, Address) VALUES (@CustID, @CustName, @ICNumber, @ContactNumber, @Address)", con);
    cmd.Parameters.AddWithValue("@CustID", txtCustID.Text);
    cmd.Parameters.AddWithValue("@CustName", txtCustName.Text);
    cmd.Parameters.AddWithValue("@ICNumber", txtICNum.Text);
    cmd.Parameters.AddWithValue("@ContactNumber", txtContact.Text);
    cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
    cmd.ExecuteNonQuery();
}
catch
{
    // some errors 
}
finally 
{
    if (con.State == ConnectionState.Open)
       con.Close();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Why not use an using statement?
Yes, could be using statement, but what happen if the database server is out?

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.