Hi Justin,
You'll want to use ExecuteScalar() as shown in this article about one third
of the way down:
http://dotnetadvisor.net/doc/13279
Here's the relevant VB code:
<%@ import Namespace="Syst em.Data" %>
<%@ import Namespace="Syst em.Data.SqlClie nt" %>
<html>
<script language="VB" runat="server">
Sub Page_Load(Sourc e as Object, E as EventArgs)
Dim dbConn As SqlConnection
' Create a new connection object pointing to
the database
dbConn = New SqlConnection(" server=.;uid=sa ;pwd=
;database=North wind")
Dim dbComm As New SqlCommand()
dbComm.Connecti on = dbConn
dbComm.CommandT ype = CommandType.Tex t
dbComm.CommandT ext = "INSERT INTO Categories
(CategoryName, Description) " & _
"VALUES ('Guitars','Her e you can find just the " & _
"guitar you looked for'); SELECT @@IDENTITY As 'Identity'"
Dim iID As Integer
Try
dbConn.Open()
iID = dbComm.ExecuteS calar()
Catch ex As Exception
Response.Write( ex.Message)
Response.End
Finally
If dbConn.State = ConnectionState .Open Then
dbConn.Close()
End If
End Try
Response.Write( "The ID of the new record is: "
& iID.ToString())
End Sub
</script>
</html>
"Justin" wrote:
Using SQL server and VS.NET with ASP.NET/C# how can I retrieve the Key ID of
a record right after creating that record in the same event? Do I need to
close and reopen the connection and requery?
Thanks, Justin.