Maximiliano Poggio
Maximiliano Poggio

Reputation: 23

Session variable in master page asp.net

I have a login page, and I want to set the user name as a session variable, because I want to show it on the right corner on my web application and I have to access it in all content pages.

What I'm doing is after doing login, setting a session variable in master page like:

Session("nameUser") = Request.QueryString("nameUser")

Then, first time when I access the default page it's working ok. But I change the page to another one, that inherits masterpage too, it crashes and the error says: reference to object isn't established as an object instance. How can I resolve this?

I just want to set a session variable and use it in all content pages. (or if I can't, set it into the master page for all the content pages)

Edited:

This is in my login page. If login is ok i make a request to another page with the name of the user as a parameter.

Dim SQL As String = "SELECT * FROM Usuarios WHERE Identificacion='" & txtNomUsuario.Text & _
        "' AND Password='" & txtPasswordUsuario.Text & "'"

        Dim da As New SqlDataAdapter(SQL, cnn)
        Dim ds As New DataSet

        da.Fill(ds)

        If ds.Tables(0).Rows.Count() = 1 Then

            Dim nomUsuario As String = txtNomUsuario.Text

            Response.Redirect("Default.aspx?nomUsuario=" & nomUsuario)


        Else
            Response.Redirect("about:blank")

        End If

And im doing this in the init of the master page

Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init

        If Not IsPostBack Then
            If Request.QueryString("nomUsuario") <> Nothing Then
                Session("nomUsuario") = Request.QueryString("nomUsuario")
                lblUsuario.Text = Session("nomUsuario")
            Else
                lblUsuario.Text = Session("nomUsuario")
            End If

        End If

    End Sub

Upvotes: 0

Views: 4056

Answers (3)

Md. Arafat Al Mahmud
Md. Arafat Al Mahmud

Reputation: 3214

just remove your session variable declaration from the masterpage and declare it in the login page. Hope this helps,

Session("nameUser") = Request.QueryString("nameUser")

    Dim SQL As String = "SELECT * FROM Usuarios WHERE Identificacion='" & txtNomUsuario.Text & _
            "' AND Password='" & txtPasswordUsuario.Text & "'"

            Dim da As New SqlDataAdapter(SQL, cnn)
            Dim ds As New DataSet

            da.Fill(ds)

            If ds.Tables(0).Rows.Count() = 1 Then

                Dim nomUsuario As String = txtNomUsuario.Text

                Response.Redirect("Default.aspx?nomUsuario=" & nomUsuario)


            Else
                Response.Redirect("about:blank")

            End If

Upvotes: 0

Ravi Vanapalli
Ravi Vanapalli

Reputation: 9942

This is because on each of the new page the MasterPage checks for QueryString variable "nameUser".

It is better to set Session("nameUser") in login page as it will be accessible across the Session.

Happy Coding !!!

Upvotes: 1

huMpty duMpty
huMpty duMpty

Reputation: 14460

Why don't you try using HttpContext.User Property

HttpContext.Current.User

Upvotes: 0

Related Questions