C # MDF database does not insert data even if success message is displayed

I need to insert data when the user clicks. But my code doesn't do that. Although it displays the entered data, no data is inserted. How can I find the error?

private void bunifuFlatButton2_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=**F:\Blackhat\Blackhat\Blackhat.mdf**;Integrated Security=True");
        try
         {
            con.Open();
            SqlCommand cmd = new SqlCommand("Insert Into Clients(name) VALUES ('"+clientname.Text+"')", con);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Success "+clientname.Text);
             con.Close();
         }
         catch (SqlException ex)
         {
             MessageBox.Show("Failed"+ex);
         }
    }

      

+3


source to share


1 answer


Since there are no exceptions, the problem will be like this: you are writing it to the wrong file in the wrong place or something like that.

If it fails to write, there will be an exception and it will be posted to the catch block.

So, we can expect that there is nothing wrong with C # code.



If you are checking records using SQL Server Management Studio, try querying the database. The edit 200 rows option requires updating the database and editing again after closing the existing edit tab. otherwise, the values ​​are not changed.

Hope this helps. If the problem is something else, please indicate in the comments.

0


source







All Articles