GetConnection() method in Global.asax

Posted on 2004-11-15
Medium Priority
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
  • 2
  • 2
  • 2
  • +1
LVL 24

Accepted Solution

Jeff Certain earned 600 total points
ID: 12588275
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

MikeMCSD earned 1000 total points
ID: 12588732
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
ID: 12588766
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.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Assisted Solution

by:Fred Goodwin
Fred Goodwin earned 200 total points
ID: 12590584
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

Author Comment

ID: 12590656
.net 2.0 is coming out in January then?

Expert Comment

by:Fred Goodwin
ID: 12590683
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

Author Comment

ID: 12590741
oh, i see.. thanks for the info

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month16 days, 20 hours left to enroll

862 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