Avatar of Ed
EdFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

Accessing Stored Procedure Return Value From .NET


Can somone explain to me the simplest most effective way of accessing the return value from a stored procedure using .Net(vb)?

I want to display it in a label on a page

Return @@identity

ASP.NETMicrosoft SQL ServerMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
Ed
ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Hi,
You can use following:
SqlCommand cmd = new SqlCommand();
            //please command Initialization

            object val = cmd.ExecuteScalar();
            if (val != null && val != DBNull.Value)
                lbl.Text = val.ToString();

or

SqlCommand cmd = new SqlCommand();
            //please command Initialization

            object val = cmd.ExecuteScalar();
            if (val != null && val != DBNull.Value)
                return  val.ToString();
            else
                return string.Empty;
   

Thanks
Avatar of Alpesh Patel
Alpesh Patel
Flag of India image

Hi,
You can use following:

If inserting a new row then use Scope_Identity()
SqlCommand cmd = new SqlCommand();
            object val = cmd.ExecuteScalar();
            if (val != null && val != DBNull.Value)
                lbl.Text = val.ToString();

or

SqlCommand cmd = new SqlCommand();
            //please command Initialization

            object val = cmd.ExecuteScalar();
            if (val != null && val != DBNull.Value)
                return  val.ToString();
            else
                return string.Empty;

Open in new window

edjones1,

Since you just need to get the RETURN parameter, you cannot use the ExecuteScalar method, go with the ExecuteNonQuery method instead as suggested by Dhaest (no points please).
Avatar of Ed
Ed
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

The link was really helpful, thanks
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo