Solved

Dim a variable accross all my ASP.NET pages?

Posted on 2012-03-09
2
339 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
2 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
makes sense!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

10 Experts available now in Live!

Get 1:1 Help Now