Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Dim a variable accross all my ASP.NET pages?

Posted on 2012-03-09
2
Medium Priority
?
385 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

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

636 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