Hope someone can help with this. The following code is meant to add an entry to a Table; however, whilst the data is added at runtime, when the app is closed it disappears. The Database is set to 'Copy if Newer'.
private void AddToNameList()
{
SqlConnection con = new SqlConnection(connectionString);
cmd = new SqlCommand("INSERT INTO NameSurname VALUES (@NameId, @Surname)", con);
cmd.Parameters.AddWithValue("@NameId", textBox2.Text);
cmd.Parameters.AddWithValue("@Surname", textBox3.Text);
con.Open();
int i= cmd.ExecuteNonQuery();
con.Close();
if (i != 0)
{
MessageBox.Show(i + "Data Saved");
}
textBox2.Clear();
textBox3.Clear();
}
private void button1_Click(object sender, EventArgs e)
{
if(textBox2.Text!= ("") && textBox3.Text!=(""))
{
AddToNameList();
}
else
{
MessageBox.Show("Please enter Name and Surname","Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
The message box confirms that the data is saved, and when I carry out a search it is there. However, when I stop the app running and check the database, the data is not there.
Grateful for any help.
Thanks