DeniseGoodheart
asked on
Get SQL Server SCOPE_IDENTITY Using ADO.NET Command Parameter
Hi,
I’m using ASP.NET 2005. The following code works using a stored procedure and the SqlDataReader (my Rdr.Read), but I would like to replace the following code with the ADO.NET command parameter to get the SCOPE_IDENTITY:
'Get Identity ID
If Rdr.Read Then
Me.lblStatus.Text = "Created Record ID: " & Rdr(0)
End If
I tried the following with no success to get the SCOPE_IDENTITY:
cmd.Parameters.Add("SCOPE_ IDENTITY", Data.SqlDbType.Int)
cmd.Parameters(0).Directio n = Data.ParameterDirection.Re turnValue
My Stored Pocedure is as follows:
CREATE PROCEDURE [dbo].[spInsTestSite]
@fknGeneratorID [numeric]
AS
INSERT INTO .dbo.aTest
( [GeneratorID])
VALUES
( @fknGeneratorID)
SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]
********My entire VB.NET code is as follows:
Private Sub ExecStoredProc()
Dim sSQL As String
'Set up sql string and connection
sSQL = "spInsTestSite"
'Use clsDatabase class for connection string
sCon = oCon.SqlConString
Dim Con As SqlConnection = New SqlConnection(sCon)
Dim cmd As New SqlCommand
With cmd
'Create connection object
.Connection = New SqlConnection(sCon)
'Set the SQL
.CommandText = sSQL
.CommandType = Data.CommandType.StoredPro cedure
End With
cmd.Parameters.Add("@fknGe neratorID" , Data.SqlDbType.Int)
cmd.Parameters("@fknGenera torID").Va lue = Me.ddlGenerator.SelectedVa lue
cmd.Connection.Open()
Dim Rdr As SqlDataReader = cmd.ExecuteReader()
'Get Identity ID
If Rdr.Read Then
Me.lblStatus.Text = "Created Record ID: " & Rdr(0)
End If
cmd.Connection.Close()
End Sub
Thanks,
Denise
I’m using ASP.NET 2005. The following code works using a stored procedure and the SqlDataReader (my Rdr.Read), but I would like to replace the following code with the ADO.NET command parameter to get the SCOPE_IDENTITY:
'Get Identity ID
If Rdr.Read Then
Me.lblStatus.Text = "Created Record ID: " & Rdr(0)
End If
I tried the following with no success to get the SCOPE_IDENTITY:
cmd.Parameters.Add("SCOPE_
cmd.Parameters(0).Directio
My Stored Pocedure is as follows:
CREATE PROCEDURE [dbo].[spInsTestSite]
@fknGeneratorID [numeric]
AS
INSERT INTO .dbo.aTest
( [GeneratorID])
VALUES
( @fknGeneratorID)
SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]
********My entire VB.NET code is as follows:
Private Sub ExecStoredProc()
Dim sSQL As String
'Set up sql string and connection
sSQL = "spInsTestSite"
'Use clsDatabase class for connection string
sCon = oCon.SqlConString
Dim Con As SqlConnection = New SqlConnection(sCon)
Dim cmd As New SqlCommand
With cmd
'Create connection object
.Connection = New SqlConnection(sCon)
'Set the SQL
.CommandText = sSQL
.CommandType = Data.CommandType.StoredPro
End With
cmd.Parameters.Add("@fknGe
cmd.Parameters("@fknGenera
cmd.Connection.Open()
Dim Rdr As SqlDataReader = cmd.ExecuteReader()
'Get Identity ID
If Rdr.Read Then
Me.lblStatus.Text = "Created Record ID: " & Rdr(0)
End If
cmd.Connection.Close()
End Sub
Thanks,
Denise
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is another one that uses SCOPE_IDENTITY
http://davidhayden.com/blog/dave/archive/2006/02/16/2803.aspx
http://davidhayden.com/blog/dave/archive/2006/02/16/2803.aspx
ASKER
Hi naveenkohli,
Thanks for your reply. My code currently uses the SqlDataReader Read method to get the SCOPE_IDENTITY,
and so does the example at davidhayden.com. I tried experimenting with the ExecuteScaler with no success. I've been
searching for a good example on the Internet that uses the Command Parameter and ExecuteScaler methods with
no success.
Thanks,
Denise
Thanks for your reply. My code currently uses the SqlDataReader Read method to get the SCOPE_IDENTITY,
and so does the example at davidhayden.com. I tried experimenting with the ExecuteScaler with no success. I've been
searching for a good example on the Internet that uses the Command Parameter and ExecuteScaler methods with
no success.
Thanks,
Denise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi All,
Thanks for the help. I couldn’t get it to work using the ExecuteScalar method, but I was able to get it to work using the ExecuteReader method and by using mmarksbury suggestion, and by finding a good example at http://aspnet.4guysfromrolla.com/articles/062905-1.aspx
My modified code is as follows:
'1
cmd.Parameters.Add("@fknGe neratorID" , Data.SqlDbType.Int)
'Get value from drop down list control
cmd.Parameters("@fknGenera torID").Va lue = Me.ddlGenerator.SelectedVa lue
'2
Dim retValParam As New SqlParameter("@RETURN_VALU E", Data.SqlDbType.Int)
retValParam.Direction = Data.ParameterDirection.Re turnValue
cmd.Parameters.Add(retValP aram)
cmd.Connection.Open()
Dim Rdr As SqlDataReader = cmd.ExecuteReader()
Me.lblStatus.Text = Convert.ToInt32(retValPara m.Value)
cmd.Connection.Close()
I modified my stored procedure to the following:
ALTER PROCEDURE [dbo].[spInsTestSite]
@fknGeneratorID [numeric]
AS
INSERT INTO .dbo.aTest
( [GeneratorID])
VALUES
( @fknGeneratorID)
Return SCOPE_IDENTITY()
Many Thanks,
Denise
Thanks for the help. I couldn’t get it to work using the ExecuteScalar method, but I was able to get it to work using the ExecuteReader method and by using mmarksbury suggestion, and by finding a good example at http://aspnet.4guysfromrolla.com/articles/062905-1.aspx
My modified code is as follows:
'1
cmd.Parameters.Add("@fknGe
'Get value from drop down list control
cmd.Parameters("@fknGenera
'2
Dim retValParam As New SqlParameter("@RETURN_VALU
retValParam.Direction = Data.ParameterDirection.Re
cmd.Parameters.Add(retValP
cmd.Connection.Open()
Dim Rdr As SqlDataReader = cmd.ExecuteReader()
Me.lblStatus.Text = Convert.ToInt32(retValPara
cmd.Connection.Close()
I modified my stored procedure to the following:
ALTER PROCEDURE [dbo].[spInsTestSite]
@fknGeneratorID [numeric]
AS
INSERT INTO .dbo.aTest
( [GeneratorID])
VALUES
( @fknGeneratorID)
Return SCOPE_IDENTITY()
Many Thanks,
Denise
ASKER
Will you please provide me with an example?
Thanks,
Denise