Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 19th, 2003, 03:19 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 141
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL Access

We have a SQL Server where our data resides. All access from the ASP.NET pages will involve Stored Procedures. I want to maintain as much of the connection and redundant stuff associated with a database connection in one place.

I have selected to use a User control. Maybe I'll create a class in there that has a method to open the connection and another method to close the connection.

First of all, is this the best way for me to do this, and second, how would I access the connection object? Do I have a property that returns the connection object? I am so new to the .NET that I really have no experience with it. I have almost read all the way through the Beginning ASP.Net with VB.Net Wrox book. I am trying to get the basics of the site set up before I put a lot of work on web pages and then decide I want to move a bunch of the functionality out of the page.

With .NET removing include files, I am not sure how to have 1 header file with all header information in it like CSS and stuff (so far it seems .NET just does not allow this great functionality allowed in ASP 3.0.

Does anyone else have any design tips that would be good for me to know when setting up the initial framework of the site?

Chris
__________________
Chris
 
Old August 19th, 2003, 05:20 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Chris,

In the old days the rule for DB connections was "Open Late, Close Early". This rule really still applies when building desktop/client applications. With regards to web applications however everything is happening in one fell swoop so it's kind of silly to implement that strategy when doing multiple calls to the same data store. From several database driven site implementations, I can offer the following suggestion as a feasible option.

My method consisted of having pages that all used common functionality (in ASP it was includes) which prepared and opened the database connection and closed it at the beginning and end (respectively) of page execution. This minimized the number of opens and closes of the DB connection down to just a single instance of each. I have working proof that this method works quite well. All your pages do is use the existing DB connection to execute their calls. The site framework (or "skeleton" as I have come to call it to not conflict with .Net's "Framework") does all the work involved with connecting to the DB. This saves a tremendous amount of code. This technique works in certain circumstances, but you need to keep a wary eye on how you use it. If you create a utility class that has data control functions in it, you may find that you need to add method parameters or properties to them to provide a reference to the DBConnection object because those classes won't necessarily always be running within the context of an executing page. This becomes even more apparent when you start building shared functions in generic class libraries. Adjusting for this is not difficult, you just have to be wary of it.

So anyway, here's something more tangible...

In the ASP.Net paradigm, building pages from a skeleton becomes much easier than with classic ASP. Here's my idea simplified: You need to create a base page class that all your aspx's will inherit from.


[u]BasePage.vb</u>
Code:
Public Class BasePage
    Inherits System.Web.UI.Page

    Private m_objDBConn As _
      New System.Data.SqlClient.SqlConnection

    Public ReadOnly Property DBConn() As _
      System.Data.SqlClient.SqlConnection
        Get
            Return m_objDBConn
        End Get
    End Property

    Private Sub Page_Load(ByVal sender As Object, _
      ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: Do all your setup for m_objDBConn here
    End Sub

    Private Sub Page_Unload(ByVal sender As Object, _
      ByVal e As System.EventArgs) Handles MyBase.Unload
        'TODO: Close and cleanup m_objDBConn here
    End Sub

End Class
Now, in all your aspx code behind files you change their inherits statement from
Code:
    Inherits System.Web.UI.Page
to
Code:
    Inherits BasePage
and voila, you can now access the connection object...

Code:
    Me.DBConn
Before you say "But wait a minute... what if I have user controls on my pages that need the DB?!" take a look at this:


[u]BaseUserControl.vb</u>
Code:
Public Class BaseUserControl
    Inherits System.Web.UI.UserControl

    Private m_objDBConn As _
      New System.Data.SqlClient.SqlConnection

    Public ReadOnly Property DBConn() As _
      System.Data.SqlClient.SqlConnection
        Get
            Return m_objDBConn
        End Get
    End Property

    Private Sub Page_Load(ByVal sender As Object, _
      ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Me.Page Is Nothing Then
            m_objDBConn = CType(Me.Page, BasePage).DBConn
        End If
    End Sub

End Class
Now, all your user controls inherit from this class instead of System.Web.UI.UserControl. ** Note that this works only if the user controls inheriting from BaseUserControl are put onto pages that inherit from BasePage. Usually this is not a problem.

Now just like in the page codebehind, you can access the DB connection object with "Me.DBConn".

You can extend this technique to work with loads of common properties that you use site wide. The great part of something like this is that you can strongly type data that gets managed thru the viewstate, cookie or the session objects. The base page class loads then all up and populates the properties, and your calling code never has to worry about where the data comes from or where it needs to go to. If you build all your common properties/objects into a little class and make an instance of that class a member of the base page, then it gets even easier cause all you need to know is what the basepage property is. I use "Common". So in your code, you can just call "Me.Common." and you get all your common "stuff". Using the method above, you can make the common property a member of both the page and user control base classes and tie them together. When you add common properties to your common class, all pages/controls are updated.

I have implemented several variations of the example above and would be happy to share more details and ideas with people. Also, if you have any criticism/comments/suggestions or anything else regarding my examples, please let me know. I'm always looking for improvements.

Good luck!

Peter
 
Old August 20th, 2003, 10:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 141
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think this looks great. I cannot say if it is the best method or not because I simply do not know. I like the idea of having code in 1 place so when I change it, the entire site changes. The more I read your response.the more excited I get.

Do I just leave the new BasePage and BaseUserControl code in the .vb files? Do I need to compile them into the assembly? Once I get the initial setup done, it will be great. I want to have this "common" as you mentioned and be able to add to it whenever I want. I'd rather not have to recompile it into the assembly if I do not have to, but I do not understand everything about the setup that well yet.

Thanks a ton for your reply. The things I have learned from it already will be greatly helpful.

Chris
 
Old August 20th, 2003, 11:22 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Put the two base class .vb files in your web project alongside all the other files (aspx/ascx etc). They will become part of the web application assembly just like all the webform/usercontrol code behind files. Create a separate file for each.

Here's an option for what you can do with the "Common" piece:

Inside the base page class define the "Common" class and set up a private instance and public property for it. This will live next to your db connection members inside the base page class. And if you wanted to, you could actually make the DBConnection member part of "Common" so it's all together. I'd recommend doing it that way. This make it's particularly easy if someone else starts to add to the code. All they need to know is that they can type "Me.Common" and they'll be able to see what they need.
Code:
Public Class BasePage
    Inherits System.Web.UI.Page

    Public Class CommonProperties
        Private m_nSomeIntegerProperty
        Public Property SomeIntegerProperty() As Integer
            Get
                Return m_nSomeIntegerProperty
            End Get
            Set(Value As Integer)
                m_nSomeIntegerProperty = Value
            End Set
        End Property
    End Class

    Private m_objCommon As New CommonProperties()

    Public ReadOnly Property Common() As CommonProperties
        Get
            Return m_objCommon
        End Get
    End Property

End Class
In case you were wondering, the reason for the DBconnection and Common members to be readonly is that they are objects and there is no reason that any inheriting page should need to overwrite the object instances themselves. However, you still have complete access to the readonly objects' members.

There's no limit as to how you can set this up. In one site I built my "Common" member is further divided into specific types of properties. I have a Common.Settings which are used for page layout settings (ShowThisHeader = true, ShowThatNav = false, HelpText = "Click this button") and Common.Data for items that utilize state management (ClientID, UserID). Like I mentioned in the previous post, using this technique for state managed data is great because it a)strictly types data that normally isn't (coming from name/value pair collections like the session) and b)completely eliminates the need to remember how to deal with the specific bits of data in the code that's actually using it. In my case, my state managed data is handled thru the session object. The property functions (Get,Set) deal with retrieving and setting the values in and out of the session object. The calling code simply Gets/Sets the properties as they are.

Peter






Similar Threads
Thread Thread Starter Forum Replies Last Post
convert a SQL Statement from MS Access to a SQL Corey Access 6 March 28th, 2007 12:33 PM
SQL Access/ASP.NET data access issue saeta57 SQL Server ASP 1 July 4th, 2004 04:29 PM
SQL Access/ASP.NET data access issue saeta57 Classic ASP Databases 1 July 4th, 2004 03:32 PM
Access SQL danielwajnberg Access ASP 1 September 26th, 2003 05:58 PM
SQL in Access and in SQL Server mikko SQL Server 2000 1 July 29th, 2003 06:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.