title: ASP.NET Connection String
body: <p>I am having some trouble with one of my ASP.NET 2.0 application's connection string.  There are instances that I would get a <em>ConnectionString Property Has not Been Initialized</em> problem which occurs randomly and intermittently and without any reason.</p>
  
  <p>My Connection string is actually coming from a webservice, because different kinds of users are provided with different sets of connection string depending on their user level.</p>
  
  <p>What I have done so far goes like this:</p>
  
  <p>I have a master page (<code>mstr.page</code>) and the corresponding code behind (<code>mstr.page.vb</code>).</p>
  
  <p>In my master page, I retrieve initially the connection string and store the same into a session variable, i.e.</p>
  
  <pre><code>Session("ConnString") = "RetrievedConnectionString"
  </code></pre>
  
  <p>Now in one of my pages, let us say <code>page1.aspx.vb</code>, I use public shared functions from a class (named <code>MyClass.vb</code>) and use it in my <code>page1.aspx.vb</code>.</p>
  
  <p>Some codes for reference:</p>
  
  <pre><code>[MyClass.vb]
  Imports System.Web
  
  NameSpace ClassNameSpace
    Public Class Admin
      Protected Shared da as New DataAccess()
  
      Public Shared Function MYFunction() as String
        'Execute some sql statements here, using the DataAccess
        stringToReturn = Ctype(da.ExecuteScalar("SQLCommand"), String)
        Return stringToReturn
      End Function
    End Class
  End NameSpace
  
  [DataAccessClass.vb]
  Public Class DataAccess()
    Private m_ConStr As String = ""
  
    Public Sub New()
      m_ConStr = HttpContext.Current.Session("ConnString")
    End Sub
  
    'Some methods for SQL statement execution (ExecuteQuery, ExecuteScalar)
  End Class
  
  [Page1.aspx.vb]
  Imports ClassNameSpace
  
  Protected Sub Page_Load(....) Handles Me.Load
    Dim strValue as String = Admin.MyFunction()
  End Sub
  </code></pre>
  
  <p>I have placed the code above to show you some rough idea of how things are going.</p>
  
  <p>Basically, the function <code>Admin.MyFunction()</code> at times fails, because in the data access class, the connection string seems to have lost it's value (either blank or Nothing).</p>
  
  <p>This has troubled me for quite some time already.  </p>
  
  <p>I hope someone can point me in the right direction to resolve this.  Basically, I want my connection string which is retrieved by each user visiting the web application be maintained across all the time and be used anywhere.  Session variable does not seem to be the best fit since when the ASP.NET recycles its process, the session is lost.</p>
  
  <p>By the way, I am retrieving the connectionstring initially via the master page from a web service.  I tried to place the same retrieve function in the Data Access class when conditions is that the session variable is lost, but I think my application cannot connect to the Web Service during the recycle process.</p>
  
  <p>Any advice would be greatly appreciated on this.</p>
  
  <p><em>Update on this:</em></p>
  
  <p>I tried to use Session Variable and set the mode to State Server, but apparently some DLLs which I am using cannot be serialized, thus I am back to square one.</p>
  
  <p>Is there a better way to do this?</p>