Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hello everyone
My framework:
- Microsofr window7
- Visual studio 2008
I used AdventureWorks database sample for my application.
I created window form application include:
- 1 datagridview: name = datagridview1
- 1 button: name=button1
I used visual studio 2008's tool to add Datasource (Data>Add new data source...)
I only selected AWBuildVersion table (which within AdventureWorks database)
Now I had:
- AdventureWorks_DataDataSet
My purpose is press 'button1' will create new AWBuildVersionRow, it was display to datagridview1 and then save it to AdventureWorks database permanently
My code:

C#
byte i = 1;
 private void button1_Click(object sender, EventArgs e)
 {

 AdventureWorks_DataDataSetTableAdapters.AWBuildVersionTableAdapter da = new WindowsFormsApplication2.AdventureWorks_DataDataSetTableAdapters.AWBuildVersionTableAdapte();
 AdventureWorks_DataDataSet.AWBuildVersionDataTable table = da.GetData();
 AdventureWorks_DataDataSet.AWBuildVersionRow row = table.NewAWBuildVersionRow();
 row.BeginEdit();
 row.Database_Version = "version";
 row.SystemInformationID = ++i;
 row.VersionDate = DateTime.Now;
 row.ModifiedDate = DateTime.Now;
 row.EndEdit();
 table.AddAWBuildVersionRow(row);
 da.Update(table);      //save changes to database
             this.aWBuildVersionTableAdapter.Fill(this.adventureWorks_DataDataSet.AWBuildVersion);
 }


I ran DEBUG. The result is:
when I press 'button1'. New row was displayed on datagridview1. Then stop DEBUG
A moment later, I run DEBUG. Above created new row still was displayed.
But if I stop DEBUG. And a long time later I run DEBUG, Above created new row was not displayed.
I can't understand why is that.
please help me! thank you very much
Posted
Comments
bowlturner 8-Jul-14 14:01pm    
I've seen answers saying you need row.AcceptChanges() and some saying you don't. Give a try?
rocket42 8-Jul-14 21:34pm    
yeah, thank bowlturner
I tried to add row.AcceptChanges(). But it is still so
jasonHall 8-Jul-14 16:58pm    
Is it possible that during the DataSource Configuration Wizard that you checked the box "Enable local database caching" and somehow the development environment is retaining a cached copy of the data before your insert? Then later on it reloads that cached data? I've tried your code and it is certainly writing to SQL Server correctly, then updating the DataGridView...
Maciej Los 8-Jul-14 17:03pm    
My virtual 5!
rocket42 8-Jul-14 21:36pm    
thank jason Hall
you are right.
But if not cache database. How to package sofware?

for update the ds in database you need to set CommandBuilder and set InsertCommand, UpdateCommand, or DeleteCommand .
pl. check below link
http://support.microsoft.com/kb/308507
 
Share this answer
 
Is it possible that during the DataSource Configuration Wizard that you checked the box "Enable local database caching" and somehow the development environment is retaining a cached copy of the data before your insert? Then later on it reloads that cached data? I've tried your code and it is certainly writing to SQL Server correctly, then updating the DataGridView...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900