laurajs laurajs - 1 year ago 57 Question

How to get username to appear on label when user logs in

I am creating a website using and vb on visual studio 2013. I have a log in that holds a session - When the user logs in and are authenticated they are redirected to the user page (user.aspx). This is the patient table (this is dummy data):


The login:

Imports System.Data.SqlClient
Imports System.Data

Partial Class Pages_Login
Inherits System.Web.UI.Page

Protected Sub btnlogin_Click(sender As Object, e As EventArgs) Handles btnlogin.Click

Dim username As String
Dim password As String
Dim bAuthethicated As Boolean
username = txtuser.Text
password = txtpassword.Text
bAuthethicated = CheckUser(username, password)

If bAuthethicated Then
Response.Redirect("user.aspx", True)
lblresult.Text() = "Incorrect Student Number and/or Password"
End If

End Sub

The session:

Public Function CheckUser(username As String, password As String) As Integer
Dim cmdstring As String = "SELECT * FROM Patient Where Username=@USERNAME AND Password=@PASSWORD"
Dim found = 0
Using conn As New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Laura\Final_proj\App_Data\surgerydb.mdf;Integrated Security=True;Connect Timeout=30")

Dim cmd = New SqlCommand(cmdstring, conn)
cmd.Parameters.Add("@USERNAME", SqlDbType.NChar).Value = username
cmd.Parameters.Add("@PASSWORD", SqlDbType.NChar).Value = password

Dim reader = cmd.ExecuteReader()

While reader.Read()
Session("PatientId") = CInt(reader.Item("PatientId"))
found = CInt(reader.Item("PatientId"))
End While

End Using
Return (found)
End Function

Once logged in the user is redirected to the user page and the label displays the PatientId:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Label1.Text = Session("PatientId")
End Sub

I want this label to display the username from the table instead of Patient id - it works when I PatientId is entered but when I changed it to username but it does not display anything anymore does the session then have to be changed to username or can I still hold the session under patientId and display their username? - I will have like a welcome note infront of the label but I dont want the patient to log in and see their ID I want them to see their username.


Just retrieve the username from the database as well. reader.Item() can be used to reference any of the columns returned by your query:

While reader.Read()
    Session("PatientId") = CInt(reader.Item("PatientId"))
    Session("PatientUsername") = CStr(reader.Item("Username"))
    found = CInt(reader.Item("PatientId"))
End While`


Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Label1.Text = Session("PatientUsername")
End Sub`