Link to home
Start Free TrialLog in
Avatar of DeniseGoodheart
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).Direction = Data.ParameterDirection.ReturnValue

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.StoredProcedure
        End With

        cmd.Parameters.Add("@fknGeneratorID", Data.SqlDbType.Int)
        cmd.Parameters("@fknGeneratorID").Value = Me.ddlGenerator.SelectedValue
       
        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
Avatar of naveenkohli
naveenkohli

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DeniseGoodheart
DeniseGoodheart

ASKER

Hi naveenkohli,

Will you please provide me with an example?

Thanks,
Denise
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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("@fknGeneratorID", Data.SqlDbType.Int)
        'Get value from drop down list control
        cmd.Parameters("@fknGeneratorID").Value = Me.ddlGenerator.SelectedValue
        '2
        Dim retValParam As New SqlParameter("@RETURN_VALUE", Data.SqlDbType.Int)
        retValParam.Direction = Data.ParameterDirection.ReturnValue
        cmd.Parameters.Add(retValParam)
        cmd.Connection.Open()

        Dim Rdr As SqlDataReader = cmd.ExecuteReader()
        Me.lblStatus.Text = Convert.ToInt32(retValParam.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