?
Solved

Returning Scope_Identity() in VB.NET

Posted on 2005-03-21
5
Medium Priority
?
923 Views
Last Modified: 2012-05-05
Hello,

I have the following SQL command in VB.NET to update a Table on my SQL 2000 server and then i wan to retrieve the key of the newly inserted row. I know this can be done either by @@IDENTITY or be Scope_Identity(). Though i don't know how to reference the specific value returned from the SELECT statement.

Dim cmd As New SqlCommand("insert into NewMail(MessageID,FromAddr,ReplyTo,MailTime,Subject,contentType,Encoding,Charset,Bound,AttachNum,AttachType,AttachFile,MailBody)
values (@MessageID,@FromAddr,@ReplyTo,@MailTime,@Subject,@contentType,@Encoding,@Charset,@Bound,@AttachNum,@AttachType,@AttachFile,@MailBody);
Select @Identity1=Scope_Identity()", conn)

How can i store the value returned by Scope_Identity() to a variable so i can use is later on.

Thank you.
0
Comment
Question by:Globemaster
[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
5 Comments
 
LVL 35

Accepted Solution

by:
YZlat earned 200 total points
ID: 13593601
CREATE pROCEDURE spAddNewMail
@MessageID,@FromAddr,
@ReplyTo,
@MailTime,
@Subject,
@contentType,
@Encoding,
@Charset,
@Bound,
@AttachNum,
@AttachType,
@AttachFile,
@MailBody,
@identity1 int OUTPUT

AS

insert into NewMail(MessageID,FromAddr,ReplyTo,MailTime,Subject,contentType,Encoding,Charset,Bound,AttachNum,AttachType,AttachFile,MailBody)
values (@MessageID,@FromAddr,@ReplyTo,@MailTime,@Subject,@contentType,@Encoding,@Charset,@Bound,@AttachNum,@AttachType,@AttachFile,@MailBody)

SELECT @identity1=@@IDENTITY




myConnection.Open()

myCommand = New SqlCommand( "spAddNewMail", myConnection )
myCommand.CommandType = CommandType.StoredProcedure

 

''add input parameters here


myCommand.ExecuteNonQuery()
 If myCommand.Parameters( "RETURN VALUE" ).Value Then
 
      Response.write(MyCommand.Parameters( "@identity1" ).Value )
 
END If
myConnection.Close()
0
 

Author Comment

by:Globemaster
ID: 13596053
Why is it necessary to create a procedure? what will it help me?
0
 

Author Comment

by:Globemaster
ID: 13596147
Also should the procedure be created in the SQL Server?
0
 

Author Comment

by:Globemaster
ID: 13596585
Well here is the Stored Procedure.

CREATE PROCEDURE spAddNewMail
@MessageID text,
@FromAddr text,
@ReplyTo text,
@MailTime text,
@Subject text,
@contentType text,
@Encoding text,
@Charset text,
@Bound text,
@AttachNum int,
@AttachType text,
@AttachFile text,
@MailBody text,
@identity1 int OUTPUT

AS

insert into NewMail(MessageID,FromAddr,ReplyTo,MailTime,Subject,contentType,Encoding,Charset,Bound,AttachNum,AttachType,AttachFile,MailBody)
values (@MessageID,@FromAddr,@ReplyTo,@MailTime,@Subject,@contentType,@Encoding,@Charset,@Bound,@AttachNum,@AttachType,@AttachFile,@MailBody)

SELECT @identity1=SCOPE_IDENTITY()
GO



Also here are my input parameters:

        cmd.Parameters.Add("@MessageID", SqlDbType.Text)
        cmd.Parameters.Add("@FromAddr", SqlDbType.Text)
        cmd.Parameters.Add("@ReplyTo", SqlDbType.Text)
        cmd.Parameters.Add("@MailTime", SqlDbType.Text)
        cmd.Parameters.Add("@Subject", SqlDbType.Text)
        cmd.Parameters.Add("@contentType", SqlDbType.Text)
        cmd.Parameters.Add("@Encoding", SqlDbType.Text)
        cmd.Parameters.Add("@Charset", SqlDbType.Text)
        cmd.Parameters.Add("@Bound", SqlDbType.Text)
        cmd.Parameters.Add("@AttachNum", SqlDbType.Int)
        cmd.Parameters.Add("@AttachType", SqlDbType.Text)
        cmd.Parameters.Add("@AttachFile", SqlDbType.Text)
        cmd.Parameters.Add("@MailBody", SqlDbType.Text)
        cmd.Parameters.Add("@identity1", SqlDbType.Int)



Though when i execute the following error appears:

"Procedure 'spAddNewMail' expects parameter '@identity1', which was not supplied."
0
 

Expert Comment

by:FOnderwater
ID: 13601087
You don't have to use a SP if you don't want to... The command you entered is just fine. I presume MessageID is an int.

Dim result as integer 'We will store the identity in here

Dim cmd As New SqlCommand("insert into NewMail(MessageID,FromAddr,ReplyTo,MailTime,Subject,contentType,Encoding,Charset,Bound,AttachNum,AttachType,AttachFile,MailBody)
values ( @MessageID,@FromAddr,@ReplyTo,@MailTime,@Subject,@contentType,@Encoding,@Charset,@Bound,@AttachNum,@AttachType,@AttachFile,@MailBody); Select Scope_Identity()", conn)

comm.parameters.add("@MessageID", MsgID)
etc.

conn.open
result = cmd.ExecuteScalar
conn.close

Now we have the scope_identity available...

Ferry
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

765 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