?
Solved

Dim a variable accross all my ASP.NET pages?

Posted on 2012-03-09
2
Medium Priority
?
380 Views
Last Modified: 2012-03-09
I have an ASP.NET application with about 20 webforms.

I created Class1.vb, and in this class I have this:

    Public Shared MSSQLCommand As Data.SqlClient.SqlCommand
    Public Shared MSSQLConnection As New Data.SqlClient.SqlConnection

And in all of my webforms, I have this code:
Class1.MSSQLConnection.ConnectionString = ConfigurationManager.ConnectionStrings("DataConnection").ConnectionString

This is causing all kinds of problems, giving errors saying a "connection is string is open" when multiple users access the pages at the same time.

The only other thing I know to do is delcare MSSQLConnection as private on all of my forms.  I'd much rather delcare them once and reference them.  Is there another way I can do this?   I like having code that uses as little repetition as possible...it sems that if I add a
"Private MSSQLConnection as New Data.SqlClient.SqlConnection" at the top of all of my web forms, that I'm being inefficient...or is that the only way to do this?
0
Comment
Question by:arthurh88
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 37702945
You generally don't want to leave a connection open for longer than it has to be, and sharing one connection in an ASP.NET application is sure to be troublesome--each request to your page will end up in its own thread; you can end up with synchronization issues. What is the reason you wouldn't want to declare a connection object on each page?

Also, have you considered an n-tier architecture. You can have a "data access layer" which communicates with your database. In this way, you define a contract with your UI and your DAL, and the method names defined by your DAL won't change--which means your UI doesn't have to change if you ever move your backend database to a different platform. You can create connection objects in each method which accesses the database, and you shouldn't encounter synchronization issues.
0
 

Author Closing Comment

by:arthurh88
ID: 37703928
makes sense!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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