• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

Dim a variable accross all my ASP.NET pages?

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
arthurh88
Asked:
arthurh88
1 Solution
 
käµfm³d 👽Commented:
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
 
arthurh88Author Commented:
makes sense!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now