472,351 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,351 software developers and data experts.

Return value before reading DataReader

I am trying to set up a class to handle my database accesses.

I can't seem to figure out how to get the return value from my dataReader
from these routines (most of which I got elsewhere). They do work pretty
well, except for the change I made to get the return value.

For example, I have the following:
************************************************** ********************
Public Overloads Function RunProcedure( _
ByVal storedProcName As String, _
ByVal parameters As IDataParameter(), _
ByRef returnValue As Integer) _
As SqlDataReader

Dim returnReader As SqlDataReader

myConnection.Open()
Dim command As SqlCommand = _
BuildIntCommand(storedProcName, parameters)

returnReader = command.ExecuteReader( CommandBehavior.CloseConnection)
' Connection will be closed automatically
returnValue = CInt(command.Parameters("ReturnValue").Value)

Return returnReader

End Function

Private Function BuildIntCommand( _
ByVal storedProcName As String, _
ByVal parameters As IDataParameter()) _
As SqlCommand

Dim command As SqlCommand = _
BuildQueryCommand(storedProcName, parameters)
Dim parameter As New SqlParameter()

With parameter
.ParameterName = "ReturnValue"
.DbType = SqlDbType.Int
.Size = 4
.Direction = ParameterDirection.ReturnValue
.IsNullable = False
.Precision = 0
.Scale = 0
.SourceColumn = String.Empty
.SourceVersion = DataRowVersion.Default
.Value = Nothing
End With
command.Parameters.Add(parameter)

Return command

End Function

Private Function BuildQueryCommand( _
ByVal storedProcName As String, _
ByVal parameters As IDataParameter()) _
As SqlCommand

Dim command As New SqlCommand(storedProcName, myConnection)
command.CommandType = CommandType.StoredProcedure

Dim parameter As SqlParameter
For Each parameter In parameters
command.Parameters.Add(parameter)
Next

Return command

End Function
************************************************** **************

The problem seems to be that I found out I either need to close the
connection or run NextResult() to get the return value. I would need to get
access to the Command object to get the return value (I think). Since I
would need to Read the DataReader before I close the connection - I have no
access to Command (since I am only passing back the DataReader to the
caller).

Is there anyway to get the return value from the DataReader itself?

Thanks,

Tom
Nov 19 '05 #1
3 2256
Ben
Sql return values are a lot like method/function return values. They aren't
available until the function has finished running. You need to close your
reader to access the return value or output parameters iirc... If you
really need to access the return value before closing the reader try
returning the return value as a seperate result set if possible...

HTH,
Ben
"tshad" <ts**********@ftsolutions.com> wrote in message
news:OX**************@TK2MSFTNGP09.phx.gbl...
I am trying to set up a class to handle my database accesses.

I can't seem to figure out how to get the return value from my dataReader
from these routines (most of which I got elsewhere). They do work pretty
well, except for the change I made to get the return value.

For example, I have the following:
************************************************** ********************
Public Overloads Function RunProcedure( _
ByVal storedProcName As String, _
ByVal parameters As IDataParameter(), _
ByRef returnValue As Integer) _
As SqlDataReader

Dim returnReader As SqlDataReader

myConnection.Open()
Dim command As SqlCommand = _
BuildIntCommand(storedProcName, parameters)

returnReader = command.ExecuteReader(
CommandBehavior.CloseConnection)
' Connection will be closed automatically
returnValue = CInt(command.Parameters("ReturnValue").Value)

Return returnReader

End Function

Private Function BuildIntCommand( _
ByVal storedProcName As String, _
ByVal parameters As IDataParameter()) _
As SqlCommand

Dim command As SqlCommand = _
BuildQueryCommand(storedProcName, parameters)
Dim parameter As New SqlParameter()

