473,835 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

returning datareader with output parameter

How do you return a datareader from a stored procedure, but also return
an output parameter?

Here is my code, which is just returning a data reader at the moment :

_conn.Open();
SqlCommand _comm = new SqlCommand();
_comm.Connectio n = _conn;
_comm.CommandTy pe = CommandType.Sto redProcedure;
_comm.CommandTe xt = "usp_CE_User_Au thenticate";
_comm.Parameter s.AddWithValue( "@UserName" ,
txtUserName.Tex t.Trim());
_comm.Parameter s.AddWithValue( "@Password" ,
txtPassword.Tex t.Trim());
SqlDataReader dr = _comm.ExecuteRe ader();
User u;
if (dr.HasRows)
{
while (dr.Read())
*** Sent via Developersdex http://www.developersdex.com ***
Sep 11 '07 #1
8 21660
Mike,

You won't have access to the parameters values until you have cycled
through the data reader and read all the data.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Mike P" <mi*******@gmai l.comwrote in message
news:ug******** ******@TK2MSFTN GP04.phx.gbl...
How do you return a datareader from a stored procedure, but also return
an output parameter?

Here is my code, which is just returning a data reader at the moment :

_conn.Open();
SqlCommand _comm = new SqlCommand();
_comm.Connectio n = _conn;
_comm.CommandTy pe = CommandType.Sto redProcedure;
_comm.CommandTe xt = "usp_CE_User_Au thenticate";
_comm.Parameter s.AddWithValue( "@UserName" ,
txtUserName.Tex t.Trim());
_comm.Parameter s.AddWithValue( "@Password" ,
txtPassword.Tex t.Trim());
SqlDataReader dr = _comm.ExecuteRe ader();
User u;
if (dr.HasRows)
{
while (dr.Read())
*** Sent via Developersdex http://www.developersdex.com ***

Sep 11 '07 #2
Mike,

Re-reading your post, it occurs to me that my response might not have
been the answer you were looking for. If you are looking to access the
return value, or the values of output parameters, then you have to access
the SqlParameter instance exposed by the Parameters collection, and get the
value from that, as it will be changed after execution (what I said in my
previous post about reading through all the data still applies).

To get a return value, you have to create a parameter with the Direction
property set to ParameterDirect ion.ReturnValue .

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Mike P" <mi*******@gmai l.comwrote in message
news:ug******** ******@TK2MSFTN GP04.phx.gbl...
How do you return a datareader from a stored procedure, but also return
an output parameter?

Here is my code, which is just returning a data reader at the moment :

_conn.Open();
SqlCommand _comm = new SqlCommand();
_comm.Connectio n = _conn;
_comm.CommandTy pe = CommandType.Sto redProcedure;
_comm.CommandTe xt = "usp_CE_User_Au thenticate";
_comm.Parameter s.AddWithValue( "@UserName" ,
txtUserName.Tex t.Trim());
_comm.Parameter s.AddWithValue( "@Password" ,
txtPassword.Tex t.Trim());
SqlDataReader dr = _comm.ExecuteRe ader();
User u;
if (dr.HasRows)
{
while (dr.Read())
*** Sent via Developersdex http://www.developersdex.com ***

Sep 11 '07 #3
I have tried this way, but I am just getting a null value being
returned. The sproc does return a value, so my syntax must be wrong :

_conn.Open();
SqlCommand _comm = new SqlCommand();
_comm.Connectio n = _conn;
_comm.CommandTy pe = CommandType.Sto redProcedure;
_comm.CommandTe xt = "usp_CE_User_Au thenticate";
_comm.Parameter s.AddWithValue( "@UserName" ,
txtUserName.Tex t.Trim());
_comm.Parameter s.AddWithValue( "@Password" ,
txtPassword.Tex t.Trim());
SqlParameter prmLogID = new SqlParameter("@ LogID",
SqlDbType.Int, 4);
prmLogID.Direct ion = ParameterDirect ion.Output;
_comm.Parameter s.Add(prmLogID) ;

SqlDataReader dr = _comm.ExecuteRe ader();

int intLogID =
Convert.ToInt32 (_comm.Paramete rs["@LogID"].Value);

User u;
if (dr.HasRows)
{
while (dr.Read())
*** Sent via Developersdex http://www.developersdex.com ***
Sep 11 '07 #4
Mike,

As per my first post, the parameter won't be populated until AFTER you
have cycled through all the rows.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Mike P" <mi*******@gmai l.comwrote in message
news:es******** ******@TK2MSFTN GP04.phx.gbl...
>I have tried this way, but I am just getting a null value being
returned. The sproc does return a value, so my syntax must be wrong :

_conn.Open();
SqlCommand _comm = new SqlCommand();
_comm.Connectio n = _conn;
_comm.CommandTy pe = CommandType.Sto redProcedure;
_comm.CommandTe xt = "usp_CE_User_Au thenticate";
_comm.Parameter s.AddWithValue( "@UserName" ,
txtUserName.Tex t.Trim());
_comm.Parameter s.AddWithValue( "@Password" ,
txtPassword.Tex t.Trim());
SqlParameter prmLogID = new SqlParameter("@ LogID",
SqlDbType.Int, 4);
prmLogID.Direct ion = ParameterDirect ion.Output;
_comm.Parameter s.Add(prmLogID) ;

SqlDataReader dr = _comm.ExecuteRe ader();

int intLogID =
Convert.ToInt32 (_comm.Paramete rs["@LogID"].Value);

User u;
if (dr.HasRows)
{
while (dr.Read())
*** Sent via Developersdex http://www.developersdex.com ***

Sep 11 '07 #5
On Sep 11, 11:08 am, Mike P <mike.p...@gmai l.comwrote:
I have tried this way, but I am just getting a null value being
returned. The sproc does return a value, so my syntax must be wrong :

_conn.Open();
SqlCommand _comm = new SqlCommand();
_comm.Connectio n = _conn;
_comm.CommandTy pe = CommandType.Sto redProcedure;
_comm.CommandTe xt = "usp_CE_User_Au thenticate";
_comm.Parameter s.AddWithValue( "@UserName" ,
txtUserName.Tex t.Trim());
_comm.Parameter s.AddWithValue( "@Password" ,
txtPassword.Tex t.Trim());
SqlParameter prmLogID = new SqlParameter("@ LogID",
SqlDbType.Int, 4);
prmLogID.Direct ion = ParameterDirect ion.Output;
_comm.Parameter s.Add(prmLogID) ;

SqlDataReader dr = _comm.ExecuteRe ader();

int intLogID =
Convert.ToInt32 (_comm.Paramete rs["@LogID"].Value);

User u;
if (dr.HasRows)
{
while (dr.Read())
I believe that you need to Close() the SqlDataReader before you can
access output parameters...

Sep 11 '07 #6
Mel
Here is from the help file

the ParameterDirect ion is output, and execution of the associated SqlCommand
does not return a value, the SqlParameter contains a null value.
Output, InputOut, and ReturnValue parameters returned by calling
ExecuteReader cannot be accessed until you call Close or Dispose on the
SqlDataReader.

"Mike P" <mi*******@gmai l.comwrote in message
news:es******** ******@TK2MSFTN GP04.phx.gbl...
>I have tried this way, but I am just getting a null value being
returned. The sproc does return a value, so my syntax must be wrong :

_conn.Open();
SqlCommand _comm = new SqlCommand();
_comm.Connectio n = _conn;
_comm.CommandTy pe = CommandType.Sto redProcedure;
_comm.CommandTe xt = "usp_CE_User_Au thenticate";
_comm.Parameter s.AddWithValue( "@UserName" ,
txtUserName.Tex t.Trim());
_comm.Parameter s.AddWithValue( "@Password" ,
txtPassword.Tex t.Trim());
SqlParameter prmLogID = new SqlParameter("@ LogID",
SqlDbType.Int, 4);
prmLogID.Direct ion = ParameterDirect ion.Output;
_comm.Parameter s.Add(prmLogID) ;

SqlDataReader dr = _comm.ExecuteRe ader();

int intLogID =
Convert.ToInt32 (_comm.Paramete rs["@LogID"].Value);

User u;
if (dr.HasRows)
{
while (dr.Read())
*** Sent via Developersdex http://www.developersdex.com ***

Sep 11 '07 #7
Mike,

You're not listening to what Nicholas said. The value will not be in
the output parameter until AFTER you have read through all the data ...
so you would have to pick up the value AFTER your while loop.

If you need the value in advance then you'll have to make a separate
call to get that info. If that's not practical, and it's not a large
result set, you could consider filling a DataTable instead of using a
DataReader.

--Bob

Mike P wrote:
I have tried this way, but I am just getting a null value being
returned. The sproc does return a value, so my syntax must be wrong :

_conn.Open();
SqlCommand _comm = new SqlCommand();
_comm.Connectio n = _conn;
_comm.CommandTy pe = CommandType.Sto redProcedure;
_comm.CommandTe xt = "usp_CE_User_Au thenticate";
_comm.Parameter s.AddWithValue( "@UserName" ,
txtUserName.Tex t.Trim());
_comm.Parameter s.AddWithValue( "@Password" ,
txtPassword.Tex t.Trim());
SqlParameter prmLogID = new SqlParameter("@ LogID",
SqlDbType.Int, 4);
prmLogID.Direct ion = ParameterDirect ion.Output;
_comm.Parameter s.Add(prmLogID) ;

SqlDataReader dr = _comm.ExecuteRe ader();

int intLogID =
Convert.ToInt32 (_comm.Paramete rs["@LogID"].Value);

User u;
if (dr.HasRows)
{
while (dr.Read())
*** Sent via Developersdex http://www.developersdex.com ***
Sep 11 '07 #8
Thanks everybody, I got it working by reading the output parameter after
I had closed my datareader.
*** Sent via Developersdex http://www.developersdex.com ***
Sep 12 '07 #9

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

Similar topics

5
6049
by: vivienne.netherwood | last post by:
I am developing an Access Project front end with a SQL server database. I have written a stored procedure that returns a record set and also a value via an output parameter. The procedure is as follows CREATE PROCEDURE qslCheckShiftTimes @Ward NVARCHAR(6), @Shift NVARCHAR(10), @Exists TINYINT OUTPUT AS
0
4744
by: Chris Fink | last post by:
Why is the below code not returning an output parameter. When I run the SQL, it is returning the value '1111', but the param1.value is empty? string sql = String.Format("SELECT LICENSEID FROM TABLE WHERE ....."); OracleConnection cn = new OracleConnection(datasource8i);
3
2056
by: Hewit | last post by:
I have a stored procedure which returns records and output parameters(SQL2K). How to call this stored procedure using ADO.Net so that I can use both results in my application. I have .Net Framework 1.1. when I use ExecuteReader() of Command object I cannot read the output parametrs of the stored procedure in code. Please advice. Hewit
8
2016
by: Patreek | last post by:
Hi, On the line where I'm assigning RecordCount to be the value of my output parameter, I'm getting the generic "Object reference not set to an instance of an object" error. I've isolated it down to this line, as the line of code commented out just beneath it runs fine. Can anyone see why my parameter isn't an object? When I grab the command from SQL profiler and run it in Query Analyzer, I get my output parameter returned with a...
4
50419
by: Mr Not So Know It All | last post by:
im new to SQL Server and ASP.Net. Here's my problem. I have this SQL Server stored procedure with an input parameter and output parameter CREATE PROCEDURE . @in_rc varchar(8) @out_eList varchar(7) output AS select @out_eList = ecuid from _organization where rccode = @in_rc GO
6
2737
by: c676228 | last post by:
Hi everyone, I wrote a store procedure that fetch one row data in the database based on the parameter value I entered. After I created the store procedure, the store procedure code looks like this: ALTER proc getProductCommScale @product As varchar(30), @TISCommRate As Decimal(5,2) OUTPUT, @BrokerCommRate As Decimal(5,2) OUTPUT, @Fee As Decimal(5,2) OUTPUT As if RTRIM(@product)='Imed' Select @TISCommRate=TISComm,...
8
2707
by: Alec MacLean | last post by:
Hi, I'm using the DAAB Ent Lib (Jan 2006) for .NET 2.0, with VS 2005 Pro. My project is a Web app project (using the WAP add in). Background: I'm creating a survey system for our company, for which invites will target selected personnel among our customers via email. Each email will provide a custom hyperlink for each respondent using a SQL generated GUID value in the querystring. The GUID will be checked for validity before the user...
1
12328
by: John Bailo | last post by:
This is a my solution to getting an Output parameter from a SqlDataSource. I have seen a few scant articles but none of them take it all the way to a solution. Hopefully this will help some poor soul. Situation: I want to do a lookup using a stored procedure for each value in a Row within a GridView. I use a lookup function in my code behind, evaluating the necessary bound fields. The problem is the SqlDataSource representing...
10
4046
by: Mike | last post by:
Sql Server, Scope_Identity(), Ado.NET: Which is better? Using an output parameter to return Scope_Identity through ExecuteNonQuery(), or adding Select Scope_Identity() to the end of the procedure or ad hoc SQL and using ExecuteScalar()? Thanks.
0
9802
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9652
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10517
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10559
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7765
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5802
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3990
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3086
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.