1

I am using Visual Studio 2010 as my IDE and creating a simple website using Visual Basic I dunno if it's possible but can I display the Username that has just logged into my LoginForm to the other forms using sessions?

I'm not that good enough to understand it but can anyone tell me, is this the right way to contain the value in a session?, how can I display it to the other form?

Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        Dim connect As String = "Provider=Microsoft.ACE.OleDb.12.0;" & _
        "Data Source=C:\Users\cleanfuel\Documents\Visual Studio 2010\Projects\FinalProject4a2p\FinalProject4a2p\bin\DBFinalProject.accdb"
        Dim query As String
        query = "Select Count(*) From tblAccount Where Username = ? And UserPass = ?"
        Dim result As Integer = 0
        Using conn As New OleDbConnection(connect)
            Using cmd As New OleDbCommand(query, conn)
                cmd.Parameters.AddWithValue("", TxtUser.Text)
                cmd.Parameters.AddWithValue("", txtPass.Text)
                conn.Open()
                result = DirectCast(cmd.ExecuteScalar(), Integer)
            End Using
        End Using

        If result > 0 Then
            Response.Redirect("Menus.aspx")
            Session("User") = TxtUser.Text
            Session("Pass") = txtPass.Text
        Else
            Response.Write("<td>")
            Response.Write("<div align=""center"">")
            Response.Write("<font color='white'>")
            Response.Write("Unable to Login, Invalid Username or Password! </font>")
            Response.Write("</div>")
            Response.Write("</td>")
        End If
    End Sub
2
  • I think that you should first store the user name in the session and only then redirect him to menus.aspx Commented Feb 18, 2013 at 7:46
  • I did that and this is the error that I got : "Object reference not set to an instance of an object." Commented Feb 18, 2013 at 8:00

3 Answers 3

3

Setup a label in your Master Page (if you have one), assign the user name from your session to the label and it will appear in all the pages. If you don't have Master page then can setup a label in the page (you want username to appear) and then set the label Text property to value from the session.

The way you are storing the values in the session is correct, you should redirect to Menu.aspx once the values are stored in the session like:

If result > 0 Then
            Session("User") = TxtUser.Text
            Session("Pass") = txtPass.Text
            Response.Redirect("Menus.aspx")

....

And to access them you can do :

labelUserName.Text = Session("User").ToString()
Sign up to request clarification or add additional context in comments.

5 Comments

sir Habib I don't have Master Page, should I do the same process?
@Danjor, if you don't have a master page, you can put the label in any of the page you want to show the user name and show it like above
I have an error "Object reference not set to an instance of an object." what does it mean?
That means your session is not being setted up before you are accessing it.
huh? does it mean it didn't get some value from LoginForm?
2

Use FormsAuthentication, then you can simply put a LoginName control on your form, or get the UserName from HttpContext.Current.User.Identity.Name

4 Comments

I'm newbie, I can't understand the term that you have used. it's like an professional way...
What don't you understand? Use google to find a tutorial on Forms Authentication.
but I only need to used sessions , is this easier to used than sessions?
Yes, it's easier to use the built-in authentication functionality rather than rolling your own.
0

The answers that the other users provide can be used also, but I find this one and successfully got the result that I want to have.

here are my codes:

Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        Dim connect As String = "Provider=Microsoft.ACE.OleDb.12.0;" & _
        "Data Source=C:\Users\cleanfuel\Documents\Visual Studio 2010\Projects\FinalProject4a2p\FinalProject4a2p\bin\DBFinalProject.accdb"
        Dim query As String
        query = "Select Count(*) From tblAccount Where Username = ? And UserPass = ?"
        Dim result As Integer = 0
        Using conn As New OleDbConnection(connect)
            Using cmd As New OleDbCommand(query, conn)
                cmd.Parameters.AddWithValue("", TxtUser.Text)
                cmd.Parameters.AddWithValue("", txtPass.Text)
                conn.Open()
                result = DirectCast(cmd.ExecuteScalar(), Integer)
            End Using
        End Using

        If result > 0 Then
            Dim myCookie As HttpCookie = New HttpCookie("USER")
            myCookie.Value = TxtUser.Text
            Response.Cookies.Add(myCookie)

            Response.Redirect("Menus.aspx")
        Else
            Response.Write("<td>")
            Response.Write("<div align=""center"">")
            Response.Write("<font color='white'>")
            Response.Write("Unable to Login, Invalid Username or Password! </font>")
            Response.Write("</div>")
            Response.Write("</td>")
        End If

    End Sub

I used HTTPcookie instead of session because I can't satisfy myself because it didn't displayed the value that I want to display and it always shows me the same ERROR over and over again.

here are the codes to display:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Request.Cookies("USER") Is Nothing Then
            Label7.Text = "No Account Logged In"
        Else
            Dim aCookie As HttpCookie = Request.Cookies("USER")
            Label7.Text = Convert.ToString(Server.HtmlEncode(aCookie.Value))
        End If
    End Sub

2 Comments

You should be aware that a client can easily spoof your cookie.
what do you mean by spoof? is there a problem if I used cookie instead of other alternative codes?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.