473,769 Members | 5,910 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When to close connection?

I have a function that returns a SqlDataReader created by:

result = command.Execute Reader(CommandB ehavior.CloseCo nnection);

I then bind the result to a grid control:

myDataGrid.Data Source = sr;
myDataGrid.Data Bind();

Do I need to explicitly close the DataReader (and thus the connection) after
the DataBind?

Thanks,
Bill

Oct 19 '05 #1
4 1592
your function should use sqlreader.close . That should also take care of the
connection but I see your CommandBehavior is also taking care of the closing
the connection . In the calling client user sr = nothing.

"billd" wrote:
I have a function that returns a SqlDataReader created by:

result = command.Execute Reader(CommandB ehavior.CloseCo nnection);

I then bind the result to a grid control:

myDataGrid.Data Source = sr;
myDataGrid.Data Bind();

Do I need to explicitly close the DataReader (and thus the connection) after
the DataBind?

Thanks,
Bill

Oct 19 '05 #2
If I do:

sr = null;

Won't this just flag the object for deletion until the garbage collection
actually destroys it? If I need to explicity close the reader, would it not
be better to call the Close method?

Anyhow, so I guess you are saying by this that I could also wait until the
Grid goes out of scope which will also set the sr to null.

Thanks
Bill
"Chris" wrote:
your function should use sqlreader.close . That should also take care of the
connection but I see your CommandBehavior is also taking care of the closing
the connection . In the calling client user sr = nothing.

"billd" wrote:
I have a function that returns a SqlDataReader created by:

result = command.Execute Reader(CommandB ehavior.CloseCo nnection);

I then bind the result to a grid control:

myDataGrid.Data Source = sr;
myDataGrid.Data Bind();

Do I need to explicitly close the DataReader (and thus the connection) after
the DataBind?

Thanks,
Bill

Oct 19 '05 #3
This is a sample

Try

DR = db.ExecuteReade r(whatever)

While DR.Read

MsgBox(DR.Item( 0))

End While

Catch ex As Exception

MsgBox(ex.Messa ge)

Finally

If (Not DR Is Nothing) Then
DR.Close()
End If

End Try

look at the finally block and convert to c#.

"billd" wrote:
If I do:

sr = null;

Won't this just flag the object for deletion until the garbage collection
actually destroys it? If I need to explicity close the reader, would it not
be better to call the Close method?

Anyhow, so I guess you are saying by this that I could also wait until the
Grid goes out of scope which will also set the sr to null.

Thanks
Bill
"Chris" wrote:
your function should use sqlreader.close . That should also take care of the
connection but I see your CommandBehavior is also taking care of the closing
the connection . In the calling client user sr = nothing.

"billd" wrote:
I have a function that returns a SqlDataReader created by:

result = command.Execute Reader(CommandB ehavior.CloseCo nnection);

I then bind the result to a grid control:

myDataGrid.Data Source = sr;
myDataGrid.Data Bind();

Do I need to explicitly close the DataReader (and thus the connection) after
the DataBind?

Thanks,
Bill

Oct 19 '05 #4
Bill,

sr = null; or sr = nothing does in my opinion absolute nothing here, when
your page is sent back, everything that is not shared will be completely
released by the GC at the right time.

Clossing a connection is always needed of the connection pooling.
(Which you did as Chris already wrote)

I hope this gives an idea

Cor

"billd" <bi***@discussi ons.microsoft.c om> schreef in bericht
news:2D******** *************** ***********@mic rosoft.com...
If I do:

sr = null;

Won't this just flag the object for deletion until the garbage collection
actually destroys it? If I need to explicity close the reader, would it
not
be better to call the Close method?

Anyhow, so I guess you are saying by this that I could also wait until the
Grid goes out of scope which will also set the sr to null.

Thanks
Bill
"Chris" wrote:
your function should use sqlreader.close . That should also take care of
the
connection but I see your CommandBehavior is also taking care of the
closing
the connection . In the calling client user sr = nothing.

"billd" wrote:
> I have a function that returns a SqlDataReader created by:
>
> result = command.Execute Reader(CommandB ehavior.CloseCo nnection);
>
> I then bind the result to a grid control:
>
> myDataGrid.Data Source = sr;
> myDataGrid.Data Bind();
>
> Do I need to explicitly close the DataReader (and thus the connection)
> after
> the DataBind?
>
> Thanks,
> Bill
>

Oct 19 '05 #5

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

Similar topics

4
18135
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed as I find appropriate. I am using the socket asynchronously by calling the BeingSend and BeginReceive calls. I would like to be able to call shutdown and close asynchronously if possible.
7
1798
by: Dan Bass | last post by:
In a somewhat complex application, I've developed plug-in architecture and am having a problem as to when to catch general exceptions for logging purposes. In each plug-in class library, for example, I'm assuming it's throw on errors, and that the core then catches them. I've got proxy classes handling some fiddly details of the plug-ins. Should I allow the exception to bubble to the top most point it can in a call stack, then catch...
2
5063
by: Patrick Olurotimi Ige | last post by:
Why do i get "Invalid attempt to FieldCount when reader is closed" Is the problem the way the datareader reads data as opposed to a dataset? When trying to compile this code:- Dim reader As IDataReader = GetReader() Dim chart As New LineChart()
4
238
by: billd | last post by:
I have a function that returns a SqlDataReader created by: result = command.ExecuteReader(CommandBehavior.CloseConnection); I then bind the result to a grid control: myDataGrid.DataSource = sr; myDataGrid.DataBind(); Do I need to explicitly close the DataReader (and thus the connection) after
1
3898
by: Billy | last post by:
Hi All, I'm attempting to use the MapNetworkDrive <snippedbelow from entire code below with very poor performance results. Basically, I have very small 73kb text files that are rewritten daily to a network share using the MapNetworkDrive command to connect to the valid \\servername\share path. After successfully connecting the drive in the ASP code, the text file
2
12305
by: =?Utf-8?B?U3VuaWwgUGFuZGl0YQ==?= | last post by:
Hi All, I have created a VB.Net application that used classic ADO to access the oracle database. I have mentained a persistant connection in it. When i start the application and it works fine. All of a sudden i start receiving the following error messages countineoully as my appllication polls the database afetr some specified amount odf time. "Error : No. :3709 :The requested operation requires an OLE DB Session object, which is not...
8
13256
by: mark_aok | last post by:
Hi all, I have a split database. Both the forms, and the tables are stored on a shared network drive (this is Access 2003). The users use the forms, and the tables on the network drive, there are no local copies. When connection to this drive is lost, Access CRASHES. It does it every single time. Does anyone know if there is a way to check if
4
1537
by: Marine | last post by:
Hi, My project must connect to a Sql Server quite often to read/write datas. I am wondering : do i have to open the connection to the Sql Server at the beginning when the application loads, then leave the connection open and close it when the application closes ? Or is it more efficient toopen and close the connection each time i have to use a datareader, a command etc... Thanks !
0
9589
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
9423
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,...
1
9996
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
9865
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
8872
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
7410
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
5304
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...
1
3963
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
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.