none
how to login and insert username, date and time into a database table with a single button RRS feed

  • Question

  • hey guys. how do I login and at the same time insert the username and date into a database table in a single login button? I have a table named history in my database where I want the username and date to be inserted. i used timer and label for the time and date and a textbox for the username. I can't make it work, please help. thanks guys:)
    • Edited by Judzzz Saturday, November 17, 2018 9:08 AM
    Saturday, November 17, 2018 8:59 AM

All replies

  • Hello,

    Can you please explain the login process and what type of database.


    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

    Saturday, November 17, 2018 9:18 AM
    Moderator
  • I login using a username and password. there is only one button, the login button. I'm using mysql database
    • Edited by Judzzz Saturday, November 17, 2018 10:12 AM
    Saturday, November 17, 2018 10:05 AM
  • I login using a username and password. there is only one button, the login button. I'm using mysql database
    Let's see the code which is not working please.

    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

    Saturday, November 17, 2018 10:34 AM
    Moderator
  • Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            FirstOperation()
            SecondOperation()
    
        End Sub
    Private Sub FirstOperation()
       
            If conn.State = ConnectionState.Closed Then
                conn.ConnectionString = "server=localhost;uid=root;database=lis;"
                conn.Open()
            End If
    
            Dim cmd As New MySqlCommand
            Dim SqlQuery As String = "SELECT * FROM account WHERE (username = '" & TBusername.Text & "') AND (password = '" & TBpassword.Text & "')"
    
    
            With cmd
                .CommandText = SqlQuery
                .Connection = conn
            End With
    
            cmd.ExecuteNonQuery()
    
            Dim sqlReader As MySqlDataReader
    
            sqlReader = cmd.ExecuteReader()
    
            Dim id As New Integer
            Dim i As New Integer
            id = 0
            i = 0
            While sqlReader.Read
                i = (i + 1)
                id = sqlReader("id")
            End While
    
            If i > 0 Then
                TBusername.Text = Nothing
                TBpassword.Text = Nothing
                MsgBox("Successfully Logged In!", MsgBoxStyle.Information, "Welcome")
                Me.Hide()
    
                main.Show()
                main.Lusername.Text = sqlReader("username")
                main.Llevel.Text = sqlReader("user_lvl")
                userlevel = sqlReader("user_lvl")
    
    
            Else
                MsgBox("Invalid Username or Password", MsgBoxStyle.Exclamation, "Invalid")
                TBpassword.Text = Nothing
            End If
            conn.Close()
        End Sub
    
        Private Sub SecondOperation()
            
            If conn.State = ConnectionState.Closed Then
                conn.ConnectionString = "server=localhost;uid=root;database=lis;"
                conn.Open()
            End If
            Dim sqlQuery As String = "INSERT INTO history(i_d,name,) VALUES (Null,'" & TBusername.Text & "')"
            Dim sqlCommand As New MySqlCommand
    
            With sqlCommand
                .CommandText = sqlQuery
                .Connection = conn
                .ExecuteNonQuery()
            End With
    
            conn.Close()
    
        End Sub

    Saturday, November 17, 2018 12:33 PM
  • Hi,

    I don't know where your code is reporting errors, but I think you need to fix the code below.

     Dim id As New Integer
            Dim i As New Integer
            id = 0
            i = 0
            Dim t1 As String = Nothing
            Dim t2 As String = Nothing
            While sqlReader.Read
                i = (i + 1)
                id = sqlReader("id")
                t1 = sqlReader("username").ToString
                t2= sqlReader("user_lvl").ToString
            End While
    
            If i > 0 Then
                TBusername.Text = Nothing
                TBpassword.Text = Nothing
                MsgBox("Successfully Logged In!", MsgBoxStyle.Information, "Welcome")
                Me.Hide()
    
                main.Show()
                main.Lusername.Text = t1
                main.Llevel.Text = t2
                'userlevel = sqlReader("user_lvl")

    and the TBusername.Text you inserted is a null value, if you don't want this, you need to put a Textbox on the form to store the value.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 19, 2018 2:54 AM