469,903 Members | 1,374 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

Retrieving Identity After Dataset update

I have created a dataset with two tables and an insert command, I need to be
able to retreive the Key Identity after inserting into table "A" for use in
table "B".

Should I use ExecuteScalar() or is there a better solution?

Thanks, Justin.
Nov 18 '05 #1
3 9421
Hi Justin,

I think you want to use ADO.NET's InsertCommand for this.

You might want to check this:

http://msdn.microsoft.com/library/de...anidcrisis.asp

Retrieving Identity or Autonumber Values

http://msdn.microsoft.com/library/de...mberValues.asp
"The following code example shows how to return the auto-incremented value
as the output parameter and specify it as the source value for the
CategoryID column in the DataSet.

[Visual Basic]
Dim nwindConn As SqlConnection = New SqlConnection("Data
Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind")

Dim catDA As SqlDataAdapter = New SqlDataAdapter("SELECT CategoryID,
CategoryName FROM Categories", nwindConn)

catDA.InsertCommand = New SqlCommand("InsertCategory", nwindConn)
catDA.InsertCommand.CommandType = CommandType.StoredProcedure

catDA.InsertCommand.Parameters.Add("@CategoryName" , SqlDbType.NChar, 15,
"CategoryName")

Dim myParm As SqlParameter = catDA.InsertCommand.Parameters.Add("@Identity",
SqlDbType.Int, 0, "CategoryID")
myParm.Direction = ParameterDirection.Output

nwindConn.Open()

Dim catDS As DataSet = New DataSet
catDA.Fill(catDS, "Categories")

Dim newRow As DataRow = catDS.Tables("Categories").NewRow()
newRow("CategoryName") = "New Category"
catDS.Tables("Categories").Rows.Add(newRow)

catDA.Update(catDS, "Categories")

nwindConn.Close()
"Justin" <Ju****@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
I have created a dataset with two tables and an insert command, I need to
be
able to retreive the Key Identity after inserting into table "A" for use
in
table "B".

Should I use ExecuteScalar() or is there a better solution?

Thanks, Justin.


Nov 18 '05 #2
I have tried that code, what I can't figure out is exactly what variable
holds the identity. If its "myParam" then how do I cast it to an int variable?

Thanks, Justin.

"Ken Cox [Microsoft MVP]" wrote:
Hi Justin,

I think you want to use ADO.NET's InsertCommand for this.

You might want to check this:

http://msdn.microsoft.com/library/de...anidcrisis.asp

Retrieving Identity or Autonumber Values

http://msdn.microsoft.com/library/de...mberValues.asp
"The following code example shows how to return the auto-incremented value
as the output parameter and specify it as the source value for the
CategoryID column in the DataSet.

[Visual Basic]
Dim nwindConn As SqlConnection = New SqlConnection("Data
Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind")

Dim catDA As SqlDataAdapter = New SqlDataAdapter("SELECT CategoryID,
CategoryName FROM Categories", nwindConn)

catDA.InsertCommand = New SqlCommand("InsertCategory", nwindConn)
catDA.InsertCommand.CommandType = CommandType.StoredProcedure

catDA.InsertCommand.Parameters.Add("@CategoryName" , SqlDbType.NChar, 15,
"CategoryName")

Dim myParm As SqlParameter = catDA.InsertCommand.Parameters.Add("@Identity",
SqlDbType.Int, 0, "CategoryID")
myParm.Direction = ParameterDirection.Output

nwindConn.Open()

Dim catDS As DataSet = New DataSet
catDA.Fill(catDS, "Categories")

Dim newRow As DataRow = catDS.Tables("Categories").NewRow()
newRow("CategoryName") = "New Category"
catDS.Tables("Categories").Rows.Add(newRow)

catDA.Update(catDS, "Categories")

nwindConn.Close()
"Justin" <Ju****@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
I have created a dataset with two tables and an insert command, I need to
be
able to retreive the Key Identity after inserting into table "A" for use
in
table "B".

Should I use ExecuteScalar() or is there a better solution?

Thanks, Justin.


Nov 18 '05 #3
The solution is to set up cascade update in your dataset so that when the
new Identity value is returned from the database you update the master table
and cascade the change to the child.
You have to trap the RowUpdated event and then post the new identity value.

For SQL Server, there is a way to send 2 statements separated by a
semi-colon.
The first is your Parent update statement the second is Select
@@Scope_Identity.

For Access and Oracle (and SQL Server if you don't use multiple statements):
You have to trap the RowUpdated event and then post the new identity value.

================================================== ==============
In my update method I have some code like this:

'handle the RowUpdated event to get the Identity value back from
SQL Server
'w/o the real Identity value, the child records won't be added to
SQL Server.
AddHandler da_Hdr.RowUpdated, AddressOf da_Handle_RowUpdated

'parent table
da_Hdr.Update(NewHdrRecords)

'child table
da_Ln.Update(NewLnRecords)
================================================== ==============
'this is how to handle the insert of each row:

Private Sub da_Handle_RowUpdated(ByVal sender As Object, ByVal e As
SqlRowUpdatedEventArgs)
If e.Status = UpdateStatus.Continue And e.StatementType
=StatementType.Insert Then
e.Row("key") = GetIdentity(e.Command.Connection)
e.Row.AcceptChanges()

'use this if you do not want to AcceptChanges for each row.
'e.Status = UpdateStatus.SkipCurrentRow
End If
End Sub
================================================== ==============
Private Function GetIdentity(ByRef cnn As SqlConnection) As Integer
Dim oCmd As New SqlCommand("SELECT @@IDENTITY", cnn)
Dim x As Object = oCmd.ExecuteScalar()
Return CInt(x)
End Function
================================================== ==============

For Oracle - I use this code:
================================================== ==============

Private Sub da_Handle_OracleRowUpdated(ByVal sender As Object, ByVal e As
OracleRowUpdatedEventArgs)
If e.Status = UpdateStatus.Continue And e.StatementType
=StatementType.Insert Then
e.Row("key") = GetOracleSequence(e.Command.Connection)
e.Row.AcceptChanges()

'use this if you do not want to AcceptChanges for each row.
'e.Status = UpdateStatus.SkipCurrentRow
End If
End Sub

Private Function GetOracleSequence(ByRef cnn As OracleConnection) As
Integer
Dim oCmd As New OracleCommand("SELECT SEQ_EIMHDR_EIMKEY.CURRVAL FROM
DUAL", cnn)
Dim x As Object = oCmd.ExecuteScalar()
Return CInt(x)
End Function
================================================== ==============

--
Joe Fallon


"Justin" <Ju****@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
I have created a dataset with two tables and an insert command, I need to
be
able to retreive the Key Identity after inserting into table "A" for use
in
table "B".

Should I use ExecuteScalar() or is there a better solution?

Thanks, Justin.

Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mojtaba Faridzad | last post: by
9 posts views Thread by Kevin Hodgson | last post: by
4 posts views Thread by Mark Olbert | last post: by
15 posts views Thread by gunnar.sigurjonsson | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.