Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

get the value of return from a stored procedure

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
vbnetcoder
Asked:
vbnetcoder
  • 4
  • 3
1 Solution
 
gdupadhyayCommented:
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
 
gdupadhyayCommented:
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
 
vbnetcoderAuthor Commented:
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
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!

 
gdupadhyayCommented:
due to:

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


can you please post sql output with col. name?
0
 
vbnetcoderAuthor Commented:
This

sss = parameterResult.Value

did the trick
0
 
gdupadhyayCommented:
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
 
Rajkumar GsSoftware EngineerCommented:
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
 
vbnetcoderAuthor Commented:
ty
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now