Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dim Connection As New MySqlConnection
     Dim Command As New MySqlCommand
     Dim iexit As MsgBoxResult
     Connection = New MySqlConnection("server=localhost; userid=root; database=login;")
     iexit = MsgBox("Do you want to Log out?", vbYesNo, "Tiger Leaf Tea")
     If iexit = MsgBoxResult.Yes Then
         Connection.Open()
         Command = New MySqlCommand("update morongtimedatabase set Time_Out = '" & LblTime.Text & "' where Date = '" & LblDate.Text & "'", Connection)
         Command.ExecuteNonQuery()
         Connection.Close()
         Response.Redirect("LoginPage.aspx")
     End If


What I have tried:

Dim Connection As New MySqlConnection
Dim Command As New MySqlCommand
Dim iexit As MsgBoxResult
Connection = New MySqlConnection("server=localhost; userid=root; password=pogingalien25; database=login;")
iexit = MsgBox("Do you want to Log out?", vbYesNo, "Tiger Leaf Tea")
If iexit = MsgBoxResult.Yes Then
    Connection.Open()
    Command = New MySqlCommand("update morongtimedatabase set Time_Out = '" & LblTime.Text & "' Work_Hours= Time_in-Time_out where Date = '" & LblDate.Text & "'", Connection)
    Command.ExecuteNonQuery()
    Connection.Close()
    Response.Redirect("LoginPage.aspx")
End If
Posted
Updated 20-Feb-22 22:12pm

1 solution

Couple of things:
1) "It doesn't work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.

2) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

3) Never assume UPDATE operations worked: ExecuteNonQuery returns a count of the rows affected - so store that and look at it. At a guess, here it's either returning 0 or loads more than 1 ... The debugger will show you which and you can then check the label content against the DB content (neither of which we have any access to).

4) That code doesn't calculate anything: it just updates a row or rows. You want to calculate work hours, you will need a SELECT at the very least ...
 
Share this answer
 
v2

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