473,320 Members | 2,083 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,320 software developers and data experts.

Best practice for using connection throughout page?

When I drop a SqlConnection on the web form designer, it's created in
InitializeComponent. I'd like to use this connection in my Page_Load
method, but I don't see anywhere that it's opened. Do I need to open
it myself? If so, should I close it also, or will I be interfering
with databound controls that use it? If I want to use it in PostBack
methods should I re-open it, or leave it open in Page_Load?

It seems that I would like to have a connection open for the duration
of the page, but I don't know any way to do that while guaranteeing
that it gets closed in case of an error. What is the best practice for
using a connection in Page_Load, PostBacks, and databound controls?

Mar 28 '06 #1
2 1243

<je*********@gmail.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...
When I drop a SqlConnection on the web form designer, it's created in
InitializeComponent. I'd like to use this connection in my Page_Load
method, but I don't see anywhere that it's opened. Do I need to open
it myself? If so, should I close it also, or will I be interfering
with databound controls that use it? If I want to use it in PostBack
methods should I re-open it, or leave it open in Page_Load?

It seems that I would like to have a connection open for the duration
of the page, but I don't know any way to do that while guaranteeing
that it gets closed in case of an error. What is the best practice for
using a connection in Page_Load, PostBacks, and databound controls?


In most opinions, it is not wise to use a "global" connection object.
Create the object, open it, use it, close it. If done correctly, the
connection will be pooled and can quickly be created/opened again (by
retrieving the object from the pool automatically) and re-used. In this
case, you would just need to make sure the object is closed after an error
by:

' Create connection.
Dim conn As SqlConnection = New SqlConnection(...)
conn.Open()
Try
... fill dataset, update database, or something else ...
Finally
conn.Close()
End Try

' Use data retrieved from the database. Connection is no longer needed
here.

HTH,
Mythran

Mar 28 '06 #2
There's not much advantage in trying to keep the connection open for the
life of the page, and NEVER try to keep it open across page requests.
ASP.NET's efficient built-in connection pooling makes this kind of thing
unnecessary. The rule of thumb is to open a connection just before you need
it, and close it as soon as you've retrieved the data you need.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
<je*********@gmail.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...
When I drop a SqlConnection on the web form designer, it's created in
InitializeComponent. I'd like to use this connection in my Page_Load
method, but I don't see anywhere that it's opened. Do I need to open
it myself? If so, should I close it also, or will I be interfering
with databound controls that use it? If I want to use it in PostBack
methods should I re-open it, or leave it open in Page_Load?

It seems that I would like to have a connection open for the duration
of the page, but I don't know any way to do that while guaranteeing
that it gets closed in case of an error. What is the best practice for
using a connection in Page_Load, PostBacks, and databound controls?

Mar 28 '06 #3

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

Similar topics

5
by: John Pastrovick | last post by:
I am not sure what the most effective way to organize a db for users is. I have 40 users (teachers) and 20 tables (grades, assignments, students, etc). Each needs access to its OWN grades,...
2
by: John Pastrovick | last post by:
I am not sure what the most effective way to organize a db for users is. I have 40 users (teachers) and 20 tables (grades, assignments, students, etc). Each needs access to its OWN grades,...
131
by: Peter Foti | last post by:
Simple question... which is better to use for defining font sizes and why? px and em seem to be the leading candidates. I know what the general answer is going to be, but I'm hoping to ultimately...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
11
by: Bob | last post by:
In our new .NET web applications, we try to limit the use of SqlConnection to just one instance per page, even if there are multiple accesses to various queries. The thinking behind is that this...
2
by: davihigh | last post by:
Hello all, I am looking for a neat and best practice way to layout a number of items (say, no more than 16 records) in one page, fill in a 4x4 grid. The most clear way is using <tableto display...
51
by: bigHairy | last post by:
Hello. I have been teaching myself .NET over the last few months and have had some success. I would like to ask a question though... A number of examples I have followed have the following in...
13
by: Justin.Voelker | last post by:
Hello Everyone: I am in search of an easier way to develop pages. My most current website, www.Base2WebDesign.com, has the exact same layout throughout the entire site. Right now I use a...
3
by: at_the_gonq | last post by:
Hello, I am hoping to get some guidance on the following scenerio: I have a password protected site where users have various permissions. Are sessions the best way of storing the user's id? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.