Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 308051 - Last Review: December 6, 2006 - Revision: 2.6
Output parameters are not returned when you run an ADO.NET command in Visual Basic
This article was previously published under Q308051
For a Microsoft Visual C# .NET version of this
article, see
308621Â
(http://kbalertz.com/Feedback.aspx?kbNumber=308621/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
- System.Data.OleDb
- System.Data.SqlClient
On This Page
SYMPTOMS
Output parameters do not appear to be initialized or return
a wrong value when executing an ADO.NET command.
CAUSE
This problem can occur for the following reasons:
- Output parameters are returned at the end of the data
stream when using a DataReader object.
- The Direction property of the parameter is not set properly.
RESOLUTION
- When using a DataReader, you must close it or read to the end of the data before the
output parameters are visible.
- Make sure that the Direction property of the parameter is set to Output, or InputOutput if the
parameter is used in the procedure to both send and receive data.
NOTE: The parameter object for the return value must be the first item
in the
Parameters collection. Also 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
- Create a stored procedure "myProc" in the Pubs database by executing the following query in SQL Server Query
Analyzer:
CREATE proc MyProc
@out integer OUTPUT
AS
Select @out = count(*) from authors
GO
- Start Microsoft Visual Studio .NET.
- Create a new Windows Application in Visual Basic
.NET.
- Make sure that your project contains a reference to the System.Data namespace.
- Place two Command buttons on Form1. Change the Name property of the first button to btnDirection and the Text property to Direction. Change the Name property of the second button to btnReader and the Text property to Reader.
- Use the Imports statement on the System and System.Data namespaces so that you are not required to qualify declarations
in those namespaces later in your code. Add this to the General Declarations
section of Form1.
Imports System
Imports System.Data.OleDb
Imports System.Data.SqlClient
- Paste the following code in the code window after the
region "Windows Form Designer generated code."
Note You
must replace User ID <user name> with an account that has
appropriate permissions to perform these operations on the database.
Private Sub btnDirection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDirection.Click
Dim myConnString As String = _
"User ID=<username>;password=<strong password>;Initial Catalog=pubs;Data Source=myServer"
Dim myConnection As New SqlConnection(myConnString)
Dim myCommand As New SqlCommand()
myCommand.CommandType = CommandType.StoredProcedure
myCommand.Connection = myConnection
myCommand.CommandText = "MyProc"
myCommand.Parameters.Add("@out", OleDbType.Integer)
'Uncomment this line to return proper output value.
'myCommand.Parameters("@out").Direction = ParameterDirection.Output
Try
myConnection.Open()
myCommand.ExecuteNonQuery()
MessageBox.Show("Return Value : " & myCommand.Parameters("@out").Value)
Catch ex As Exception
MessageBox.Show(ex.ToString())
Finally
myConnection.Close()
End Try
End Sub
Private Sub btnReader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReader.Click
Dim myConnString As String = _
"User ID=<username>;password=<strong password>;Initial Catalog=pubs;Data Source=myServer"
Dim myConnection As New SqlConnection(myConnString)
Dim myCommand As New SqlCommand()
Dim myReader As SqlDataReader
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 proper output value.
'myReader.Close()
MessageBox.Show("Return Value : " & myCommand.Parameters("@out").Value)
Catch ex As Exception
MessageBox.Show(ex.ToString())
Finally
myConnection.Close()
End Try
End Sub
- Modify the Connection string (myConnString) as appropriate
for your environment.
- Save your project. On the Debug menu, click Start to run your project.
- Click the Direction button and you can see that an incorrect value is returned for
the output parameter.
- Uncomment the line of code that sets the Direction property for the output parameter and then run the project. Now
you can see that output parameter is returned correctly when the Direction button is clicked.
- Click the Reader button and you may see that an incorrect value is returned for
the output parameter.
- Uncomment the line of code that closes the DataReader object and then run the project. Now you can see that output
parameter is returned correctly when the Reader button is clicked.
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 (included with the .NET Framework)
- Microsoft Visual Basic .NET 2002 Standard Edition
- Microsoft Visual Basic .NET 2003 Standard Edition
- Microsoft Visual Basic 2005
| kbtshoot kbnofix kbprb kbsqlclient kbsystemdata KB308051 |
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