1

I am using a .mdf file for database (SQL Server) and ADO Model in Windows Forms using C#. On a button click, the data is saved into the database, and I have debugged this. But when I close the application, the data vanishes.

Here is my code:

private void btn_add_patientt_Click(object sender, EventArgs e)
{
        Doctor doctor = new Doctor
        {
            First_Name = A_doc_name.Text,
            Last_Name = A_doc_last.Text
        };
        db.Doctors.Add(doctor);
        db.SaveChanges();
        var v = (from a in db.Doctors select a.Doctor_id).Max();
        int x = int.Parse(v.ToString());
        MessageBox.Show(x.ToString());
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Do you start software from VS? Then uncheck "copy to output folder" setting for this mdf-file. – Sinatr Dec 18 '17 at 10:37
  • 1
    Please show your connectionstring. What is the tool used to check for the data? Did you use the Visual Studio Server Explorer? Check [Why saving changes to database fails](https://stackoverflow.com/questions/17147249/why-saving-changes-to-a-database-fails) – Steve Dec 18 '17 at 10:38
  • If the data was saved in the DB, it's highly unlikely it would just "disappear" when the app closes. This kind of thing doesn't happen all by itself. Check there's not some other code or config which is doing more updates / deletes to the database, or re-creating the database when the app restarts. Since it's an MDF file-based DB, it could be that Visual Studio is over-writing it every time, if you're just running in debug mode. – ADyson Dec 18 '17 at 10:38
  • I have selected it to "Copy if Newer" and its not working properly. the records are still null in db but the count of records is working. Like, the database is null but when i start a linq query on this table the max of primary key is displayed but the records i.e FirstName and LastName are still Null @Sinatr – Zerry Mirza Dec 18 '17 at 11:02

0 Answers0