Solved

Returned value from stored procedure - where is it coming from?

Posted on 2008-06-12
4
176 Views
Last Modified: 2010-04-23
Hi

I'm updating a website and I'm looking at code that I didn't write :)

I see code that inserts data into a table in a database.
It's fairly straightforward.
The table 'news' has a primary key which is an identity - 'news_rid'

In the Visual Basic.Net website I see this line.

_newsid = Convert.ToInt32(SqlHelper.ExecuteScalar(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"), "usp_News_Add", contentid, _homepageflag, UserName))
               
I assumed that this inserted a row and assigned the newly created 'news_rid' into _newsid

Is it right to think that?

If so how does the stored procedure return the news_rid?
I don't see an output parameter in the stored procedure (below)

Thanks!
ALTER PROCEDURE [dbo].[usp_News_Add]

(

	@contentid int,

	@homepageflag bit,

	@updatedby nvarchar(55)

)

AS
 

	DECLARE @Error int

	DECLARE @lineID int

	DECLARE @ptrval binary(16)	
 

	declare @updatedid int

	select @updatedid= users_rid from users where users_login = @updatedby

	SET @Error = @@ERROR
 

            IF @Error != 0 GOTO ERROR_HANDLER
 

             BEGIN TRANSACTION 
 

	Insert INTO news

		(

		content_rid,

		news_homepage,

		UpdatedBy,

		deleted_flag

	)

	Values(

		@contentid,

		@homepageflag,

		@updatedid,

		0

	)

	

            COMMIT TRANSACTION           
 

SELECT @@identity AS ID  
 

ERROR_HANDLER:
 

            IF @@TRANCOUNT != 0 ROLLBACK TRANSACTION
 

            RETURN @Error

Open in new window

0
Comment
Question by:hmcgeehan
  • 2
  • 2
4 Comments
 

Author Comment

by:hmcgeehan
ID: 21767113
Just to help I did this


                HttpContext.Current.Response.Write("[" & _newsid & "]<br>")
                _newsid = Convert.ToInt32(SqlHelper.ExecuteScalar(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"), "usp_News_Add", contentid, _homepageflag, UserName))
                HttpContext.Current.Response.Write("[" & _newsid & "]<br>")
                HttpContext.Current.Response.End()

and got this ..

[0]
[9]

so it is assigning the value I just don't understand how :)
0
 
LVL 12

Expert Comment

by:renjurdevan
ID: 21767137
you will get value as table which contains last inserted row's ID

0
 
LVL 12

Accepted Solution

by:
renjurdevan earned 25 total points
ID: 21767144
ExecuteScalar method will return @@identity value! ie, last inserted row's identity value!
0
 

Author Closing Comment

by:hmcgeehan
ID: 31466462
Easy when you know how!

Thanks so much!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net bundle 8 36
System32Int Error 8 44
dynamic menu in asp.net c# 11 31
Round up to 100% in .NET 10 46
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now