I have a table (tblArticleGroup) with a automatic increasing primary key (ArticleGroupID). Im using a datatable to insert a new row in the table. Then I want to know the generated ArticleGroupID, but I havent found a solution.
Im using SQL Server 7.0
I checked on google and found a good example in C##. I translated it to vb.net (see below), but it doesnt work, because ArticleGroupID = 0 all the time. Can anyone see whats wrong with the code:
Dim conn As New SqlConnection(myConnectionString)
Dim da As New SqlClient.SqlDataAdapter("Select * from tblArticleGroup", conn)
da.MissingSchemaAction = MissingSchemaAction.AddWithKey
Dim builder As New SqlClient.SqlCommandBuilder(da)
Dim Command As SqlCommand
Command = builder.GetInsertCommand()
Command.CommandText += " ; select @@IDENTITY as ArticleGroupID"
Dim dt As New DataTable()
da.FillSchema(dt, SchemaType.Source)
conn.Open()
dt.Clear()
Dim row As DataRow
row = dt.NewRow()
row("ChargeCodeSearchStr") = ""
row("Name") = "Testgroup"
row("IsTemporary") = True
dt.Rows.Add(row)
da.Update(dt)
dt.AcceptChanges()
MsgBox(row("ArticleGroupID")) This returns "0" all the time!!!
conn.Close()
Im using SQL Server 7.0
I checked on google and found a good example in C##. I translated it to vb.net (see below), but it doesnt work, because ArticleGroupID = 0 all the time. Can anyone see whats wrong with the code:
Dim conn As New SqlConnection(myConnectionString)
Dim da As New SqlClient.SqlDataAdapter("Select * from tblArticleGroup", conn)
da.MissingSchemaAction = MissingSchemaAction.AddWithKey
Dim builder As New SqlClient.SqlCommandBuilder(da)
Dim Command As SqlCommand
Command = builder.GetInsertCommand()
Command.CommandText += " ; select @@IDENTITY as ArticleGroupID"
Dim dt As New DataTable()
da.FillSchema(dt, SchemaType.Source)
conn.Open()
dt.Clear()
Dim row As DataRow
row = dt.NewRow()
row("ChargeCodeSearchStr") = ""
row("Name") = "Testgroup"
row("IsTemporary") = True
dt.Rows.Add(row)
da.Update(dt)
dt.AcceptChanges()
MsgBox(row("ArticleGroupID")) This returns "0" all the time!!!
conn.Close()