1

I have made a datagridview with data from database, then I have 3 different forms with insert data, delete data, etc.

The problem is that when I'm trying to insert data and restart the program, the data doesn't get saved.

Here is my code:

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");

con.Open();

string sql = "INSERT INTO Elevi (Nume, Prenume, SportulPracticat) VALUES(@nume, @prenume, @sport)";

using (SqlCommand cmd = new SqlCommand(sql, con))
{
    cmd.Parameters.AddWithValue("@nume", textBox1.Text);
    cmd.Parameters.AddWithValue("@prenume", textBox2.Text);
    cmd.Parameters.AddWithValue("@sport", textBox3.Text);

    cmd.ExecuteNonQuery();
}

con.Close();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dabuleanu Catalin
  • 59
  • 1
  • 1
  • 15
  • 1
    Have you run this in debug mode to see what's happening? The way it's written, it looks like as soon as `cmd.ExecuteNonQuery()` is executed, you should have a row inserted into the table. – SlimsGhost Mar 23 '16 at 18:29
  • Probably it is this http://stackoverflow.com/questions/17147249/why-saving-changes-to-a-database-fails/17147460#17147460 – Steve Mar 23 '16 at 18:33
  • 1
    Yes.i run it into the Debug mode.And i do have the row inserted.But it doesnt save.As soon as I press X button and run the dubug again,the row inserted dissapears. – Dabuleanu Catalin Mar 23 '16 at 18:34
  • No,Steve.It is not.I tried doing that without success. – Dabuleanu Catalin Mar 23 '16 at 18:49
  • Let me ask you another clarification. ExecuteNonQuery returns the number of rows affected. Could you get that value and tell us if it is zero or one? If it is one then your record has been inserted and some other mechanism causes you record to disappear, not this code – Steve Mar 23 '16 at 19:01

3 Answers3

1

The whole User Instance and AttachDbFileName= approach is flawed - at best! When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\bin\debug - where you app runs) and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!

If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.

The real solution in my opinion would be to

  1. install SQL Server Express (and you've already done that anyway)

  2. install SQL Server Management Studio Express

  3. create your database in SSMS Express, give it a logical name (e.g. YourDatabase)

  4. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:

    Data Source=.\\SQLEXPRESS;Database=YourDatabase;Integrated Security=True
    

    and everything else is exactly the same as before...

Also see Aaron Bertrand's excellent blog post Bad habits to kick: using AttachDbFileName for more background info.

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

I can't spot any error. Maybe you're looking on the wrong piece of code. If idata vanish from db when starting the debug perhaps you should look to what happens before or after (do you perform a deletion or a truncate?).

-1

Try adding a COMMIT; command to the SQL code

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
SqlTransaction trans = con.BeginTransaction();
con.Open();
string sql = "INSERT INTO Elevi (Nume , Prenume , SportulPracticat) VALUES(@nume,@prenume,@sport)";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
    cmd.Transaction = trans;
    cmd.Parameters.AddWithValue("@nume", textBox1.Text);
    cmd.Parameters.AddWithValue("@prenume", textBox2.Text);
    cmd.Parameters.AddWithValue("@sport", textBox3.Text);
    cmd.ExecuteNonQuery();
    trans.Commit();
}
con.Close();