473,320 Members | 2,094 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.

Custom Page member

Hi!
The situation is:
1. In Page_Load, I open a database connection which is closed on handler
finish.
2. In Button_Click, I do same thing again as above.

So during one request, a db connection opens/closes 2 times. I think this is
not normally
and now I added to my page class a 'private SqlConnection MyConn'. Both
above 2 handlers
check whether this field is null and creates/opens the connection. Am I
doing right?
This is a standard technique of ASP.NET for such situations?
Also, what event occurs early, Page_Load or Button_Click?

Thanks!
Nov 19 '05 #1
3 1076
Actually, you're probably better off doing it the first way you
describe. Database connections work best if you open and close them in
a single block of code. Ideally, wrapped in a using(){} block.

If you try to manage a private connection, you run the risk of leaving
it open or referenced by something that stays alive longer that you
expected. This leads to leaky code (which is still very possible in
C#), which will cause you much more trouble than the occasional page
opening and closing two connections to the database.

Good luck!

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

Nov 19 '05 #2
After debugging my first ASP.NET app, I found
that Page.Load event occurs before Button_Click and
Page.Unload raised at the end. So I placed a connection
close code within Unload. But can I be sure that Page.Unload
will be called in any situation?
"jasonkester" <ja*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Actually, you're probably better off doing it the first way you
describe. Database connections work best if you open and close them in
a single block of code. Ideally, wrapped in a using(){} block.

If you try to manage a private connection, you run the risk of leaving
it open or referenced by something that stays alive longer that you
expected. This leads to leaky code (which is still very possible in
C#), which will cause you much more trouble than the occasional page
opening and closing two connections to the database.

Good luck!

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

Nov 19 '05 #3
In theory.

All I can tell you is that I've spent loads of time tracking down
ConnectionPool exceptions in other people's code. There's a dozen ways
for your connections to get left open if you're not on top of them. In
a web app that you expect to have running for weeks at a time, they'll
pile up on you and you'll start seeing strange exceptions.

In my code, database connections and transactions always live in a
using block, and open/close statements are always within a few lines of
eachother. I've never had any leaks from this practice.
Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

Nov 19 '05 #4

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

Similar topics

7
by: Luc Tremblay | last post by:
Given the typical following code: void Listener::HandleEvent(const Event& event) { // handling code } In a "clean" fashion, how is it possible to add custom data (to be subsequently...
0
by: Wim Hoogenraad | last post by:
Dear Webprofessional, In this newsletter we like to inform you about some developments on our website. First of all we would like to thank you who joined WebmastersLookup. Without the...
1
by: Tom L | last post by:
I'm pretty new to .net stuff, but not new to programming (lots of vb, sql, cold fusion, and some asp).. I have a "middle layer" object model I created, that has all my business rules and database...
3
by: Mr Newbie | last post by:
I'm testing error handling configurations and having some trouble. I created a WebForm called. ErrDefault.aspx and I am trying to use the Page error attribute to force the redirection to a custom...
3
by: Adam J Knight | last post by:
Hi all, I am in the process of creating a custom membership provider (ASP.NET 2.0): Currently I am inheriting from MembershipProvider which isn't a problem. However, the...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
5
by: Ben R. | last post by:
My website uses a custome membership and role provider. I can use a custom login control and user creation control and can debug my providers while doing so with breakpoints. However, when I launch...
1
by: Jakob Lithner | last post by:
When I started a new ASP project I was eager to use the login facilities offered in Framework 2.0/VS 2005. I wanted: - A custom principal that could hold my integer UserID from the database -...
0
by: Tim | last post by:
have a 1.1 framework site that is being converted to 2.0. I have a custom class MyPage that I'm trying to have all of my .Net pages inherit. My site uses both classic and .Net sessions so I...
0
by: Ravi Kumar | last post by:
hi :) I was trying to develop a custom mod_python based web-site, just today. the problem I got though i liked the mod_python's feature of mapping and calling functions in python script by...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
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...
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...
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)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.