0

I need help with my project please. I am using a service based database in visual studio, When I am adding a row using dataset and sqldataAdapter, the row is added and then I load the content of the database using another method, it shows me the old data of the database and the new row I inserted. However when the application is closed, the database don't update, thank you for your replies:

This the code of inserting a row in the database

string sConnectionString;
            sConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\TrainedFacesDB.mdf;Integrated Security=True;User Instance=True";

            SqlConnection objConn = new SqlConnection(sConnectionString);


            SqlDataAdapter daDistances = new SqlDataAdapter("Select * From TrainedFacesDistances", objConn);

            DataSet dsDistances = new DataSet();


            daDistances.FillSchema(dsDistances, SchemaType.Source, "TrainedFacesDistances");
            daDistances.Fill(dsDistances,"TrainedFacesDistances");


            DataRow newDistance = dsDistances.Tables["TrainedFacesDistances"].NewRow();

            newDistance["Name"] = "Unknown";
            newDistance["EyesDistance"] = ed;
            newDistance["EyesCornerDistance"] = ecd;
            newDistance["NoseWidth"] = nw;


            dsDistances.Tables["TrainedFacesDistances"].Rows.Add(newDistance);


            SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(daDistances);

            daDistances.UpdateCommand = cmdBuilder.GetUpdateCommand();
            daDistances.InsertCommand = cmdBuilder.GetInsertCommand();

            daDistances.Update(dsDistances, "TrainedFacesDistances");



            objConn.Close();
Dhaval Patel
  • 7,471
  • 6
  • 37
  • 70
OusGh
  • 23
  • 1
  • 4
  • possible duplicate of [DataAdapter.Update() does not Update the Database](http://stackoverflow.com/questions/6833277/dataadapter-update-does-not-update-the-database) – Arseni Mourzenko May 29 '14 at 11:09
  • Does your table have a primary key ??? I've had issues when using standard update command if the table was lacking a key. It will run but then not show any error. – Dhaval Patel May 29 '14 at 11:12

1 Answers1

0

Try changing this to

daDistances.UpdateCommand = cmdBuilder.GetUpdateCommand();
daDistances.InsertCommand = cmdBuilder.GetInsertCommand();

to this

daDistances.UpdateCommand = cmdBuilder.GetUpdateCommand(true);
daDistances.InsertCommand = cmdBuilder.GetInsertCommand(true);
Sid M
  • 4,354
  • 4
  • 30
  • 50