User Controls and ADO Connections

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have 3 User Control .ascx files that are included in a base .aspx file.

In each ascx file, as well as the base aspx file, I make ADO.Net calls that
require a datatbase connection.

It appears that I have to open and close the connection in each file (i.e. I
can not reuse the open connection between all 4 pages)? This means that I
must open and close the connection 4 times when servering one page (base aspx
and 3 ascx files).

Is there any way that I can accomplish this without so opening and closing
the connection so many times?

Wouldn't this become a scalability problem if I continually have to open
and close connections multiple times on each page?
 
Paul,

Why do you do this database handling in your controls

In my opinion should you seperate that, just create a seperate component for
it.

Open that as new Item in your solution explorer and drag by instance from
your toolbox a connection for that.

This is all the information I can reply to you, given the information you
have given to us.

I hope this helps anyway.

Cor
 
Paul,

Just the opposite. It will become a scalabiliy problem if you don't open and
close connections each time you use them.

Plus, with connection pooling, one connection is getting reused several
times behind the scenes.

Kerry Moorman
 
Kerry,
Just the opposite. It will become a scalabiliy problem if you don't open
and
close connections each time you use them.
However, if you have two controls on your form which are using the same part
of the data, than it is in my opinion a waste of time to get them 2 times.

Maybe do I misunderstand the problem.

Cor
 
Cor,

Yes, getting the data from the database once is the way to go.

However, if for whatever reason, the data is retrieved more than once, then
a connection needs to be opened and closed for each database access.

Hanging on to an open connection so that it can be used repeatedly is the
scalability problem that I meant.

Kerry Moorman
 
Kerry,
However, if for whatever reason, the data is retrieved more than once,
then
a connection needs to be opened and closed for each database access.

Hanging on to an open connection so that it can be used repeatedly is the
scalability problem that I meant.

You will never find it written in another way here by me. (With the
exception from an Access Database, in that you can use it to lock the Access
Database on its place)

I knew that you did mean that, my message was to overcome confusions by the
OP.

Cor
 
The reason that I do database calls in the UserControl is because I need to
display data (retrieved from the database) in each of those controls.

I do not understand what you mean by create a seperate component?
 
I understand the importance of closing database connections.

What I am struggling with is the need to open and close them multiple times
(within multiple UserControls) on the same page.

Maybe it is because in Classic ASP, I used to use include files where I
would open the connection once at the beginning of the base page, then make
database calls in multiple include files (not having to reopen or close the
connection), then close the connection at the end of the base page.

So I only had to open/close the connection once per page.

I have read that opening/closing the db connection is an "expensive"
transaction that should not be done often (only once per page). But now I am
doing it multiple times per page and that concerns me.

It comes down to the difference be Classic ASP include files to user
controls I guess.
 
Paul,

You can of course as well use for your data by instance a dataset/datatable.

Than you can get the data by making in your usercontrol a property for that
datatable as what it is everywhere DataSource.

Just a thought,

Cor
 
Back
Top