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...
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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)


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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
in the sample ... notice @Identity is declared by the stored proc as output ...
  @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

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);
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.