473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

When to close connection?

I have a function that returns a SqlDataReader created by:

result = command.ExecuteReader(CommandBehavior.CloseConnect ion);

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
the DataBind?

Thanks,
Bill

Oct 19 '05 #1
4 1563
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.ExecuteReader(CommandBehavior.CloseConnect ion);

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
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.ExecuteReader(CommandBehavior.CloseConnect ion);

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
the DataBind?

Thanks,
Bill

Oct 19 '05 #3
This is a sample

Try

DR = db.ExecuteReader(whatever)

While DR.Read

MsgBox(DR.Item(0))

End While

Catch ex As Exception

MsgBox(ex.Message)

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.ExecuteReader(CommandBehavior.CloseConnect ion);

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
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***@discussions.microsoft.com> schreef in bericht
news:2D**********************************@microsof t.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.ExecuteReader(CommandBehavior.CloseConnect ion);
>
> 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
> 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
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...
7
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...
2
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...
4
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 =...
1
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...
2
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....
8
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...
4
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.