I am trying to write a C# application which will have an attached database.
So I did an insert operation which is working perfectly and after insert operation I can see my data in my data grid view. But after closing the application, I saw that my data table is empty. So I reopened my application and the data grid view is also empty.
Here is my sample code :
Insert
private void button_create_Click(object sender, EventArgs e)
{
int id = 7;
string ConStr = ConfigurationManager.ConnectionStrings["Doctor_s_Assistant.Properties.Settings.DocAssistDBConnectionString"].ConnectionString;
SqlConnection offDBconnection = new SqlConnection(ConStr);
offDBconnection.Open();
string sqlCommand = "INSERT INTO Treatment_Template VALUES (@id, @t_name, @t_body, @t_advice)";
SqlCommand sql = new SqlCommand(sqlCommand, offDBconnection);
sql.Parameters.AddWithValue("@id",id);
sql.Parameters.AddWithValue("@t_name", textBox_temp_name.Text);
sql.Parameters.AddWithValue("@t_body", richTextBox_temp_body.Text);
sql.Parameters.AddWithValue("@t_advice", richTextBox_temp_advice.Text);
sql.ExecuteNonQuery();
offDBconnection.Close();
MessageBox.Show("New Template Created");
}
Preview data:
private void button_Load_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DocAssistDB.mdf;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select * from Treatment_Template", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
Thanks in advance