-3

I have this code but when i run it,it didn't execute the query,can someone help me?

If timenow <= time2 Then
            cmd = New SqlCommand("INSERT INTO attendance(timeIn,date,tid,late)VALUES('" & timein.Text & "','" & datein.Text & "','" & TidLabel1.Text & "',0)", cn)
            MsgBox("Time out record success!")
        ElseIf timenow < time1 Then
            MsgBox("your late!")
            cmd = New SqlCommand("INSERT INTO attendance(timeIn,date,tid,absent)VALUES('" & timein.Text & "','" & datein.Text & "','" & TidLabel1.Text & "',1)", cn)
            MsgBox("Time in record success!")
            cmd.ExecuteScalar()
        End If
HavelTheGreat
  • 3,299
  • 2
  • 15
  • 34
  • http://stackoverflow.com/questions/542510/how-do-i-create-a-parameterized-sql-query-why-should-i There are many things wrong such as apparently storing text for date data; the data model seems wonky as well - you will probably have a hard time working with that long term – Ňɏssa Pøngjǣrdenlarp Feb 03 '15 at 14:41
  • Please take a moment to review this *carefully*: [Ask] - that is a really awful title – Ňɏssa Pøngjǣrdenlarp Feb 03 '15 at 14:59
  • You never execute the SqlCommand in the first section of the if statement so it would never run in that scenario. You have your message box saying it's a success _before_ you actually execute the statement in the second block and you should most definitely always use parameterized SQL statement to help protect you from SQL injection exploits. Here a VB parameterized query example: http://www.blakepell.com/2012-02-28-vbnet-parameterized-query-example-and-why-you-should-care – b.pell Feb 03 '15 at 15:17

2 Answers2

0

Well, first I would look and see that in the first part of the if statement you didn't execute the query. Assuming you declared the cmd variable above, you only need to call it once after the if statement.

    If timenow <= time2 Then
        cmd = New SqlCommand("INSERT INTO attendance(timeIn,date,tid,late)VALUES('" & timein.Text & "','" & datein.Text & "','" & TidLabel1.Text & "',0)", cn)
        MsgBox("Time out record success!")
    ElseIf timenow < time1 Then
        MsgBox("your late!")
        cmd = New SqlCommand("INSERT INTO attendance(timeIn,date,tid,absent)VALUES('" & timein.Text & "','" & datein.Text & "','" & TidLabel1.Text & "',1)", cn)
        MsgBox("Time in record success!")
    End If

cmd.ExecuteScalar()
Wade73
  • 4,359
  • 3
  • 30
  • 46
  • its still didnt work .. i add this after the query ............... cn.Open() i = cmd.ExecuteNonQuery cn.Close() If i > 0 Then MsgBox("save", MessageBoxIcon.Information, "save") showRecord() clear() Else MsgBox("err", MessageBoxIcon.Error, "error") End If – RM Teklet Feb 03 '15 at 14:41
  • Well, you need to give me more than it doesn't work. Is there any errors? Did you put a break point to make sure the code is being executed in the first place? Additionally, please add any code changes to your original question and format them. It is very hard to see what you added in the comment section. Thanks. – Wade73 Feb 03 '15 at 15:02
  • @RMTeklet - Please do not put code in a comment, update your question instead. – Chris Dunaway Feb 04 '15 at 15:16
0

here's the code

    If timenow <= time2 Then
        cmd = New SqlCommand("INSERT INTO attendance(timeIn,date,tid,late)VALUES('" & timein.Text & "','" & datein.Text & "','" & TidLabel1.Text & "',0)", cn)
        cn.Open()
        i = cmd.ExecuteNonQuery
        cn.Close()
        If i > 0 Then
            MsgBox("save", MessageBoxIcon.Information, "save")
        Else
            MsgBox("err", MessageBoxIcon.Error, "error")

        End If
    ElseIf timenow < time1 Then
        MsgBox("your late!")
        cmd = New SqlCommand("INSERT INTO attendance(timeIn,date,tid,absent)VALUES('" & timein.Text & "','" & datein.Text & "','" & TidLabel1.Text & "',1)", cn)
        cn.Open()
        i = cmd.ExecuteNonQuery
        cn.Close()
        If i > 0 Then
            MsgBox("save", MessageBoxIcon.Information, "save")
        Else
            MsgBox("err", MessageBoxIcon.Error, "error")
        End If
    End If
Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48