Click here to Skip to main content
13,350,029 members (50,101 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

49.4K views
12 bookmarked
Posted 12 Oct 2014

Using SqlParameter Direction in C#.Net with with Stored Procedure

, 12 Oct 2014
Rate this:
Please Sign up or sign in to vote.
Basic way on using SqlParameter Directions (Input, InputOutput,Output,ReturnValue)

Introduction

When using the Microsoft Data Access in your Application and accessing your Microsoft SQL Server 2008 database and the CommandType is a stored procedure, there may be a need to pass input parameters to the stored procedure and get an output or a return value coming from the stored procedure. This article will show you the basic way on how to use the SqlParameter Direction (Input, InputOutput, Output and RetrunValue)

Direction

Description

Input (Default)

The parameter is an input parameter.

Output

The parameter is an output parameter.

InputOutput

The parameter is capable of both input and output.

ReturnValue

The parameter represents a return value from an operation such as a stored   

procedure

Code

C#

private int Execute(ref object outputParam,ref object inoutParam)
    {
            string oUsername = "Jelo";
            int InOutParamValue = 3;
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
            conn.Open();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = conn;
                cmd.CommandText = "MyStoredProcedure";
                cmd.CommandType = CommandType.StoredProcedure;

                // Set Input Parameter
                SqlParameter oParam = new SqlParameter("@Username", oUsername);
                oParam.SqlDbType = SqlDbType.VarChar; 
                cmd.Parameters.Add(oParam);
                
                // Set Output Paramater
                SqlParameter OutputParam = new SqlParameter("@OutputParam", SqlDbType.VarChar);
                OutputParam.Direction = ParameterDirection.Output;
                cmd.Parameters.Add(OutputParam);

                // Set Input Output Parameter
                SqlParameter InOutParam = new SqlParameter("@InputOutputParam", InOutParamValue);
                InOutParam.SqlDbType = SqlDbType.Int;
                InOutParam.Direction = ParameterDirection.InputOutput;
                cmd.Parameters.Add(InOutParam);

                // Set ReturnValue Parameter
                SqlParameter RetParam = new SqlParameter("ReturnValue", DBNull.Value);
                RetParam.Direction = ParameterDirection.ReturnValue;
                cmd.Parameters.Add(RetParam);

                cmd.ExecuteNonQuery();
                 
                // Start getting the RetrunValue and Output from Stored Procedure
                int IntReturn = Convert.ToInt32(cmd.Parameters["ReturnValue"].Value);
                if (IntReturn == 1)
                {
                    outputParam = cmd.Parameters["@OutputParam"].Value;
                    inoutParam = cmd.Parameters["@InputOutputParam"].Value;
                }
                return IntReturn;
           }
           conn.Close();
    }

Stored Procedure

ALTER PROCEDURE MyStoredProcedure
    @Username VARCHAR(100),
    @OutputParam VARCHAR(100) OUTPUT,
    @InputOutputParam INT OUTPUT
AS
BEGIN
    IF EXISTS(SELECT * FROM dbo.Table1 WHERE MemberCode = @Username AND id < @InputOutputParam)
    BEGIN                
        SELECT @OutputParam = MAX(ReferenceNo),@InputOutputParam = MAX(id) FROM dbo.Table1
        WHERE MemberCode = @Username    
        RETURN 1
    END
    ELSE
    BEGIN
        RETURN -1
    END        
END

Hope it helps someone!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

jelo pasiolan
Software Developer
Philippines Philippines
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 1 Pin
Kim Togo20-Oct-14 1:54
professionalKim Togo20-Oct-14 1:54 
GeneralMy vote of 1 Pin
Sinisa Hajnal13-Oct-14 3:20
memberSinisa Hajnal13-Oct-14 3:20 
GeneralMy vote of 2 Pin
VitorHugoGarcia13-Oct-14 0:02
memberVitorHugoGarcia13-Oct-14 0:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 13 Oct 2014
Article Copyright 2014 by jelo pasiolan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid