i got problem , i don't how to retrive ID information then pass it into session
i want the system get the User ID after the user successfully log in
below is my code for your references
Dim mySQLConnection = New SqlConnection()
mySQLConnection.ConnectionString = ConfigurationManager.ConnectionStrings("default").ConnectionString
'Try
mySQLConnection.Open()
Dim cmd As New SqlCommand()
cmd.Connection = mySQLConnection
Dim userName = txtId.Text
Dim passWord = txtPass.Text
'Dim KelasSelian = drpLevel.SelectedValue
cmd.CommandText = "select * from MaklumatKakitangan where UserName=@username and Password=@password"
'cmd.CommandText = "select * from MaklumatGuru where UserName=@username and Password=@password"
cmd.Parameters.AddWithValue("username", userName)
cmd.Parameters.AddWithValue("password", passWord)
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
da.Fill(dt)
If dt.Rows.Count > 0 Then
Session("username") = userName
If drpLevel.SelectedValue = "admin" Then
Session("Kelas") = drpLevel.SelectedValue
Response.Redirect("MainLogIn.aspx")
ElseIf drpLevel.SelectedValue = "Pengguna" Then
Session("ID") = drpLevel.SelectedValue
Response.Redirect("SejarahPinjaman.aspx")
End If
Do you have a column called "Id" in your MaklumatKakitangan table in the database? Then you get the value from it like this and store in a session variable like this:
Prepare one class to strore the loginresult (which is logged in user session), and retrieve the value using property like below
Public Shared Property User(ByVal Pg As Page) As LoginResult
Get
If Pg.Session("UserProfile") Is Nothing Then
Return Nothing
Else
Return DirectCast(Pg.Session("UserProfile"), LoginResult)
End If
End Get
Set(ByVal value As LoginResult)
If Pg.Session("UserProfile") Is Nothing Then
Pg.Session.Add("UserProfile", value)
Else
Pg.Session("UserProfile") = value
End If
End Set
End Property
and then use the below code to get the logged in userid
By assigning the result of user logged in the code behind, you can use like this,
Dim mySQLConnection = New SqlConnection()
mySQLConnection.ConnectionString = ConfigurationManager.ConnectionStrings("default").ConnectionString
'Try
mySQLConnection.Open()
Dim cmd As New SqlCommand()
cmd.Connection = mySQLConnection
Dim userName = txtId.Text
Dim passWord = txtPass.Text
'Dim KelasSelian = drpLevel.SelectedValue
cmd.CommandText = "select * from MaklumatKakitangan where UserName=@username and Password=@password"
'cmd.CommandText = "select * from MaklumatGuru where UserName=@username and Password=@password"
cmd.Parameters.AddWithValue("username", userName)
cmd.Parameters.AddWithValue("password", passWord)
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
da.Fill(dt)
If dt.Rows.Count > 0 Then
Session("username") = userName
Session("ID") = dt.Rows(0)("Id")
If drpLevel.SelectedValue = "admin" Then
Session("Kelas") = drpLevel.SelectedValue
Response.Redirect("MainLogIn.aspx")
ElseIf drpLevel.SelectedValue = "Pengguna" Then
Response.Redirect("SejarahPinjaman.aspx")
End If
afastars
Member
52 Points
221 Posts
Retrive Value From Database
Feb 27, 2013 02:15 PM|LINK
hi all expertise
i got problem , i don't how to retrive ID information then pass it into session
i want the system get the User ID after the user successfully log in
below is my code for your references
Dim mySQLConnection = New SqlConnection() mySQLConnection.ConnectionString = ConfigurationManager.ConnectionStrings("default").ConnectionString 'Try mySQLConnection.Open() Dim cmd As New SqlCommand() cmd.Connection = mySQLConnection Dim userName = txtId.Text Dim passWord = txtPass.Text 'Dim KelasSelian = drpLevel.SelectedValue cmd.CommandText = "select * from MaklumatKakitangan where UserName=@username and Password=@password" 'cmd.CommandText = "select * from MaklumatGuru where UserName=@username and Password=@password" cmd.Parameters.AddWithValue("username", userName) cmd.Parameters.AddWithValue("password", passWord) Dim da As New SqlDataAdapter(cmd) Dim dt As New DataTable() da.Fill(dt) If dt.Rows.Count > 0 Then Session("username") = userName If drpLevel.SelectedValue = "admin" Then Session("Kelas") = drpLevel.SelectedValue Response.Redirect("MainLogIn.aspx") ElseIf drpLevel.SelectedValue = "Pengguna" Then Session("ID") = drpLevel.SelectedValue Response.Redirect("SejarahPinjaman.aspx") End Ifmm10
Contributor
6455 Points
1187 Posts
Re: Retrive Value From Database
Feb 27, 2013 02:38 PM|LINK
Do you have a column called "Id" in your MaklumatKakitangan table in the database? Then you get the value from it like this and store in a session variable like this:
Session("ID") = dt.Rows(0)("Id")
afastars
Member
52 Points
221 Posts
Re: Retrive Value From Database
Feb 28, 2013 12:43 AM|LINK
hi mm10
yes i have column Id in MaklumatKakitangan, could you please write down the simple full code for example that you show above..?
farooque84
Participant
1358 Points
321 Posts
Re: Retrive Value From Database
Feb 28, 2013 04:16 AM|LINK
Hi,
Prepare one class to strore the loginresult (which is logged in user session), and retrieve the value using property like below
Public Shared Property User(ByVal Pg As Page) As LoginResult
Get
If Pg.Session("UserProfile") Is Nothing Then
Return Nothing
Else
Return DirectCast(Pg.Session("UserProfile"), LoginResult)
End If
End Get
Set(ByVal value As LoginResult)
If Pg.Session("UserProfile") Is Nothing Then
Pg.Session.Add("UserProfile", value)
Else
Pg.Session("UserProfile") = value
End If
End Set
End Property
and then use the below code to get the logged in userid
By assigning the result of user logged in the code behind, you can use like this,
MyUser.User(Me) = res
here res will bring the user id
mm10
Contributor
6455 Points
1187 Posts
Re: Retrive Value From Database
Feb 28, 2013 07:58 AM|LINK
Try something like this:
Dim mySQLConnection = New SqlConnection() mySQLConnection.ConnectionString = ConfigurationManager.ConnectionStrings("default").ConnectionString 'Try mySQLConnection.Open() Dim cmd As New SqlCommand() cmd.Connection = mySQLConnection Dim userName = txtId.Text Dim passWord = txtPass.Text 'Dim KelasSelian = drpLevel.SelectedValue cmd.CommandText = "select * from MaklumatKakitangan where UserName=@username and Password=@password" 'cmd.CommandText = "select * from MaklumatGuru where UserName=@username and Password=@password" cmd.Parameters.AddWithValue("username", userName) cmd.Parameters.AddWithValue("password", passWord) Dim da As New SqlDataAdapter(cmd) Dim dt As New DataTable() da.Fill(dt) If dt.Rows.Count > 0 Then Session("username") = userName Session("ID") = dt.Rows(0)("Id") If drpLevel.SelectedValue = "admin" Then Session("Kelas") = drpLevel.SelectedValue Response.Redirect("MainLogIn.aspx") ElseIf drpLevel.SelectedValue = "Pengguna" Then Response.Redirect("SejarahPinjaman.aspx") End If