GetConnection() method in Global.asax

Posted on 2004-11-15
Last Modified: 2012-08-13

I put a public SqlConnection GetConnection() method inside my Global.asax.  I was wondering if any of you had any hesitations about this, whether or not it is good practice, and why?  

Thx - Trevor
Question by:trevorhartman
    LVL 24

    Accepted Solution

    I do the same thing... this lets you set the connection string once for the entire application. Personally, I encrypt the connection string and store it in the registry, so that it's not sitting in my code in plain  text.
    LVL 16

    Assisted Solution

    I'm not sure you want to put the method in there . . . I just store the
    connection string in mine:

       <add key="ConnectionString"  

    Then I just get the value like this:

    Dim connection As SqlConnection = New SqlConnection  

    Or . .  if you are using a Class file that has a lot of Functions that use
    the same Database connection try this:

    I put this in the Class file:

    Private Shared ReadOnly Property connectionString() As String
                Return ConfigurationSettings.AppSettings("ConnectionString")
            End Get
    End Property

    I can then use the connectionString Property for all the Functions in the Class:

    Public Shared Function AddProduct . . . .
            Dim connection As New SqlConnection(connectionString)
            Dim command As New SqlCommand("AddProductToCart", connection)
    LVL 24

    Expert Comment

    by:Jeff Certain
    If you're using a database outside your domain (and can't use a trusted connection), then the connection string will have a user name and password included in it. Not the sort of thing you want readily available. If that information is ANYWHERE in your app, .Net is pretty easy to reverse compile and get the data from it.

    Again, you want to store that info in a relatively secure format, even if it's just in a registry key to amke it harder to get to.
    LVL 7

    Assisted Solution

    Or you can wait 2 months for the go live license and framework 2.0 and just put it in the web config and encrypt that section
    LVL 8

    Author Comment

    .net 2.0 is coming out in January then?
    LVL 7

    Expert Comment

    Beta 2 is with a go live license.  They are calling it beta but they are supporting it and they believe it to be stable
    LVL 8

    Author Comment

    oh, i see.. thanks for the info

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now