locked
Retrive Value From Database RRS feed

  • Question

  • User1109811461 posted

    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 If



    Wednesday, February 27, 2013 10:15 AM

Answers

  • User-917364509 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 28, 2013 3:58 AM

All replies

  • User-917364509 posted

    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")

    Wednesday, February 27, 2013 10:38 AM
  • User1109811461 posted

    hi mm10

    yes i have column Id in MaklumatKakitangan, could you please write down the simple full code for example that you show above..?

    Wednesday, February 27, 2013 8:43 PM
  • User220175380 posted

    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 

    Thursday, February 28, 2013 12:16 AM
  • User-917364509 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 28, 2013 3:58 AM