Solved

get the value of return from a stored procedure

Posted on 2011-02-11
8
277 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 9

Expert Comment

by:gdupadhyay
Comment Utility
due to:

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


can you please post sql output with col. name?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:vbnetcoder
Comment Utility
This

sss = parameterResult.Value

did the trick
0
 
LVL 9

Accepted Solution

by:
gdupadhyay earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
ty
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

18 Experts available now in Live!

Get 1:1 Help Now