Microsoft KB Archive/308051

= Output parameters are not returned when you run an ADO.NET command in Visual Basic =

Article ID: 308051

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft ADO.NET 2.0
 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic 2005

-



This article was previously published under Q308051



For a Microsoft Visual C# .NET version of this article, see 308621.

For a Microsoft Visual C++ .NET version of this article, see 308624.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Data
 * System.Data.OleDb
 * System.Data.SqlClient



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.



Steps to Reproduce the Behavior
  Create a stored procedure &quot;myProc&quot; 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.</li>  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 </li>  Paste the following code in the code window after the region &quot;Windows Form Designer generated code.&quot;

Note You must replace User ID 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 = _ &quot;User ID= ;password= ;Initial Catalog=pubs;Data Source=myServer&quot; Dim myConnection As New SqlConnection(myConnString) Dim myCommand As New SqlCommand

myCommand.CommandType = CommandType.StoredProcedure myCommand.Connection = myConnection myCommand.CommandText = &quot;MyProc&quot; myCommand.Parameters.Add(&quot;@out&quot;, OleDbType.Integer) 'Uncomment this line to return proper output value. 'myCommand.Parameters(&quot;@out&quot;).Direction = ParameterDirection.Output Try myConnection.Open myCommand.ExecuteNonQuery MessageBox.Show(&quot;Return Value : &quot; & myCommand.Parameters(&quot;@out&quot;).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 = _ &quot;User ID= ;password= ;Initial Catalog=pubs;Data Source=myServer&quot; Dim myConnection As New SqlConnection(myConnString) Dim myCommand As New SqlCommand Dim myReader As SqlDataReader

myCommand.CommandType = CommandType.StoredProcedure myCommand.Connection = myConnection myCommand.CommandText = &quot;MyProc&quot;

myCommand.Parameters.Add(&quot;@out&quot;, OleDbType.Integer) myCommand.Parameters(&quot;@out&quot;).Direction = ParameterDirection.Output

Try myConnection.Open myReader = myCommand.ExecuteReader

'Uncomment this line to return proper output value. 'myReader.Close MessageBox.Show(&quot;Return Value : &quot; & myCommand.Parameters(&quot;@out&quot;).Value)

Catch ex As Exception MessageBox.Show(ex.ToString) Finally myConnection.Close End Try End Sub </li> Modify the Connection string (myConnString) as appropriate for your environment.</li> Save your project. On the Debug menu, click Start to run your project.</li> Click the Direction button and you can see that an incorrect value is returned for the output parameter.</li> 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.</li> Click the Reader button and you may see that an incorrect value is returned for the output parameter.</li> 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.</li></ol>

<div class="references_section">