How can I validate Date.Now?

genesix

New member
Joined
Jul 27, 2024
Messages
2
Programming Experience
1-3
Hi,

I just want to let you know that I am not a well-verse programmer that is why I am seeking help. I really hope that someone will help me. THANK YOU.

My question is how can I validate the dates column in my SQL stable.

The validation should be IF THE DATE.NOW is not equal today then it will insert a record. It found the DATE.NOW is equal then it will just do nothing.


Try
con.ConnectionString = "Data Source=PHHWNELABIO01\SQL2019; Initial Catalog=biometric; User ID=sa; Password=Test1234;"
con.Open()
cmd.Connection = con

'If MessageBox.Show("Do you want To Continue Logging In?", "VISUAL Timesheet", MessageBoxButtons.YesNo) = DialogResult.No Then

Else

cmd = New SqlCommand("insert into timesheet ([ID], [empNo], [empName], [shift], [login], [dates]) values ('" & TextBox2.Text & "','" & TextBox1.Text & "','" & ComboBox1.Text & "','" & ComboBox2.Text & "','" & DateTimePicker1.Value.ToString("MMMM dd, yyyy hh:mm:ss") & "','" & Date.Now & "')", con)
cmd.ExecuteNonQuery()

MessageBox.Show("Successfully Clock-In")


Catch ex As Exception


Finally
con.Close()
End Try

'End If
 

Attachments

  • SQL table.jpg
    SQL table.jpg
    37.7 KB · Views: 9
When posting your code, please use the code block (the left most button in the toolstrip) - your code would then be presented in an easier format, Like:


VB.NET:
Try
    con.ConnectionString = "Data Source=PHHWNELABIO01\SQL2019; Initial Catalog=biometric; User ID=sa; Password=Test1234;"
    con.Open()
    cmd.Connection = con

    'If MessageBox.Show("Do you want To Continue Logging In?", "VISUAL Timesheet", MessageBoxButtons.YesNo) = DialogResult.No Then
    'Else
        cmd = New SqlCommand("insert into timesheet ([ID], [empNo], [empName], [shift], [login], [dates]) values ('" & TextBox2.Text & "','" & TextBox1.Text & "','" & ComboBox1.Text & "','" & ComboBox2.Text & "','" & DateTimePicker1.Value.ToString("MMMM dd, yyyy hh:mm:ss") & "','" & Date.Now & "')", con)
        cmd.ExecuteNonQuery()

        MessageBox.Show("Successfully Clock-In")
    'End If
Catch ex As Exception

Finally
      con.Close()

End Try

The first thing I notice is that your If Then End If is partly outside the Try Catch I fixed that in the code above.
I understand that you want to check the dates column, would that be before you do the insert, you want to check in the table to see if there is already a record containing Today's date?

If so, you would use a query beforehand it would look something like
VB.NET:
Dim checkForTodayDate as SqlDataReader
Try

    con.ConnectionString = "Data Source=PHHWNELABIO01\SQL2019; Initial Catalog=biometric; User ID=sa; Password=Test1234;"
    con.Open()
    cmd.Connection = con
   
    ' check for a record that has today's date
    cmd = New SqlCommand("SELECT * FROM timesheet Where [dates]=Date()", con)
    checkForTodayDate = cmd.ExecuteReader()
   
    If checkForTodayDate.HasRows Then
        ' the query found a record with today's date
        MessageBox.Show("There is already a record for today")
    Else
        ' no records for today, insert the data
        cmd = New SqlCommand("insert into timesheet ([ID], [empNo], [empName], [shift], [login], [dates]) values ('" & TextBox2.Text & "','" & TextBox1.Text & "','" & ComboBox1.Text & "','" & ComboBox2.Text & "','" & DateTimePicker1.Value.ToString("MMMM dd, yyyy hh:mm:ss") & "','" & Date.Now & "')", con)
        cmd.ExecuteNonQuery()
        MessageBox.Show("Successfully Clock-In")

    End If
   
Catch ex As Exception

Finally
    checkForTodayDate.Close()
    con.Close()
End Try
 
Last edited by a moderator:
Hi jDelano,

I really appreciate of the suggestion that you give me. Thank you sir.

I will try it and let you know how it goes.
 
Last edited by a moderator:

Latest posts

Back
Top