Solved

return stored procedure output value in vb.net

Posted on 2009-05-06
1
795 Views
Last Modified: 2013-11-26
I have seen the examples on returning parameters in stored  procedures that use the OUTPUT value.  I am unclear on the syntax however for returning the actual varchar value that is returned by the stored procedure.  It seems the examples return a value of 0 or 1.  How can I call the stored procedure with it's three paramaters and return the varchar value that is returned when the stored procedure is ran into a variable?  I thought using the executescalar method would accomplish this but it does not seem to be working.  Does anyone have any sample code I can look at?  Please advise.
0
Comment
Question by:v4seasons
1 Comment
 
LVL 4

Accepted Solution

by:
cauos earned 250 total points
ID: 24312080
this code snippet below show how to create a stored procedure and call it from asp.net page
Sql Stored Procedure

Create PROCEDURE [dbo].[RS_TestGetUserName] 
 

@UserID int,

@username nvarchar(50) output

AS

begin

select @username = user_username from users where userid=@Userid

end

-------------------

VB.net code calling the stored procedure 

Dim constr As String = ConfigurationManager.ConnectionStrings("ConnStr").ConnectionString

            Dim con As New SqlConnection(constr)

            Dim cmd As New SqlCommand("RS_TestGetUserName", con)

            cmd.CommandType = Data.CommandType.StoredProcedure

            cmd.Parameters.AddWithValue("@UserID", 38107)

            cmd.Parameters.Add("@username", Data.SqlDbType.NVarChar, 50)

            cmd.Parameters("@username").Direction = Data.ParameterDirection.Output

            con.Open()
 

            cmd.ExecuteNonQuery()

            Dim username As String = cmd.Parameters("@username").Value.ToString()

            con.Close()

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

861 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

25 Experts available now in Live!

Get 1:1 Help Now