none
how to store username into a database when you login? RRS feed

  • Question

  • hey guys, is it possible to store username into another table in a database after you login? It's for the login history. I tried to do it but I can't make it work. Maybe you guys can help me. Thanks :)
    Saturday, November 24, 2018 5:29 PM

Answers

  • hey guys, is it possible to store username into another table in a database after you login? It's for the login history. I tried to do it but I can't make it work. Maybe you guys can help me. Thanks :)

    Hi Jud.......,

    Yes you can .... you will pass the username from username textbox to the form that open when click login button like this 

      Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
            If Len(Trim(txtUserName.Text)) = 0 Then
                MessageBox.Show("Please enter user name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                txtUserName.Focus()
                Exit Sub
            End If
    
            If Len(Trim(txtPassword.Text)) = 0 Then
                MessageBox.Show("Please enter password", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                txtPassword.Focus()
                Exit Sub
            End If
            Try
                Dim myConnection As SqlConnection
                myConnection = New SqlConnection(cs)
    
                Dim myCommand As SqlCommand
    
                myCommand = New SqlCommand("SELECT User_Name,User_Pass FROM Registration WHERE User_Name = @User_Name AND User_Pass = @User_Pass", myConnection)
    
                Dim uName As New SqlParameter("@User_Name", SqlDbType.NVarChar)
    
                Dim uPassword As New SqlParameter("@User_Pass", SqlDbType.NVarChar)
    
                uName.Value = txtUserName.Text
    
                uPassword.Value = txtPassword.Text
    
                myCommand.Parameters.Add(uName)
    
                myCommand.Parameters.Add(uPassword)
    
                myCommand.Connection.Open()
    
                Dim myReader As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    
                Dim Login As Object = 0
    
                If myReader.HasRows Then
    
                    myReader.Read()
    
                    Login = myReader(Login)
    
                End If
    
                If Login = Nothing Then
    
                    MsgBox("Login is Failed...Try again !", MsgBoxStyle.Critical, "Login Denied")
                    txtUserName.Clear()
                    txtPassword.Clear()
                    txtUserName.Focus()
    
                Else            
    
                    Form1.ToolStripStatusLabel2.Text = txtUserName.Text
                    Me.Hide()
                    Form1.Show()
    
                End If
                myCommand.Dispose()
                myConnection.Close()
    
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
        End Sub

    This line may help you 

    Form1.ToolStripStatusLabel2.Text = txtUserName.Text

    hope it helps .....


    Regards From Amr_Aly

    • Marked as answer by Judzzz Tuesday, November 27, 2018 9:51 PM
    Saturday, November 24, 2018 6:56 PM
  • Hello,

    No matter the database type e.g. Oracle, MS-Access, SQL-Server, MySQL the following shows a repeatable pattern that works by changing to the appropriate data provider.

    The user name can be passed to the method below via asking in a TextBox for the login or perhaps use Environment.UserName

    What I'm guessing you want to is login date/time. This would be done by adding a date time column with a default value of Now. 

    For SQL-Server here is the code to alter an existing column or do it in SSMS (SQL-Server Management Studio)

    ALTER TABLE YourTable ADD CONSTRAINT DF_YourTable DEFAULT GETDATE() FOR YourColumn
    For MS-Access do this in designer mode.
    Imports System.Data.OleDb
    Imports System.Data.SqlClient
    Public Class DataOperations
        Public Sub SqlServerLogin(pUserName As String)
            Using cn As New SqlConnection With {.ConnectionString = "TODO"}
                Using cmd As New SqlCommand With
                    {
                        .Connection = cn,
                        .CommandText = "INSERT INTO LoginHistory (UserName) Values(@UserName)"
                    }
    
                    cmd.Parameters.AddWithValue("@UserName", pUserName)
                    cn.Open()
                    cmd.ExecuteNonQuery()
                End Using
            End Using
        End Sub
        Public Sub MsAccessLogin(pUserName As String)
            Using cn As New OleDbConnection With {.ConnectionString = "TODO"}
                Using cmd As New OleDbCommand With
                    {
                        .Connection = cn,
                        .CommandText = "INSERT INTO LoginHistory (UserName) Values(@UserName)"
                    }
    
                    cmd.Parameters.AddWithValue("@UserName", pUserName)
                    cn.Open()
                    cmd.ExecuteNonQuery()
                End Using
            End Using
        End Sub
    End Class
    
    If I'm missing something it's because you have not asked for it.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Judzzz Tuesday, November 27, 2018 9:50 PM
    Saturday, November 24, 2018 7:27 PM
    Moderator

All replies

  • Hello,

    have you tried using a procedure that runs after every successful login, the procedure will store the credentials in another table.

    Saturday, November 24, 2018 5:38 PM
  • hey guys, is it possible to store username into another table in a database after you login? It's for the login history. I tried to do it but I can't make it work. Maybe you guys can help me. Thanks :)

    Hi Jud.......,

    Yes you can .... you will pass the username from username textbox to the form that open when click login button like this 

      Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
            If Len(Trim(txtUserName.Text)) = 0 Then
                MessageBox.Show("Please enter user name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                txtUserName.Focus()
                Exit Sub
            End If
    
            If Len(Trim(txtPassword.Text)) = 0 Then
                MessageBox.Show("Please enter password", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                txtPassword.Focus()
                Exit Sub
            End If
            Try
                Dim myConnection As SqlConnection
                myConnection = New SqlConnection(cs)
    
                Dim myCommand As SqlCommand
    
                myCommand = New SqlCommand("SELECT User_Name,User_Pass FROM Registration WHERE User_Name = @User_Name AND User_Pass = @User_Pass", myConnection)
    
                Dim uName As New SqlParameter("@User_Name", SqlDbType.NVarChar)
    
                Dim uPassword As New SqlParameter("@User_Pass", SqlDbType.NVarChar)
    
                uName.Value = txtUserName.Text
    
                uPassword.Value = txtPassword.Text
    
                myCommand.Parameters.Add(uName)
    
                myCommand.Parameters.Add(uPassword)
    
                myCommand.Connection.Open()
    
                Dim myReader As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    
                Dim Login As Object = 0
    
                If myReader.HasRows Then
    
                    myReader.Read()
    
                    Login = myReader(Login)
    
                End If
    
                If Login = Nothing Then
    
                    MsgBox("Login is Failed...Try again !", MsgBoxStyle.Critical, "Login Denied")
                    txtUserName.Clear()
                    txtPassword.Clear()
                    txtUserName.Focus()
    
                Else            
    
                    Form1.ToolStripStatusLabel2.Text = txtUserName.Text
                    Me.Hide()
                    Form1.Show()
    
                End If
                myCommand.Dispose()
                myConnection.Close()
    
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
        End Sub

    This line may help you 

    Form1.ToolStripStatusLabel2.Text = txtUserName.Text

    hope it helps .....


    Regards From Amr_Aly

    • Marked as answer by Judzzz Tuesday, November 27, 2018 9:51 PM
    Saturday, November 24, 2018 6:56 PM
  • Hello,

    No matter the database type e.g. Oracle, MS-Access, SQL-Server, MySQL the following shows a repeatable pattern that works by changing to the appropriate data provider.

    The user name can be passed to the method below via asking in a TextBox for the login or perhaps use Environment.UserName

    What I'm guessing you want to is login date/time. This would be done by adding a date time column with a default value of Now. 

    For SQL-Server here is the code to alter an existing column or do it in SSMS (SQL-Server Management Studio)

    ALTER TABLE YourTable ADD CONSTRAINT DF_YourTable DEFAULT GETDATE() FOR YourColumn
    For MS-Access do this in designer mode.
    Imports System.Data.OleDb
    Imports System.Data.SqlClient
    Public Class DataOperations
        Public Sub SqlServerLogin(pUserName As String)
            Using cn As New SqlConnection With {.ConnectionString = "TODO"}
                Using cmd As New SqlCommand With
                    {
                        .Connection = cn,
                        .CommandText = "INSERT INTO LoginHistory (UserName) Values(@UserName)"
                    }
    
                    cmd.Parameters.AddWithValue("@UserName", pUserName)
                    cn.Open()
                    cmd.ExecuteNonQuery()
                End Using
            End Using
        End Sub
        Public Sub MsAccessLogin(pUserName As String)
            Using cn As New OleDbConnection With {.ConnectionString = "TODO"}
                Using cmd As New OleDbCommand With
                    {
                        .Connection = cn,
                        .CommandText = "INSERT INTO LoginHistory (UserName) Values(@UserName)"
                    }
    
                    cmd.Parameters.AddWithValue("@UserName", pUserName)
                    cn.Open()
                    cmd.ExecuteNonQuery()
                End Using
            End Using
        End Sub
    End Class
    
    If I'm missing something it's because you have not asked for it.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Judzzz Tuesday, November 27, 2018 9:50 PM
    Saturday, November 24, 2018 7:27 PM
    Moderator