Question

I created a service-based local db via Visual Studio 2013 Express edition.. The connection string, dataset and TableAdapter were added automatically.

On click of a button, I am trying to insert some data by calling TableAdapter.Insert As it is I already have a dataGridView's datasource bound to the dataset, So I immediately see that the data was inserted in table properly at run time, But when I close the application, The actual DB dosn't contain the data. Therefore, The data isn't actually inserted in DB.

According to http://msdn.microsoft.com/en-us/library/ms233812%28v=vs.110%29.aspx

With insert, you have to only call insert, yet I am calling Update and AcceptChanges on table, for safety, well I tried the first way shown in link (i.e. creating a row and adding it to dataset then calling update) as well, but it seems the data isn't being inserted in DB at all.

Finally, the insert code, rds is DataSet and ta is TableAdapter

private void AddBtn_Click(object sender, EventArgs e)
{
     ta.Insert("foo", "bar", 2, "zing", "tada");
     ta.Fill(rds.reminders);
     rds.reminders.AcceptChanges();
     ta.Update(rds.reminders);  
}
Was it helpful?

Solution

It turned out that, as I was using the VS compiled application each time the mdf database was being overwritten, hence the changes I made were completely erased,

The possible solution could be one of following

1)Change the connectionstring to point to database that is in Debug folder, which wont be overwritten each time you compile and run the application

2)You could simply let the connectionstring be as it is and just test it through detached compiler mode.

I was able to figure this out due to the following stackoverflow link I suddenly sumbled upon after 2 days.

Database changes do not persist after ObjectContext.SaveChanges() is called

OTHER TIPS

ClearBeforeFill Property
http://msdn.microsoft.com/en-us/library/bz9tthwx.aspx

"By default, every time you execute a query to fill a TableAdapter's data table, the data is cleared and only the results of the query are loaded into the table."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top