Results 1 to 7 of 7

Thread: Retrieve Unique ID for a new Record

  1. #1

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Resolved Retrieve Unique ID for a new Record

    I cannot figure out what is going wrong here.
    I get the error.
    PK_RequestID is not a parameter for procedure sp_InsertRequestID.
    Here is my SP

    Code:
    ALTER PROCEDURE sp_InsertRequestID
    
    	(	
    	@User varchar(50),
    	 @Request varchar(50),
    	@PK_RequestID int OUTPUT
    	)
    
    AS
    	Insert INTO TBL_Requests 
    	(LoginID, Request) 
    	Values 
    	(@User, @Request)
    	Select @PK_RequestID=@@Identity
    Return
    Here is my Code to call it
    Code:
    Dim intReturnID
            Me.SqlConnection1.Open()
            Me.SqlCommand1.Parameters.Item("@Request").Value = Me.TextBox1.Text
            Me.SqlCommand1.Parameters.Item("@User").Value = Session("MyID")
            Me.SqlCommand1.ExecuteNonQuery()
            intReturnID = SqlCommand1.Parameters("@PK_RequestID").Value
            Me.SqlConnection1.Close()
    And here is my declarations
    Code:
    Me.SqlCommand1.CommandText = "[sp_InsertRequestID]"
            Me.SqlCommand1.CommandType = System.Data.CommandType.StoredProcedure
            Me.SqlCommand1.Connection = Me.SqlConnection1
            Me.SqlCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("PK_RequestID", SqlDbType.Int, ParameterDirection.Output))
            Me.SqlCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Request", System.Data.SqlDbType.Text, 16, "LoginID"))
            Me.SqlCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@User", System.Data.SqlDbType.VarChar, 50, "UserName"))
    Last edited by FishGuy; May 5th, 2005 at 08:49 AM. Reason: Resolved

  2. #2
    Addicted Member
    Join Date
    Aug 2004
    Location
    Cape Town, South Africa
    Posts
    149

    Re: Retrieve Unique ID for a new Record

    VB Code:
    1. Me.SqlCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("PK_RequestID", SqlDbType.Int, ParameterDirection.Output))

    u left out an @, would that matter?

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Retrieve Unique ID for a new Record

    If it's not a parameter for the SP, why are you using it?

  4. #4

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Retrieve Unique ID for a new Record

    It is or at least should be a parameter for the SP. I tried it again with the @ and still get the similar error

    Procedure 'sp_InsertRequestID' expects parameter '@PK_RequestID', which was not supplied

    It should be the return value therefore an outpout parameter.. I think
    Last edited by FishGuy; May 5th, 2005 at 04:14 AM. Reason: more detail

  5. #5
    Addicted Member
    Join Date
    Nov 2004
    Posts
    149

    Re: Retrieve Unique ID for a new Record

    I retrieved the @@Identity a little differently when I needed the new uniqueID.

    Instead of trying to retrieve it back as a parameter I simply wrote:

    SELECT @@Identity

    Just make sure that where you execute the the command you are receiving the ID:

    intID = comSQLProc.ExecuteScalar

    You could try that.

  6. #6

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Retrieve Unique ID for a new Record

    Nice one token it worked can you believe ive been stuck on that all day, my original way was the one given in all the txt books and iv had it working in windows forms but not in asp. Any way your way is working fine for me thanks.

  7. #7
    Addicted Member
    Join Date
    Nov 2004
    Posts
    149

    Re: Retrieve Unique ID for a new Record

    Hey no problem. I , and I'm sure many of the others here, understand the frustrations of being stuck on one small thing all day.

    Just glad I could help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width