Solved

get the value of return from a stored procedure

Posted on 2011-02-11
8
282 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
  • 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.net VB.net Errors when sending data to SQL 5 20
Footer for each row on Gridview 2 21
Help with exporting to excel 4 28
Expression Evaluater 3 27
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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 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