Since SqlConnection
is a reference type, when you assign conn
in the using statement the actual global connection is used and not a copy.
When the using scope ends Disposed
is called on the global connection and it becomes useless.
You should not use a global connection like this for thread safety issues (among others).
I would recommend either creating a new connection in the using statement with the connection string from the global class, or calling a method in the global class that creates the connection.
Public Class ReqGlobal
private shared _cnnStr As string = "someConnectionString"
Public Shared Function GetConnection() As DbConnection
Dim con = new SqlClient.SqlConnection(_cnnStr)
con.Open()
return con
End Function
End Class
Usage
Using conn As DbConnection = ReqGlobal.GetConnection()
...
End Using