locked
Problem with Session in global.asax RRS feed

  • Question

  • User1335250097 posted

    Hi!

    I have the following code that trow NullReferenceExeption. As I understand it's happend because Session wasn't initialized yet. 

    How I can initialize Session value to avoid this error?
    Sub Application_AcquireRequestState(ByVal sender As Object, ByVal e As EventArgs)
    Dim cult As String
    If Not System.Web.HttpContext.Current.Session("Lang") Is Nothing Then
    cult = Convert.ToString(System.Web.HttpContext.Current.Session("Lang"))
    Dim cultureInfo As New System.Globalization.CultureInfo(cult)
    System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo
    System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo
            End If
    End Sub
    Thursday, September 1, 2011 3:58 AM

Answers

  • User1335250097 posted

    Guys, thanks eveyone. I solved it already like that:

    Sub Application_AcquireRequestState(ByVal sender As Object, ByVal e As EventArgs)
    If System.Web.HttpContext.Current.Session IsNot Nothing Then
    Dim l As String = Convert.ToString(Session("Culture"))
    If l IsNot Nothing Then
    Dim ci As CultureInfo = New CultureInfo(l)
    Thread.CurrentThread.CurrentUICulture = ci
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name)
    Else
    l = "ru-RU"
    Dim ci As CultureInfo = New CultureInfo(l)
    Thread.CurrentThread.CurrentUICulture = ci
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name)
    End If
    End If
    End Sub
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 1, 2011 8:32 AM

All replies

  • User601024962 posted

    AS you know that Session will not be intialized in this code then you will never get value of this session accept Null

    so dont place check for the session value and go ahead with your code part.

    and retrive the culture info here and use that here directly not from session.

    Thursday, September 1, 2011 4:14 AM
  • User1335250097 posted
    Sub Application_AcquireRequestState(ByVal sender As Object, ByVal e As EventArgs)
    Dim cult As String
    cult = Convert.ToString(System.Web.HttpContext.Current.Session("Lang"))
    Dim cultureInfo As New System.Globalization.CultureInfo(cult)
    System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo
    System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo
    End Sub
    
    
    Like that it's trowing execption as well. Should I initialize Session somewhere else?
    Thursday, September 1, 2011 4:17 AM
  • User601024962 posted

    you have somewhere coded for getting Cult value and you are placing that in Session["Lang"] there

    you can do that code here

    and instead of using Session["lang"] use that code.

    Thursday, September 1, 2011 5:27 AM
  • User1335250097 posted

    Ok, but how I can handle here ChangeSelectIndex from MasterPage control event?

    Thursday, September 1, 2011 5:29 AM
  • User601024962 posted

    post your code and if its different then this then start a new thread.

    Thursday, September 1, 2011 5:31 AM
  • User1335250097 posted

    This is how I'm trying to change culture:

    Protected Sub rblSelLang_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rblSelLang.SelectedIndexChanged
    If rblSelLang.SelectedItem.Value = "en-US" Then
    If Not Session("Lang") Is Nothing Then
    Session("Lang") = "en-US"
    End If
    ElseIf rblSelLang.SelectedItem.Value = "ru-RU" Then
    If Not Session("Lang") Is Nothing Then
    Session("Lang") = "ru-RU"
    End If
    End If
    End Sub
    
    
    This is how I handle that:
    Sub Application_AcquireRequestState(ByVal sender As Object, ByVal e As EventArgs)
    If Not System.Web.HttpContext.Current.Session("Lang") Is Nothing Then
    Dim cult As String
    cult = Convert.ToString(System.Web.HttpContext.Current.Session("Lang"))
    Dim cultureInfo As New System.Globalization.CultureInfo(cult)
    System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo
    System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo
    End Sub
    Thursday, September 1, 2011 5:36 AM
  • User2045978403 posted

    Hi, You can not use session value or session in global.asax page, plz try Application variable insted of session variable.

    Thursday, September 1, 2011 5:59 AM
  • User1335250097 posted

    Ok, how else I can pass the value from the control on master page to hadle changing culture?

    Thursday, September 1, 2011 6:23 AM
  • User1536090095 posted

    Hi,

    Please elaborate your problem.

    What exactly do you want to achieve?

    Do you want to pass the value from page 1 to page 2 using session ?

    Thanks

    Thursday, September 1, 2011 6:45 AM
  • User1335250097 posted

    Guys, thanks eveyone. I solved it already like that:

    Sub Application_AcquireRequestState(ByVal sender As Object, ByVal e As EventArgs)
    If System.Web.HttpContext.Current.Session IsNot Nothing Then
    Dim l As String = Convert.ToString(Session("Culture"))
    If l IsNot Nothing Then
    Dim ci As CultureInfo = New CultureInfo(l)
    Thread.CurrentThread.CurrentUICulture = ci
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name)
    Else
    l = "ru-RU"
    Dim ci As CultureInfo = New CultureInfo(l)
    Thread.CurrentThread.CurrentUICulture = ci
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name)
    End If
    End If
    End Sub
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 1, 2011 8:32 AM