Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
FillSchema and Identity column
Message
General information
Forum:
ASP.NET
Category:
ADO.NET
Miscellaneous
Thread ID:
00740181
Message ID:
00741302
Views:
24
Don,

I would check your UpdateCommand text to see what is being issued with the row is updated. The command object could be stringing multiple commands together which is updating the columns in the row.

>Hi Cathy,
>Thank you. This works! In my OnRowUpdated() routine I reset the primary key Readonly property to False and it does allow me to store the returned @@Identity value with this code, at least in the datarow passed to the OnRowUpdated routine as an argument. However, on return from the OnRowUpdated() routine the original row values are unchanged, and I have been unable to figure out how to persist the new value to the original data table. I'm still all thumbs with the techniques and terminology.
>
>Here are the main lines of code I am using:
>*****************************************************
>' Code in calling procedure
>AddHandler DataAdapter.RowUpdated, New SqlRowUpdatedEventHandler(AddressOf OnRowUpdated)
>
>RowsUpdated = DataAdapter.Update(ds_changes, ds.Tables(0).ToString())
>...
>ds.AcceptChanges()
>
>' end calling procedure
>
>
>Public Sub OnRowUpdated(ByVal sender As Object, ByVal args As SqlRowUpdatedEventArgs)
>... if insert, get @@IDENTITY from server and store in newID variable
>... I then get the Primary Key field name, then:
>
> args.Row.Table.Columns(PkFieldName).ReadOnly = False
> args.Row(PkFieldName) = newID
>
>End Sub
>*****************************************************
>
>The args.Row(PkFieldName) value is correct in OnRowUpdated() when I check it, but not on exit from the routine. How do I persist it there, and get through the final ds.AcceptChanges() ?
>
>Any advice or links or article references would be most appreciated! I have spent a number of hours on this since implementing your last advice. But I can see that this technique will work as a "black box" routine for all tables that use a single field PK, so I think it is worth the effort to develop.
>
>Don Meuse
>
>
>
>>Hi Don,
>>
>>After you retrieve the schema information, you can "tweak" some properties it set for your DataColumns. There are properties that are set for autoincrementing (called AutoIncrement) and read-only (called ReadOnly). For your primary key, just reset each of these to false.
>>
>>>I'm writing a generic WinForms routine to insert a record, then return the @@identity value and store it back to the current row, so I can then add child records with the correct parent PK.
>>>
>>>I have an OnRowUpdated procedure that figures out the Primary Key for the current datatable using the "DataTable.PrimaryKey" property.
>>>
>>>To do this I had to retrieve the datatable schema using this command:
>>>
>>> DataAdapter.MissingSchemaAction() = MissingSchemaAction.AddWithKey
>>>
>>>Now I indeed can figure out Primary Key field for the current table. But now I am getting a "Field is read-only" when I try to store the returned @@IDENTITY value back into the table. Before using the MissingSchemaAction I could store a value but couldn't determine the PK field! Kind of a catch-22 so far!
>>>
>>>Does anyone know the way to return and store an @@identity value for a table using a generic routine that figures out the PK field programmatically? I'm trying to avoid hard-coding the insert for every table.
>>>
>>>Note: It's interesting that with "MissingSchemaAction.AddWithKey" in effect I DO get a local auto-incrementing field value now, but it's NOT in synch with the server's current value, so the functionality appears useless. In other words, if other user's add records to the server while I'm working locally my local "autoincrement" fields are duplicate IDs, so I can't use the values to create child records. So I do the "SELECT @@IDENTITY" in my OnRowUpdated to retrieve the server's value.
>>>
>>>Any advice is appreciated!
>>>
>>>TIA
>>>Don Meuse
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Previous
Reply
Map
View

Click here to load this message in the networking platform