Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I only know few knowledge about vb.net and I am trying to understand it as much as I can. I've tried the code below for login button to save the login date, and it works. Yet, I'm not sure what code to input for logout button. Please help me about this, thankyou! :)

What I have tried:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

      Dim command As New MySqlCommand("SELECT `StudentId`, `Account Password` FROM `usersaccount` WHERE `StudentId` = @username AND `Account Password` = @password", connection)

      command.Parameters.Add("@username", MySqlDbType.VarChar).Value = Username.Text
      command.Parameters.Add("@password", MySqlDbType.VarChar).Value = Pass.Text

      Dim adapter As New MySqlDataAdapter(command)
      Dim table As New DataTable()

      adapter.Fill(table)

      If table.Rows.Count = 0 Then

          MessageBox.Show("Invalid Username Or Password")

      Else

          Using con As New MySqlConnection("server=localhost;username=root;password=;database=logrecord"),
                  cmd As New MySqlCommand("Insert into logrecord (Username, Date, Login) Values(@Username, @Date, @Login);", con)

              cmd.Parameters.Add("@Username", MySqlDbType.VarChar).Value = Username.Text
              cmd.Parameters.Add("@Date", MySqlDbType.VarChar).Value = Date.Now.ToString("dd/MM/yyyy")
              cmd.Parameters.Add("@Login", MySqlDbType.VarChar).Value = TimeOfDay.ToString("hh:mm:ss")
              con.Open()
              cmd.ExecuteNonQuery()
          End Using
          Form3.Show()
          Me.Hide()


      End If

  End Sub
Posted
Updated 4-Nov-21 3:41am
v2

1 solution

Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

And remember: if this is web based and you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.

For alogout, you will have to change you DB to add a "activity type" code to your table: perhaps 0 for Login, 1 for Logout, with other values reserved for later log events.
Then you just add the activity code to your login code above, and duplicate that code with the Logout activity code for logout.

I'd also change the two columns Date and Login to a single column "Timestamp" and make it DateTime instead - storing everything as a string may seem like a simple solution, but when you want to use them it becomes a PITA: string comparisons are done character by character with the result being entirely on the first difference found. So the string dates "02/01/2001" and "01/12/2021" will compare badly, with the later date coming first!
Using DATETIME columns makes that a whole lot easier as it can compare them directly.
 
Share this answer
 
Comments
Myln29 2-Nov-21 5:55am    
Hi! I appreciate your suggestion about the password, and I'm gonna do that. However, regarding with the "activity type" code, I didn't know how to do it. Hehe. The system that I am doing is a computer laboratory time limiter which users can only access the computer based on the allotted time given by the admin. That's why I'm planning to do log history, and after that, I'm going to edit the database for log history and add another column on it which is "Time consumed". This is also my first time doing a system using vb.net with mysql workbench so please understand me if I don't have enough knowledge about it. Thank you so much!
OriginalGriff 2-Nov-21 6:26am    
So what have you done so far?
Myln29 2-Nov-21 9:13am    
There are three button options that I made in admin acc which is manage users, manage time, and reports. Manage users to update users' profile, delete and add users. Manage time to give specific time for users. Reports to preview loghistory of users, together with the time that they already consumed. (Planning to create a button that could export the data into excel)
Well, plans may still change. I'm done in manage users button but before I proceed to other buttons, I want to succeed first in doing log history. (which can also be seen in users acc) I'm still learning, and I hope you can also give me some ideas so I can apply it into my system. Thank you! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900