Dim a variable accross all my ASP.NET pages?
Posted on 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?