|
-
Dec 25th, 2009, 05:19 AM
#1
Thread Starter
New Member
Problem with datagrid identity column
I have a datagridview with contacts on a form with clients. The underlying table for contacts has a field for Contactnumber. This is a identity column so its new number is generated by SQL Server.
I add a row and when I move to the next record on the form, the form ánd the information in the datagrid is saved to the database. So far, so good. The problem is: when I move back and try to delete that newly made row I get an error about concurrency.
It's logical actually, the record is saved, but the dataset of the datagrid doesn't yet know what the new contactnumber is. Apparently it's not information that it gets back automatically from SQL Server.
How can I make the dataset know the new identiy of the row that is saved to the database? I use a simple da.Update(dsVerwijderingen, Tabelnaam) to save the changes to the database.
-
Dec 29th, 2009, 08:00 AM
#2
Addicted Member
Re: Problem with datagrid identity column
First, you must to said what sql sentence you are to using.
I know that is possible specify when you are using SqlCommand parameters, that a parameter to be input/output. This meaning that when you execute an update, the parameter is saved and then read it from the database back to the datatable asociated.
In the case of identity column maybe the parameter could be output only because its value is get after the update is executed.
-
Dec 29th, 2009, 11:50 AM
#3
Re: Problem with datagrid identity column
Your InsertCommand has to include a query to retrieve that data, e.g.
SQL Code:
INSERT INTO Person (FirstName, LastName) VALUES (@FirstName, @LastName); SELECT PersonID = SCOPE_IDENTITY()
Now the PersonID column will be refreshed with the last generated ID when a record is inserted.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|