Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 308621 - Last Review: December 11, 2006 - Revision: 4.5
Output parameters are not returned when you run an ADO.NET command in Visual C#
This article was previously published under Q308621
For a Microsoft Visual Basic .NET version of this
article, see
308051Â
(http://kbalertz.com/Feedback.aspx?kbNumber=308051/EN-US/
)
.
For a Microsoft Visual C++
.NET version of this article, see
308624Â
(http://kbalertz.com/Feedback.aspx?kbNumber=308624/EN-US/
)
.
This article refers
to the following Microsoft .NET Framework Class Library namespaces:
- System.Data.SqlClient
- System.Data.OleDb
On This Page
SYMPTOMS
Output parameters do not appear to be initialized or return
a wrong value when you run an ADO.NET command.
CAUSE
This problem can occur for two reasons:
- Output parameters are returned at the end of the data
stream when you use a DataReader object.
- The Direction property of the parameter is set not set properly.
RESOLUTION
To resolve this problem, use one of the following methods:
- When you use a DataReader object, you must close it or read to the end of the data before
you can view the output parameters.
- Make sure that the direction of the parameter is set to Output or InputOutput (if the parameter is used in the procedure to both send and
receive data).
For more information about how to implement these solutions,
see the "More Information" section.
NOTE: The parameter object for the return value must be the first item
in the
Parameters collection. In addition, make sure that the parameter's data type
matches the expected return value.
STATUS
This
behavior is by design.
MORE INFORMATION
Steps to Reproduce the Behavior
- Run the following query in SQL Server Query Analyzer to
create a stored procedure named "myProc" in the Pubs database:
CREATE proc MyProc
@out smallint OUTPUT
AS
Select * from Titles
Select @out = count(*) from titles
GO
- Start Visual Studio .NET.
- Create a new Windows Application in Visual C# .NET. Form1
is created by default.
- Make sure that your project contains a reference to the System.Data namespace, and add a reference to this namespace if it does
not.
- Place two Command buttons on Form1. Change the Name property of the first button to
btnDirection, and change the Text property to Direction. Change the Name property of the second button to
btnReader, and change the Text property to Reader.
- Use the using statement on the System and System.Data namespaces so that you are not required to qualify declarations
in those namespaces later in your code.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
- Return to Form view, and double-click Direction to add the click event handler. Add the following code to the
handler.
Note You must change User ID =<UID> and
password =<strong password> to the correct values before you run this
code. Make sure that <UID> has the appropriate permissions to
perform this operation on the database.
String myConnString =
"User ID=<UID>;password=<strong password>;Initial Catalog=pubs;Data Source=(local)";
SqlConnection myConnection = new SqlConnection(myConnString);
SqlCommand myCommand = new SqlCommand();
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Connection = myConnection;
myCommand.CommandText = "MyProc";
myCommand.Parameters.Add("@out", OleDbType.Integer);
//Uncomment this line to return the proper output value.
//myCommand.Parameters["@out"].Direction = ParameterDirection.Output;
try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
MessageBox.Show("Return Value : " + myCommand.Parameters["@out"].Value);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
myConnection.Close();
}
- Return to Form view, and double-click Reader to add the click event handler. Add the following code to the
handler.
Note You must change User ID =<UID> and
password =<strong password> to the correct values before you run this
code. Make sure that <UID> has the appropriate permissions to
perform this operation on the database.
String myConnString =
"User ID=<UID>;password=<strong password>;Initial Catalog=pubs;Data Source=(local)";
SqlConnection myConnection = new SqlConnection(myConnString);
SqlCommand myCommand = new SqlCommand();
SqlDataReader myReader;
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Connection = myConnection;
myCommand.CommandText = "MyProc";
myCommand.Parameters.Add("@out", OleDbType.Integer);
myCommand.Parameters["@out"].Direction = ParameterDirection.Output;
try
{
myConnection.Open();
myReader = myCommand.ExecuteReader();
//Uncomment this line to return the proper output value.
//myReader.Close();
MessageBox.Show("Return Value : " + myCommand.Parameters["@out"].Value);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
myConnection.Close();
}
- Modify the connection string (myConnString) in each of the
preceding code samples as appropriate for your environment.
- Save your project.
- On the Debug menu, click Start to run your project.
- Click Direction. Notice that the wrong value is returned for the output
parameter.
- To resolve this problem, uncomment the line of code that
sets the Direction property for the output parameter. Run the project, and then
click Direction. Notice that the output parameter is returned
correctly.
- Click Reader. Notice that the wrong value is returned for the output
parameter.
- To resolve this problem, uncomment the line of code that
closes the Reader object. Run the project, and then click Reader. Notice that output parameter is returned correctly.
REFERENCES
For additional information, click the article number below to
view the article in the Microsoft Knowledge Base:
308049Â
(http://kbalertz.com/Feedback.aspx?kbNumber=308049/EN-US/
)
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual Basic .NET
APPLIES TO
- Microsoft ADO.NET 2.0
- Microsoft ADO.NET 1.0
- Microsoft Visual C# 2005
- Microsoft Visual C# .NET 2003 Standard Edition
- Microsoft Visual C# .NET 2002 Standard Edition
| kbtshoot kbnofix kbprb kbsqlclient kbstoredproc kbsystemdata KB308621 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please