1

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);  
}
Omni
  • 31
  • 1
  • 6
  • Looks like you insert a record then overwrite it by calling Fill (didnt use adapters for long time though). Does it work when 2nd line is commented? – LINQ2Vodka Nov 09 '13 at 06:48
  • It does not overwrite the data, rather if I comment Fill, I don't even see the effect on runtime in dataGridView either. therefore, it dosn't work when it is commented. – Omni Nov 09 '13 at 06:55
  • If you perform Insert and then AcceptChanges, it *will* insert a new record to the database (since InsertCommand is defined). Probable GridView didnt reflect changes, need to update it or whatever. – LINQ2Vodka Nov 09 '13 at 07:01

2 Answers2

2

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

Community
  • 1
  • 1
Omni
  • 31
  • 1
  • 6
0

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."

LINQ2Vodka
  • 2,996
  • 2
  • 27
  • 47
  • Setting ta.ClearBeforeFill = false; before Inserting, has same effect, nothing changed. Although I read on the link you posted that I have to set it to false – Omni Nov 09 '13 at 07:03
  • Then check real database data. – LINQ2Vodka Nov 09 '13 at 07:07
  • As I mentioned in my question, The real database data stays the same, This code is executed on click of a button and the result is runtime only i.e. The data being inserted can be seen while in runtime but, it isn't persisting, or rather it isn't being commited to database for "some" reasons. – Omni Nov 09 '13 at 07:11
  • 1. Is there any explicit parent transaction? 2. Is connection string correct? 3. Is InsertCommand correct? – LINQ2Vodka Nov 09 '13 at 07:14
  • 1.No parent Transactions, this is sole "query" I havn't even started creating the real application yet, Just making sure DB was working. 2.As the database, Dataset and TableAdapter was added through VS, VS created and wrote the connection string in App.Config. Additionally, I can view the data from database in dataGridView, dosn't that mean the connection works? 3.Umm, About the InsertCommand, I am using Insert method pre-created by TableAdapter, I didn't write any insert command, Although the parameters the method Insert is asking is perfect. – Omni Nov 09 '13 at 07:20
  • @Omni huh i think ive googled the answer: "When you call AcceptChanges, the original data is overwritten with the current data and all RowState properties are set to Unchanged." So RowStates are Unchanged when you call Update. Try to comment AcceptChanges line. – LINQ2Vodka Nov 09 '13 at 07:34
  • I currently have code akin to, ta.ClearBeforeFill = false; ta.Insert("foo", "bar", 2, "zing", "tada");ta.Update(rds.reminders); yet the database isn't updated with new information, I am sorry for causing you so much trouble, but I thank you for your time and effort of searching an answer :) – Omni Nov 09 '13 at 07:55