"Binding" objects to the top level Page class

Discussion in 'ASP .Net' started by Davíð Þórisson, Oct 25, 2004.

  1. asking a lot these days... how would I ever learn .Net without the
    newsgroups!! Well I'm stuck on yet another point. All pages on my web have a
    user control (initialise) that handles for example database connection;
    opens db on page_onload and closes db on page_unload. But so that the
    connection object is as easily accessible from all subpages and other user
    controls I'd like to somehow "bind" it to the top level Page class.
    Otherwise I foresee problems with accessing the db object from sub-codes.
    Can anyone please tell me how do to this kind of thing?? Or should I maybe
    use some other way?
     
    Davíð Þórisson, Oct 25, 2004
    #1
    1. Advertising

  2. Davíð Þórisson

    Karl Seguin Guest

    I think most people would tell you two answers:

    First, the typical way to achieve what you are trying to do is to use a
    comon base-page for all of your pages:

    public class MyBasePage
    inherits System.Web.UI.Page

    private _connection as SqlConnection
    public property Connection as SqlConnection
    get
    return _connection
    end get
    set
    _connection = value
    end set
    end property


    sub page_load
    _connection = new Sql...
    end sub



    and have all your pages inherit from it:

    public class WebForm1
    Inherits MyBasePage



    But the other thing people would tell you is that you shouldn't be using
    connections like this. There are a couple good reasons. First of all,
    codebehind is a presentation logic layer, and it shouldn't do anything with
    System.Data.SqlClient (some people, like me would say it shouldn't do
    anything with the complete System.Data namespace, but that's for another
    day). You should be using a data access layer through a business layer for
    all database connection stuff. Secondly, even if you ignore the first piece
    of advice, you shouldn't be opening a single connection and closing it at
    the end of the request. You should open connections as late as you need
    them and close them as soon as you are done with them. Thanks to connection
    pooling it's much better to open and close connections 20 times in a page's
    life, than to hold one open at the start and close it at the end...

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Davíð Þórisson" <> wrote in message
    news:...
    > asking a lot these days... how would I ever learn .Net without the
    > newsgroups!! Well I'm stuck on yet another point. All pages on my web have

    a
    > user control (initialise) that handles for example database connection;
    > opens db on page_onload and closes db on page_unload. But so that the
    > connection object is as easily accessible from all subpages and other user
    > controls I'd like to somehow "bind" it to the top level Page class.
    > Otherwise I foresee problems with accessing the db object from sub-codes.
    > Can anyone please tell me how do to this kind of thing?? Or should I maybe
    > use some other way?
    >
    >
     
    Karl Seguin, Oct 25, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Siegl
    Replies:
    1
    Views:
    426
    David Hilsee
    Aug 19, 2004
  2. pabbu
    Replies:
    8
    Views:
    732
    Marc Boyer
    Nov 7, 2005
  3. musosdev
    Replies:
    5
    Views:
    357
    musosdev
    Sep 2, 2008
  4. JcFx
    Replies:
    0
    Views:
    280
  5. John L.
    Replies:
    6
    Views:
    918
    Thomas 'PointedEars' Lahn
    Sep 4, 2011
Loading...

Share This Page