Solved

get the value of return from a stored procedure

Posted on 2011-02-11
8
285 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

632 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