473,779 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQLDataReader question

I have a common program where I have a method which make a sql
connection and then prepares a SqlDataReader that is passed to the
calling method. But in the common method, I have the connection close
method as well. So when the query runs the data reader has data but
after the close is called the data reader becomes null. Is there a way
I can preserver the data reader after closing the connection??

Thanks.
Nov 12 '08 #1
3 1852
On Nov 12, 3:45*pm, CSharper <cshar...@gmx.c omwrote:
I have a common program where I have a method which make a sql
connection and then prepares a SqlDataReader that is passed to the
calling method. But in the common method, I have the connection close
method as well. So when the query runs the data reader has data but
after the close is called the data reader becomes null. Is there a way
I can preserver the data reader after closing the connection??

Thanks.
Found the answer, CommandBehaviou r.
Nov 12 '08 #2

No.

You should consume the DataReader as fast as you can and then dispose of it.

If you need its contents....you put it(them) into some kind of custom
object.

Database connections aren't cheap.........

You can find a downloadable example here:
http://sholliday.space s.live.com/Blog/cns!A68482B9628 A842A!140.entry

of putting the results of an IDataReader into a custom object and/or
collection.

"CSharper" <cs******@gmx.c omwrote in message
news:63******** *************** ***********@o4g 2000pra.googleg roups.com...
>I have a common program where I have a method which make a sql
connection and then prepares a SqlDataReader that is passed to the
calling method. But in the common method, I have the connection close
method as well. So when the query runs the data reader has data but
after the close is called the data reader becomes null. Is there a way
I can preserver the data reader after closing the connection??

Thanks.

Nov 12 '08 #3

Yeah, you can use the enum.

Just know what you're doing. You actually don't want to keep it around (as
short as time as possible). <<( $.05 explanation)

"sloan" <sl***@ipass.ne twrote in message
news:Oi******** ******@TK2MSFTN GP03.phx.gbl...
>
No.

You should consume the DataReader as fast as you can and then dispose of
it.

If you need its contents....you put it(them) into some kind of custom
object.

Database connections aren't cheap.........

You can find a downloadable example here:
http://sholliday.space s.live.com/Blog/cns!A68482B9628 A842A!140.entry

of putting the results of an IDataReader into a custom object and/or
collection.

"CSharper" <cs******@gmx.c omwrote in message
news:63******** *************** ***********@o4g 2000pra.googleg roups.com...
>>I have a common program where I have a method which make a sql
connection and then prepares a SqlDataReader that is passed to the
calling method. But in the common method, I have the connection close
method as well. So when the query runs the data reader has data but
after the close is called the data reader becomes null. Is there a way
I can preserver the data reader after closing the connection??

Thanks.


Nov 12 '08 #4

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

Similar topics

2
4750
by: Rod | last post by:
Thanks in advance. I have two listboxes on a single asp page. I am trying to use sqldatareader to populate both using two seperate sql stored procs. I can populate each box seperately using two different methods but when I try to populate them both no the same page I get "InvalidOperationException: Invalid attempt to FieldCount when reader is closed" WHY???
3
536
by: Ricola ! | last post by:
Why do I say: SqlDataReader dr; instead of SqlDataReader dr = new SqlDataReader();
3
3018
by: PeteZ | last post by:
I'm having a problem where I exec a stored procedure (which SELECTs all rows from a table, which has 100 rows) - each row has 8 columns. When I exec the code (see below) I get 8 in oSQLDR.FieldCount (which I expect but whenever I try and reference the data in the row (oSQLDR.GetInt32(0);) I get an exception "invalid attempt to read when no data is present". If I exec the SP in an ISQL window using: EXEC getEvents, I get 100 rows...
7
2363
by: Franck Diastein | last post by:
Hi, when I call ExportData I have this error: Invalid attempt to Read when reader is closed. Telling me that there's a problem with this line: while(_dataR.Read()){ Code: ************************************************* public void Export2CSV(){
3
5889
by: Neil Guyette | last post by:
Hello, Everyone, I'm trying to find information on how to populate a combo box using a SqlDataReader. I want to be able to set the value of the combo's value property different then the combo's text property (what the user will see). Is this possible with a SqlDataReader? Thanks
5
12136
by: Wing | last post by:
Hi all, I execute a stored procedure in my C# code, assign the result to the SqlDataReader object and display it with datagrid. the question I like to ask, is possible to edit the datagrid output (eg. delete a row in the datagrid) and update the change in the corresponding table in the database? thanks for your time.
1
3265
by: Arvind P Rangan | last post by:
Hi All, How do you get all the values of a sqldatareader if it contains multiple resultset. Using sqldatareader.nextresult and sqldatareader.read e.g. While sqldatareader.read ' If not sqldatareader then sqldatareader.nextresult
4
2365
by: mimi | last post by:
Hi Please help me out, I can't find a way to close a sqldatareader when error occur at statement cmd.ExecuteReader(). I can't close it in catch because it is local in try scope and I can't declare it outside try scope either since we have to call cmd.executeReader to create sqldatareader public string GetLogs(int logID) {
2
1626
by: SQL | last post by:
I'm new at this, so please forgive me if my question has a simple answer to it. I'm trying to get a value out of a SqlDataReader. Here is my code to do so: Dim mySQLCommand As New SqlCommand("SELECT NextPrbKey FROM SystemAdmin", cn) Dim mySQLReader As SqlDataReader = mySQLCommand.ExecuteReader() intGetNextKey = mySQLReader.GetValue(0) This does not return a value. I thought that GetValue(0) would return the
3
2831
by: Frank Milverckowitz | last post by:
Hi, Newbie question about SqlDataReader column value access... In java jdbc code to get a value from a table column we can pass the column name (instead of an int index or offset): String strLastName = rs.getString( "LastName" );
0
9636
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
10306
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10138
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
10074
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,...
0
9930
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7485
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
6724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.