0

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

You're either looking in the wrong DB (you're looking in the MDF file in the project main folder alongside your csproj but the MDF your app altered is found in the BIN\DEBUG or BIN\RELEASE subfolder - the MDF was copied there during build)

Or

For your MDF file you have "copy to output folder" set to Copy Always so your build prices is always overwriting the MDF that your exe is editing (in the BIN\xxx subfolders) with a fresh blank database from the project folder


Click the MDF in Solution Explorer and set the copy option to Copy If Newer in the Properties window. Always make schema changes to the MDF in the project folder, not the bin folder. Be aware that when you make schema changes etc the file will become newer and overwrite your test data - if you want to keep the test data and other timings your program has written, copy the file back from bin folders first, then change the schema, then it will be copied out again during builds


Oh, by the way; check out http://dapper-tutorial.net - it will save you wasting your life writing tedious data access code; all that code you wrote in your post can be reduced to about 4 lines

Caius Jard
  • 72,509
  • 5
  • 49
  • 80