DiederikEEn
DiederikEEn

Reputation: 743

Display logged in user asp.net

I have made a little custom log-in page in asp.net, see code:

        Dim strCon As String = ConfigurationManager.ConnectionStrings("Goed").ConnectionString


        'Create Connection String And SQL Statement
        Dim strSelect As String = "SELECT COUNT(*) FROM tbl_LogIn WHERE Gebruiker = @Gebruiker AND Wachtwoord = @Wachtwoord"

        Dim con As New SqlConnection(strCon)
        Dim cmd As New SqlCommand()
        cmd.Connection = con
        cmd.CommandType = CommandType.Text
        cmd.CommandText = strSelect

        Dim Gebruiker As New SqlParameter("@Gebruiker", _
                                          SqlDbType.VarChar)
        Gebruiker.Value = TxtUs.Text.Trim().ToString()
        cmd.Parameters.Add(Gebruiker)

        Dim Wachtwoord As New SqlParameter("@Wachtwoord", _
                                           SqlDbType.VarChar)
        Wachtwoord.Value = TxtPw.Text.Trim().ToString()
        cmd.Parameters.Add(Wachtwoord)


        con.Open()
        Dim result As Integer = DirectCast(cmd.ExecuteScalar(), Int32)
        con.Close()

        If result >= 1 Then
            Response.Redirect("default.aspx")
        Else
            lblMsg.Text = "Gebruikers naam en of wachtwoord kloppen niet"
        End If

    End Sub

As you can see it directs to Default.aspx.

On my defaults.aspx page I have a header. In this header I want a small label to sdhow the logged in user something like: Hello [User] How can this be done?

Upvotes: 0

Views: 936

Answers (2)

Vinayak Pahalwan
Vinayak Pahalwan

Reputation: 3005

Using Sessions:

While Directing to new page (at Login.aspx-in button's onClick event)

Session["valueName"]=value;

On new page( default.aspx in your case) use:

Label1.Text=Session["valueName"].ToString();

Or you can use cookies as well:

CREATE:

Response.Cookies("userInfo")("userName") = "DiederikEEn"
Response.Cookies("userInfo")("lastVisit") = DateTime.Now.ToString()
Response.Cookies("userInfo").Expires = DateTime.Now.AddDays(1)

READING:

If Not Request.Cookies("userName") Is Nothing Then
    Label1.Text = Server.HtmlEncode(Request.Cookies("userName").Value)
End If

If Not Request.Cookies("userName") Is Nothing Then
    Dim aCookie As HttpCookie = Request.Cookies("userName")
    Label1.Text = Server.HtmlEncode(aCookie.Value)
End If

More here:

  1. Cookies
  2. Sessions

Upvotes: 2

user2255885
user2255885

Reputation: 333

If you can create header in your master page then you can add Hello [User] there and call the session.

Upvotes: 0

Related Questions