Posted on 2004-11-19
Last Modified: 2012-05-05
Can someone explain to me the purpose of this:

public void CreateSqlParameter()
    SqlParameter myParameter = new SqlParameter("@Description", SqlDbType.VarChar);
    myParameter.Direction = ParameterDirection.Output;

When do you use the ParameterDirection.Output... or ParameterDirection.Input? When would you use these. I've read something in msdn but I still do not see the point...
Question by:dotnet22
    LVL 5

    Accepted Solution

    Sometimes a stored procedure can have OUTPUT parameters.

    CREATE PROC PostNewInvoiceAndBlankBill(@CustID int, @ProdID int,  @InvoiceID int output, @BillID int output)  as

         Insert Invoices (CustomerID, ProductID) Values (@CustID, @ProdID)
         SELECT @InvoiceID = @@IDENTITY

         INSERT Bills(CustomerID, InvoiceID) VALUES (@CustID, @InvoiceID)
         SELECT @BillID = @@IDENTITY


    In the above example, the stored proc doesn't actually "SELECT" anything for return to the caller. But, you'll notice two records are created, and each record's identity column is captured. You'll also notice that the Proc's declaration has the word "output" appended after @InvoiceID and @BillID, which tells SQL server to output whatever final values @InvoiceID and @BillID are set to when exiting the proc.

    If you were using a TSQL stored Proc to call the above stored proc, you would call it like this:

         CREATE MyProc as
              DECLARE @IDinvoice int, @IDbill int
              EXEC PostNewInvoiceAndBlankBill 102936, 88266, @IDinvoice output, @IDbill output

    in the TSQL script ... right here ... you would have access to the values OUTPUT by PostNewInvoiceAndBlankBill .... @IDinvoice and @IDbill

    The ParameterDirection.Output allows your subsequent CMD.Execute to caputre the output.

    Hope this helps

    LVL 5

    Assisted Solution

    LVL 5

    Assisted Solution

    in the sample ... notice @Identity is declared by the stored proc as output ...
    CREATE PROCEDURE InsertCategory
      @CategoryName nchar(15),
      @Identity int OUT
    INSERT INTO Categories (CategoryName) VALUES(@CategoryName)
    SET @Identity = @@Identity

    And in the C# code find the lines

    myParm = catDA.InsertCommand.Parameters.Add("@Identity", SqlDbType.Int, 0, "CategoryID");
    myParm.Direction = ParameterDirection.Output;

    which declare @identity as an expected output parameter

    LVL 5

    Assisted Solution

    Actually this example is better because it shows accessing the output parameter after the command.execute.

    The line that accesses the output is:
    Console.WriteLine(" @OutputParm: {0}", sampleCMD.Parameters["@OutputParm"].Value);

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 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

    19 Experts available now in Live!

    Get 1:1 Help Now