ErPotenza
Fri Sep 02 04:21:57 CDT 2005
On Thu, 01 Sep 2005 22:06:09 -0500, Buddy Ackerman
<buddy_nospam@buddyackerman.com> wrote:
>I have a stored procedure that returns a recordset as well as an output parameter. When I use ExecuteReader and then
>try to get the value of the output parameter I get an error saying "Object reference not set to an instance of an
>object.". Is it not possible to get a datareader and output parameters at the same time?
You should post your code, anyway this is an example:
Dim cnn = New SqlClient.SqlConnection
cnn.ConnectionString = "..."
cnn.Open()
Dim cmd As New SqlClient.SqlCommand
With cmd
.CommandText = "StoredProcedureName"
.CommandType = CommandType.StoredProcedure
.Connection = cnn
End With
Dim par As New SqlClient.SqlParameter("@OutParm", _
SqlDbType.Int)
par.Direction = ParameterDirection.Output
cmd.Parameters.Add(par)
Dim dr As SqlClient.SqlDataReader
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
dr.Close()
Dim outParm As Integer = par.Value()
According to MSDN, the DataReader must be closed before getting the
returned value.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatasqlclientsqlparameterclassdirectiontopic.asp