When i try to insert the value from the textbox into the database, the values are not being updated in the database. But the newly inserted rows are temporarily available, after inserting when i use select query to get the rows, the new rows are available. When i close the solution and open it again, the newly insert rows are gone. The table in the database explorer is always not updated.
Here is my code.
string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";
string na = textBox1.Text;
int ag = int.Parse(textBox2.Text);
string ci = textBox3.Text;
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand insertCommand = connection.CreateCommand())
{
insertCommand.CommandText = "INSERT INTO address(name,age,city) VALUES (@na,@ag,@ci)";
insertCommand.Parameters.AddWithValue("@na", na);
insertCommand.Parameters.AddWithValue("@ag", ag);
insertCommand.Parameters.AddWithValue("@ci", ci);
insertCommand.Connection.Open();
insertCommand.ExecuteNonQuery();
insertCommand.Connection.Close();
MessageBox.Show("finish");
}
//connection.Close();
}
The table name is “address” and has three fields name(varchar(50)),age(int),city(nchar(10))
Please help.
You said “C:\Users\Thangamani\documents\visual studio 2010\Projects\AddressBook\AddressBook\bin\Debug\” in the error. You are attaching to the local copy during debug.
Now that you are not copying the database over, that mdf file does not exist, hence the error.
Change your connection string to access the original database, not the local copy (which you are not making anymore).