Have another issue related to an earlier post.
I know this code works on an old SQL database without a primary key. I re-did my SQL d-base and added a primary key (Column Name - id) and changed my code to match the new d-base instance. I created the id column as data type int and set it to Identity Specification = Yes, and (Is Identity) = Yes to create an auto-numbering identity sequence, and now I am not getting my records into the database anymore (not getting any errors either???). The only difference is the primary key, so I know that is the issue. I am not accounting for the primary key in my code, but shouldn't the primary key with the Identity Specification set to Yes, auto populate when the rest of the data is inserted into the d-base record?
What gives?
Thanks in advance.
My code on the click event looks like this:
private void button1_Click(object sender, EventArgs e)
{
//add record from text boxes to database
try
{
System.Data.SqlClient.SqlConnection con;
con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\decontact.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlCommand insert = new SqlCommand();
insert.CommandText = "INSERT INTO decontact (fname, lname, address, city, state, zip, phone1, phone2, email, notes)VALUES ('" + txtFname.Text + "', '" + txtLname.Text + "', '" + txtAddress.Text + "', '" + txtCity.Text + "', '" + txtState.Text + "', '" + txtZip.Text + "', '" + txtPhone1.Text + "', '" + txtPhone2.Text + "','" + txtEmail.Text + "','" + txtNotes.Text + "')";
con.Open();
insert.Connection = con;
insert.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Added");
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}

New Topic/Question
Reply




MultiQuote




|