With parameter
.ParameterName = "ReturnValue"
.DbType = SqlDbType.Int
.Size = 4
.Direction = ParameterDirection.ReturnValue
.IsNullable = False
.Precision = 0
.Scale = 0
.SourceColumn = String.Empty
.SourceVersion = DataRowVersion.Default
.Value = Nothing
End With
command.Parameters.Add(parameter)

Return command

End Function

Private Function BuildQueryCommand( _
ByVal storedProcName As String, _
ByVal parameters As IDataParameter()) _
As SqlCommand

Dim command As New SqlCommand(storedProcName, myConnection)
command.CommandType = CommandType.StoredProcedure

Dim parameter As SqlParameter
For Each parameter In parameters
command.Parameters.Add(parameter)
Next

Return command

End Function
************************************************** **************

The problem seems to be that I found out I either need to close the
connection or run NextResult() to get the return value. I would need to
get access to the Command object to get the return value (I think). Since
I would need to Read the DataReader before I close the connection - I have
no access to Command (since I am only passing back the DataReader to the
caller).

Is there anyway to get the return value from the DataReader itself?

Thanks,

Tom

Nov 19 '05 #2
in the datastream (tds) the return value comes after all other result sets.
to access the return value, you need to read thru all rows and result sets
first (or close the reader - which will do this automatically).

-- bruce (sqlwork.com)
"tshad" <ts**********@ftsolutions.com> wrote in message
news:OX**************@TK2MSFTNGP09.phx.gbl...
I am trying to set up a class to handle my database accesses.

I can't seem to figure out how to get the return value from my dataReader
from these routines (most of which I got elsewhere). They do work pretty
well, except for the change I made to get the return value.

For example, I have the following:
************************************************** ********************
Public Overloads Function RunProcedure( _
ByVal storedProcName As String, _
ByVal parameters As IDataParameter(), _
ByRef returnValue As Integer) _
As SqlDataReader

Dim returnReader As SqlDataReader

myConnection.Open()
Dim command As SqlCommand = _
BuildIntCommand(storedProcName, parameters)

returnReader = command.ExecuteReader(
CommandBehavior.CloseConnection)
' Connection will be closed automatically
returnValue = CInt(command.Parameters("ReturnValue").Value)

Return returnReader

End Function

Private Function BuildIntCommand( _
ByVal storedProcName As String, _
ByVal parameters As IDataParameter()) _
As SqlCommand

Dim command As SqlCommand = _
BuildQueryCommand(storedProcName, parameters)
Dim parameter As New SqlParameter()

With parameter
.ParameterName = "ReturnValue"
.DbType = SqlDbType.Int
.Size = 4
.Direction = ParameterDirection.ReturnValue
.IsNullable = False
.Precision = 0
.Scale = 0
.SourceColumn = String.Empty
.SourceVersion = DataRowVersion.Default
.Value = Nothing
End With
command.Parameters.Add(parameter)

Return command

End Function

Private Function BuildQueryCommand( _
ByVal storedProcName As String, _
ByVal parameters As IDataParameter()) _
As SqlCommand

Dim command As New SqlCommand(storedProcName, myConnection)
command.CommandType = CommandType.StoredProcedure

Dim parameter As SqlParameter
For Each parameter In parameters
command.Parameters.Add(parameter)
Next

Return command

End Function
************************************************** **************

The problem seems to be that I found out I either need to close the
connection or run NextResult() to get the return value. I would need to
get access to the Command object to get the return value (I think). Since
I would need to Read the DataReader before I close the connection - I have
no access to Command (since I am only passing back the DataReader to the
caller).

Is there anyway to get the return value from the DataReader itself?

Thanks,

Tom

Nov 19 '05 #3
"Ben" <be*@online.nospam> wrote in message
news:b9***************************@FUSE.NET...
Sql return values are a lot like method/function return values. They
aren't available until the function has finished running. You need to
close your reader to access the return value or output parameters iirc...
If you really need to access the return value before closing the reader
try returning the return value as a seperate result set if possible...
So in the case of where you have something like

function GetReader("stored Procedure Name", parameters) as
SqlDataReader

