Solved

get the value of return from a stored procedure

Posted on 2011-02-11
8
284 Views
Last Modified: 2012-05-11
I have this in a stored procedure

RETURN @result;

that can be = 0 or 1

What would the .net code look like that would return it's value?
0
Comment
Question by:vbnetcoder
[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
  • 4
  • 3
8 Comments
 
LVL 9

Expert Comment

by:gdupadhyay
ID: 34876874
You just need to set your return parameter's Direction property to ParameterDirection.ReturnValue, and after you execute your command get return parameter's value like that :

SqlParameter myReturnParam = command.Parameters.Add("@MyReturnValue", SqlDbType.Int);
myReturnParam.Direction = ParameterDirection.ReturnValue;

// Execute your Command here, and get the value of your return parameter :  

int myReturnValue = (int)command.Parameters["@MyReturnValue"].Value;

0
 
LVL 9

Expert Comment

by:gdupadhyay
ID: 34876879
See following URL

http://www.devnewsgroups.net/adonet/t13565-how-run-stored-procedure-that-has-output-parameter.aspx

http://www.codeproject.com/KB/aspnet/SQLHelper20.aspx

I am sure you will get enough information in above URL.

Let me know for any more info.
0
 

Author Comment

by:vbnetcoder
ID: 34876902
THIS IS WHAT I AM DOING

SSS IS ALWAYS RETURNING 1




Dim Connection As SqlClient.SqlConnection = CreateConnection()

        Dim Command As New SqlClient.SqlCommand

        With Command
            .Connection = Connection
            .CommandText = "spValidateLogin "
            .CommandType = CommandType.StoredProcedure

            .Parameters.AddWithValue("@username", UserName)

            .Parameters.AddWithValue("@password", Password)
            .Parameters.AddWithValue("@ipaddress", IPAddress)
            .Parameters.AddWithValue("@result", "1")

            Dim parameterResult As SqlClient.SqlParameter = New SqlClient.SqlParameter("@result", SqlDbType.Int)
            .Parameters.Add(parameterResult)
            parameterResult.Direction = ParameterDirection.ReturnValue


            .ExecuteNonQuery()
            Dim sss As Integer
            sss = Command.Parameters("@result").Value
'sss IS ALWAYS RETURNING 1

        End With


       
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 9

Expert Comment

by:gdupadhyay
ID: 34876918
due to:

.Parameters.AddWithValue("@result", "1")


can you please post sql output with col. name?
0
 

Author Comment

by:vbnetcoder
ID: 34876925
This

sss = parameterResult.Value

did the trick
0
 
LVL 9

Accepted Solution

by:
gdupadhyay earned 500 total points
ID: 34876964
Here you need to follow:


     //Create command  
    SqlCommand Cmd = new SqlCommand("AddSomething", oConn);
    Cmd.CommandType = CommandType.StoredProcedure;

    SqlParameter oParam = Cmd.Parameters.AddWithValue("@zonename", sName);
    oParam = Cmd.Parameters.AddWithValue("@desc", sDesc);
    oParam = Cmd.Parameters.AddWithValue("@TheNewId", 0);
    oParam.Direction = ParameterDirection.Output;
 
    Cmd.ExecuteNonQuery();
 
    oConn.Close();
    int nTheNewId = Convert.ToInt32(Cmd.Parameters["@TheNewId"].Value);
    return nTheNewId;
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34876993
For the case that gdupadhyay: explains, you need to set the parameter as OUTPUT parameter in the Stored Procedure.
CREATE PROCEDURE YourStoredProcedure
(
      @result INT OUTPUT
)
...

Open in new window


I think no need to explain, as you got it
sss = parameterResult.Value

Open in new window

0
 

Author Closing Comment

by:vbnetcoder
ID: 34878696
ty
0

Featured Post

Independent Software Vendors: 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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

752 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