views:

1330

answers:

5

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 ConnectionString Property Has not Been Initialized problem which occurs randomly and intermittently and without any reason.

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.

What I have done so far goes like this:

I have a master page (mstr.page) and the corresponding code behind (mstr.page.vb).

In my master page, I retrieve initially the connection string and store the same into a session variable, i.e.

Session("ConnString") = "RetrievedConnectionString"

Now in one of my pages, let us say page1.aspx.vb, I use public shared functions from a class (named MyClass.vb) and use it in my page1.aspx.vb.

Some codes for reference:

[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

I have placed the code above to show you some rough idea of how things are going.

Basically, the function Admin.MyFunction() at times fails, because in the data access class, the connection string seems to have lost it's value (either blank or Nothing).

This has troubled me for quite some time already.

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.

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.

Any advice would be greatly appreciated on this.

Update on this:

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.

Is there a better way to do this?

+1  A: 

One thing to check is if your Session is getting clobbered. If your using the (default) in-memory Session, then sessions die anytime an ASP.NET worker process is recycled. If this is causing your issue, you might have to look into using the ASP.NET SessionServer in IIS, or SQL Server as your Session storage.

Harper Shelby
I do check the session variable prior to getting the value for the connection string. When it is Null or Nothing or Empty, what I do is try to retrieve the connection string again from the Web Service, but since the recycle is ongoing, the web service cannot also be accessed.
Batuta
A: 

This is the exact same problem I was experiencing, and it turned out to be the session variables dying so the connection string couldn't be initialized properly.

Jeremy Reagan
So what solution did you do? Thanks.
Batuta
I increased my session timeout and made sure in the web.config that the session mode was set to InProc. Seems to have worked thus far. Probably a better way out there to do it as I am not even a very good ASP.Net programmer.
Jeremy Reagan
I also made sure to initialize the connection string on every page, that way it was never not initialized.
Jeremy Reagan
But if I tried to initialize it on every page, then it is resource consuming because the connection string is coming from a web service.Also, I am using In Proc right now. Have not checked with State Server mode yet.
Batuta
A: 

Why cant your app use just one connection string? and why does the connection string need to come in through a webservice? that adds a huge amount of latency to the entire process. I assume it probably has to do something with data security in your database.

If you have to do this, I'd say, can't you have a fall back / default connectionstring? Wrap your code that attempts to pull it out of the session with some error handeling and if it fails revert to your default?

BPAndrew
Some stup*d guy actually proposed this kind of solution and we got no choice but to actually get the connection string from a web service.The connection string is entirely a different set of complexity, where one user may have as much as 10 different connection string based on the pages accessed.
Batuta
Sometimes the ideal solution is to reverse a bad decision made earlier on. Programming (and life!) is easy when you make the right decisions at the right time.
BPAndrew
A: 

I would place your web service and web app in different app pools. Then increase the timeout length of your web apps pool.

dr
Web Service and Web App pool are already on different machine. Timeout has also been increased but when the ASP WP recycles, Connection String is lost.
Batuta
+1  A: 

I'd try to limit your use of the Session for this type of thing as much as possible. If you utilize the Web.Config for storing your connection strings, you can access it at anytime and it will not expire on you like the session.

You may also consider having a static data access class if it is the same for all users for the applications instance...

RSolberg
As I have mentioned, each user has their own specific connection string property which is served by a web service. It is not possible to hard code the connection string in the web.config for this reason.
Batuta
What differences are there in the connection strings? If you have a standard connection string, you can always modify it for the user and then throw it away...
RSolberg
I like that approach of having a base/standard connection string and modifying it if needed
BPAndrew