GetConnection() method in Global.asax


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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeff CertainCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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)
Jeff CertainCommented:
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.
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Fred GoodwinVP of Software DevelopmentCommented:
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
trevorhartmanAuthor Commented:
.net 2.0 is coming out in January then?
Fred GoodwinVP of Software DevelopmentCommented:
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
trevorhartmanAuthor Commented:
oh, i see.. thanks for the info
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.