Click here to Skip to main content
15,892,059 members

how to read the return value of sql store procedure in C# using dataset

Member 4462638 asked:

Open original thread
hi folks,

I want to read the return value of sql store procedure in C# using dataset. How can i achieve this?

Below is my current:

store procedure:

SQL
create procedure abc
(
    @pcode varchar(max),
    @RETURNVALUE INT =0 OUT
)

as

DECLARE @COUNT INT

    BEGIN
        SELECT @COUNT=COUNT(*) FROM product where pcode=@pcode
            IF(@COUNT >0)
                BEGIN
                    SET @RETURNVALUE = 1;
                    RETURN @RETURNVALUE
                END
            ELSE
                BEGIN
                SELECT @COUNT=COUNT(*) FROM ProductColor where pcode=@pcode
                    IF(@COUNT >0)
                        BEGIN
                            SET @RETURNVALUE = 1;
                            RETURN @RETURNVALUE
                        END
                    ELSE
                        BEGIN
                            SELECT @COUNT=COUNT(*) FROM ProductColorSize where pcode=@pcode
                                IF(@COUNT >0)
                                    BEGIN
                                        SET @RETURNVALUE = 1;
                                        RETURN @RETURNVALUE
                                    END
                                ELSE
                                    BEGIN
                                        SET @RETURNVALUE = 0;
                                        RETURN @RETURNVALUE
                                    END

    END
    END
END
select RETURNVALUE=@RETURNVALUE;



---------------------------

C# application

C#
ds1 = new DataSet();


                                    try
                                    {
                                        paraPM = new SqlParameter[1];
                                        paraPM[0] = new SqlParameter("@pcode", Code.ToString().Replace("'", "'"));

                                        ds1 = Dataacess.GetDataSet("verifyProductCodeExcel2", CommandType.StoredProcedure, paraPM);




                                        if (ds1.Tables[0].Rows.Count > 0)
                                        {

                                            eok = false;


                                        }
                                        else
                                        {
                                            eok = true;

                                        }
                                    }
                                    catch (Exception ex)
                                    {


                                        throw ex;

                                    }
                                    finally
                                    {
                                        ds1.Dispose();

                                        paraPM = null;
                                    }

------------------------------------

C#
DataSet ds=null;
        SqlCommand cmd=null;
        SqlDataAdapter da=null;
        SqlConnection con=null;
        try
        {

            string conString = siteconfiguration.DbConnectionstring();
            using (con = new SqlConnection(conString))
            {
                con.Open();
                using (cmd = new SqlCommand(cmdText, con))
                {
                    cmd.CommandType = cmdType;
                    if (parameters != null)
                    {
                        foreach (SqlParameter parameter in parameters)
                        {
                            if (null != parameter) cmd.Parameters.Add(parameter);
                        }
                    }
                    using (da = new SqlDataAdapter(cmd))
                    {
                        ds = new DataSet();
                        da.Fill(ds);

                        return ds;
                    }
                }
            }
        }
        catch (Exception ex)
        {
           //HttpContext.Current.Response.Write("===" + ex.Message + ex.StackTrace);
           throw ex;

        }
        finally
            {
                if (ds != null)
                {
                    ds.Dispose();
                }
                if (da != null)
                {
                    da.Dispose();
                }
                if (cmd != null)
                {
                    cmd.Parameters.Clear();
                    cmd.Dispose();
                }
                if (con != null)
                {
                    con.Dispose();
                }

            }



================================

From Mustafa:
Please format your code and postings properly.
Tags: C#, SQL, ASP.NET, Server

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900