Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I recently had to add another database connection to my site to get the session values working correctly. What had happened is that the user info my sessions display is held in 2 different databases. I have connection Strings for both databases in my web.config file but until recently, I didn't know that sessions weren't stored unless I had declared <sessionstate> and <httpmodules>.

How do I declare <sessionstate> for a 2nd database? I tried adding a 2nd <sessionstate> in my web.config and my entire site came back with a 500 Internal Error. Below is what I have now, but I need another connection string for sessionState as well. I have also tried just adding a comma after the IBConnectionString and typing in the other connection string, but that didn't work.

HTML
<sessionState mode="InProc" cookieless="false" timeout="20" sqlConnectionString="IBConnectionString">
</sessionState>
<httpModules>
  <add name="Session" type="System.Web.SessionState.SessionStateModule"/>
</httpModules>


This is my master page's code behind function that sets the session variables and the OnClick from the textbox in which the user enters their code which will then set up the Session. The main session is Session("BAccount"), having this set will then pull the appropriate information from the database in which that user's name is stored.

VB
Protected Sub IBTextBoxButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles IBTextBoxButton.Click
    If Page.IsValid Then
        'declare variables
        Dim LSD As String = CType(Session.Item("LSD"), String)
        Dim LSC As String = CType(Session.Item("LSC"), String)
        Dim BAccount As String = CType(Session.Item("BAccount"), String)
        Session("BAccount") = IBTextBox.Text
        'add session variable
        If GetCompanyName(LSD) Then
            Session("LSD") = LSD
        Else
            'no data found
        End If
        'add session variable
        If GetWebsite(LSC) Then
            Session("LSC") = LSC
        Else
            'no data found
        End If
    End If
End Sub

Protected Function GetSessionValues(ByVal Code As String) As Boolean
    Dim FirstName As String = CType(Session.Item("First_Name"), String)
    Dim LastName As String = CType(Session.Item("Last_Name"), String)
    Dim Name As String = CType(Session.Item("Name"), String)
    If GetAccountName(Code, FirstName, LastName) Then
        'hide textbox
        IBText.Visible = False
        IBTextBox.Visible = False
        IBTextBoxButton.Visible = False
        'show welcome message to user if IB code exists in database
        lblIB.Visible = True
        lblIB.Text = "Welcome, " + Session("First_Name") + " " + Session("Last_Name") + "."
        lbNotIB.Visible = True
        lbNotIB.Text = "Not " + Session("First_Name") + " " + Session("Last_Name") + "?"
        Return True
    ElseIf GetBackUpAccountName(Code, Name) Then
        'hide textbox
        IBText.Visible = False
        IBTextBox.Visible = False
        IBTextBoxButton.Visible = False
        'show welcome message to user if IB code exists in database
        lblIB.Visible = True
        lblIB.Text = "Welcome, " + Session("Name") + "."
        lbNotIB.Visible = True
        lbNotIB.Text = "Not " + Session("Name") + "?"
        Return True
    Else
        'IB code not found
        'shows error message in red
        lblIB.ForeColor = Drawing.Color.Red
        lblIB.Text = "Account not found, please try again."
        Return False
    End If
End Function
Posted
Updated 11-Apr-12 8:48am
v5
Comments
Anuj Banka 11-Apr-12 12:08pm    
Just tell me what you want to do?
Means what you want to achieve with session.
jamielynn 11-Apr-12 12:56pm    
In the site, a user enters a code (associated with their account) and a welcome message appears with their name. But since there are 2 databases with their user account code and name, I need to be able to get the 2nd database to keep the session. The session is the code, the name is pulled from 2 databases.

1 solution

You have set mode="InProc" so your session is not stored in database.
If you want to store session in database set mode="SQLServer" and provide valid sqlConnectionString.
You can not store session in two different databases.

I think you're confusing session and data access.
 
Share this answer
 
Comments
jamielynn 11-Apr-12 14:20pm    
I'm not trying to store the session in the database. I am retrieving user information from 2 databases and putting them into sessions. I updated the original post with more code, hopefully that helps describe the situation better.
sjelen 12-Apr-12 10:24am    
Where exactly is the problem in the code you posted?
You don't need (and can't have) 2nd sessionState to work with 2 databases.
You only need to declare 2 connection strings in web.config, one for each database you use.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900