Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to Return Id of the Record from Stored Proc Using ASP.NET and SQL Server 2005

Posted on 2008-10-15
6
Medium Priority
?
237 Views
Last Modified: 2010-08-05
Helo Experts,

I am adding customer record to the database using the stored procedure below

CREATE PROCEDURE [dbo].[procAddRegistrationDetails]
      -- Parameters
      @GiftCodeID CHAR(16),
      @CertRegTitle VARCHAR(10),
      ...
        ....
      @UserTypeID      CHAR(10)
AS

BEGIN
      DECLARE @CustID      INT
      DECLARE @RowID      INT

      INSERT INTO Customer(CustTitle, CustFirstName, CustLastName, CustAddr1, CustAddr2, CustAddr3, CustCity, CustCounty, CustPostCode, CustCountryID, CustEmail, CustPhone)
      VALUES (@CustTitle, @CustFirstName, @CustLastName, @CustAddr1, @CustAddr2, @CustAddr3, @CustCity, @CustCounty, @CustPostCode, @CustCountryID, @CustEmail, @CustPhone)

      DECLARE cur_CustID CURSOR STATIC FOR
      SELECT SCOPE_IDENTITY() AS CustID

      OPEN cur_CustID
      FETCH NEXT FROM cur_CustID INTO @CustID

      CLOSE cur_CustID
      DEALLOCATE cur_CustID;

    -- Update the GiftCertificate with the Registration details
      DECLARE @StatusID      CHAR(30)
      DECLARE @GiftType      CHAR(2)

      SET @GiftType = LEFT(@GiftCodeID, 2)
               
    --       UPDATE FINISH                  
      
END

and submitting data to the above SP from the ASP.Net page using below

      ' connection and necessary statements here
      cmd.CommandText = "procAddRegistrationDetails"
        cmd.CommandType = CommandType.StoredProcedure

         'set up which parameters to pass
          cmd.Parameters.Add("@GiftCodeID", SqlDbType.Char, 16)
                cmd.Parameters.Add("@UserID", SqlDbType.Char, 30)
                cmd.Parameters.Add("@UserTypeID", SqlDbType.Char, 10)
         ' ......
       cmd.ExecuteNonQuery()

All I wanted to do is to return the CustId from the SP to some variable

SessionData.CustomerID = cmd.ExecuteNonScalar ?????? I tried this but did not work.

Please can someone advise me here?

Many thanks for your help

Regards
Kay            
0
Comment
Question by:learningnet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 1200 total points
ID: 22719487
add to your create procecdure the following parameter:
@UserTypeID      CHAR(10),
@custId int output

in your code replace the cursor part with this: (you don't declare @custId in your procedure code)
replace
      DECLARE cur_CustID CURSOR STATIC FOR
      SELECT SCOPE_IDENTITY() AS CustID

      OPEN cur_CustID
      FETCH NEXT FROM cur_CustID INTO @CustID

      CLOSE cur_CustID
      DEALLOCATE cur_CustID;
with
set @custId = SCOPE_IDENTITY()

that's it

when you call your procedure add the @custId parameter and make sure you define it's direction as output
after the procedure call will return, you will have the value in the parameter and you can copy it to your variable (from the value attribute)
0
 
LVL 16

Assisted Solution

by:TSmooth
TSmooth earned 800 total points
ID: 22719518
Add another parameter called something like "@CustID" to your stored procedure and set it's direction to OUTPUT by using a line like the following:

"@CustID int OUTPUT"

and add a SET @CustID = Scope_Identity() in the stored procedure if you don't already have one.

Then in your asp.net code, add the parameter to your cmd object's parameters collection and set that parameter's "Direction" property to "Output". Now once you call cmd.ExecuteNonQuery(), you can check the value of the new output parameter afterwards:

Dim newID As Integer = Cint(cmd.Parameters("@CustID").Value)
0
 

Author Comment

by:learningnet
ID: 22720916

thanks both for your comments,

I have added the following in the Stored Procedure as CustID was already been used for something else

DECLARE @CustIdOut INT OUTPUT
SET @CustIdOut = SCOPE_IDENTITY()

Please can you advise how about adding and setting the output diretion on the command object


cmd.Parameters.Add("@CustIdOut", SqlDbType.Int, 100)

cmd.ExecuteNonQuery()
Dim newID As Integer = Cint(cmd.Parameters("@CustID").Value)

Thanks for your help

0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 16

Expert Comment

by:TSmooth
ID: 22720934
cmd.Parameters("@CustIdOut").Direction = ParameterDirection.Output
0
 

Author Comment

by:learningnet
ID: 22720935
I have used this but it gave me an error
cmd.Parameters("@CustIdOut").Direction = ParameterDirection.Output

is this not correct?

0
 

Author Comment

by:learningnet
ID: 22721075
great it works now !! little typo my bad...

thanks for your help !!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question