There would be no way to get the return value, since I don't have the
command object (where you get the return value) and I obviously can't close
the SqlDataReader to get it in my function - because then the DataReader
would have nothing in it.

I don't really want to write my Stored procedure to accommodate the
DataReader. I want the return value to be a return value.

Thanks,

Tom
HTH,
Ben
"tshad" <ts**********@ftsolutions.com> wrote in message
news:OX**************@TK2MSFTNGP09.phx.gbl...
I am trying to set up a class to handle my database accesses.

I can't seem to figure out how to get the return value from my dataReader
from these routines (most of which I got elsewhere). They do work pretty
well, except for the change I made to get the return value.

For example, I have the following:
************************************************** ********************
Public Overloads Function RunProcedure( _
ByVal storedProcName As String, _
ByVal parameters As IDataParameter(), _
ByRef returnValue As Integer) _
As SqlDataReader

Dim returnReader As SqlDataReader

myConnection.Open()
Dim command As SqlCommand = _
BuildIntCommand(storedProcName, parameters)

returnReader = command.ExecuteReader(
CommandBehavior.CloseConnection)
' Connection will be closed automatically
returnValue = CInt(command.Parameters("ReturnValue").Value)

Return returnReader

End Function

Private Function BuildIntCommand( _
ByVal storedProcName As String, _
ByVal parameters As IDataParameter()) _
As SqlCommand

Dim command As SqlCommand = _
BuildQueryCommand(storedProcName, parameters)
Dim parameter As New SqlParameter()

With parameter
.ParameterName = "ReturnValue"
.DbType = SqlDbType.Int
.Size = 4
.Direction = ParameterDirection.ReturnValue
.IsNullable = False
.Precision = 0
.Scale = 0
.SourceColumn = String.Empty
.SourceVersion = DataRowVersion.Default
.Value = Nothing
End With
command.Parameters.Add(parameter)

Return command

End Function

Private Function BuildQueryCommand( _
ByVal storedProcName As String, _
ByVal parameters As IDataParameter()) _
As SqlCommand

Dim command As New SqlCommand(storedProcName, myConnection)
command.CommandType = CommandType.StoredProcedure

Dim parameter As SqlParameter
For Each parameter In parameters
command.Parameters.Add(parameter)
Next

Return command

End Function
************************************************** **************

The problem seems to be that I found out I either need to close the
connection or run NextResult() to get the return value. I would need to
get access to the Command object to get the return value (I think).
Since I would need to Read the DataReader before I close the connection -
I have no access to Command (since I am only passing back the DataReader
to the caller).

Is there anyway to get the return value from the DataReader itself?

Thanks,

Tom


Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0)...
3
by: Craig | last post by:
I have some methods that open a database connection, get some data and then return a datareader. How do I manage closing the connection to the...
6
by: Grant | last post by:
I am connecting to an access database using a datareader in C#. I get results when I run a certain query from Access but when I run it from Code it...
2
by: Scott Natwick | last post by:
Hi, Is there a way to obtain the return code from a stored procedure? Here is what I have so far. The procedure executes, but I'm not able to...
5
by: D. Shane Fowlkes | last post by:
This may be a very basic question but it's something I've never done before. I've looked at a couple of my favorite sites and books and can't find...
2
by: orencs | last post by:
Hi, I am using Datareader and stored procedure in C# ADO.NET. When I am running the stored procedure in the SQL Query Analyzer and recieve two...
2
by: Sam anonymous | last post by:
This is what I have so far but if someone could show me how to read through the datareader once I can get the rest. Thank you. Private Sub...
4
by: D. Shane Fowlkes | last post by:
Up until now, I've always had my functions return integers, strings, or booleans. Now, I've (hopefully) written a function to return a 2 column,...
7
by: Rudy | last post by:
Hello All! I have a value in a textbox(txbTableIDm.Text ) that I would like to use in a paremiter in a SP I wrote, and then have the select...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.