Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGridView

Status
Not open for further replies.

dBjason

Programmer
Mar 25, 2005
355
US
Hello,

I have a simple vb.net project with a form, dbSource, DataSet, BindingSource and TableAdapter. On the form there is a DataGridView, that links to a table in SQL Server (all of these are controls, dropped onto the form from the Data Toolbox).

When I run the form the DataGridView is populated with data, looks great.

The problem is when I try to change a value in the DataGridView, it won't update SQL Server. I can make a change, but it's not reflected in the database. When I close the form and run it again, my changes are lost.

I'm new to VB.NET (used to VB6), so any help here is greatly appreciated!

Thanks,
Jason
 
...Still not working.

I've added a 'Save' button with the following code:

Me.Validate()
Me.StgCustomerBindingSource.EndEdit() Me.Stg_CustomerTableAdapter.Update(Me.AdventureWorksDataSet.stg_Customer)


But it bombs. Keep getting an InvalidOperationException:
Update requires a valid UpdateCommand when passed DataRow collection with modified rows.


Any help on this would be EXTREMELY appreciated. This should be a simple project, a bound and updatable from a simple table in SQL Server.

Thanks!
-Jason
 
I encountered a very similar issue. Please review my thread to see if it can help you.
thread796-1616622

If at first you don't succeed, then sky diving wasn't meant for you!
 
Bluejay, thanks for the help. Initially I didn't find an answer in the post -- but something in it steered me towards a solution.

Steps I took to make it work:

1. Delete the Datagridview, Dataset, BindingSource and TableAdapter
2. Go into SSMS and ADD A PRIMARY KEY!!! (Won't work without one)
3. Recreate the DataGridView (I let .NET create the BindingSource stuff)
4. Enter the following code into my 'Save' button click event:

Me.Validate()
Me.StgCustomerBindingSource.EndEdit()
Me.Stg_CustomerTableAdapter.Update(Me.AdventureWorksDataSet.Tables("stg_Customer"))


Works great now, hope this post can help someone else!

BTW I owe Tek-Tips a huge amount of thanks over the years, this site and everyone in it have always come through. Thanks a million!
 
dBjason,

Coming from VB6, it's important to understand how data access is different in .Net. ADO.Net is a disconnected model -- data is fetched from the database, consumed and manipulated in memory on the client machine running the application, and optionally pushed back to the database server. So you end up with commands to perform four types of operations: Select, Insert, Update and Delete. Over the years, there have been wrappers and wizards and helper objects to kind of put some of this stuff behind the scenes. These would be things like TableAdapters and CommandBuilders.

If you want to become proficient at .Net, I would recommend learning data access in it's most basic form in .Net: DataSets, DataAdapters, and Select, Insert, Update and Delete commands. Code your command objects by hand to get a true understanding of ADO.Net.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top