Justinn Monton Justinn Monton - 3 months ago 9
Vb.net Question

If Else statement result in VB.net is random

My If Else statement in VB.net is randomly displaying either of the If or ElseIf condition that I made.

What I'm trying to do is when a DataGridView checkbox is selected and the LinkLabel is clicked, The system will get the row which is selected and it will update the "EditTime" column to either "ds" or "en" depending on what is already in the column

So, if the data in the "EditTime" column is set to "ds", the If Else condition should change it to "en" and vice versa.

Dim en As OleDbCommand = New OleDbCommand("SELECT * FROM [Roster1]", myConnection)
Dim d As OleDbDataReader = en.ExecuteReader
Dim edt As String = ""
While d.Read
edt = d("EditTime").ToString
End While


Dim CheckedRows =
(
From Rows In AdminTabDisplay.Rows.Cast(Of DataGridViewRow)()
Where CBool(Rows.Cells("ck").Value) = True
).ToList
If CheckedRows.Count > 0 Then

Dim sb As New System.Text.StringBuilder
For Each row As DataGridViewRow In CheckedRows
sb.AppendLine(row.Cells("Employee ID").Value.ToString)

Next

If edt = "ds" Then
Dim st As String = sb.ToString
Dim value As Integer = CInt(st)
Dim cmd As OleDbCommand = New OleDbCommand("UPDATE [Roster1] SET [EditTime] = @pw WHERE [Employee ID] = @v", myConnection)
cmd.Parameters.AddWithValue("@pw", "en")
cmd.Parameters.AddWithValue("@v", value)
cmd.ExecuteNonQuery()
MsgBox("Edit enabled!")

ElseIf edt = "en" Then
Dim st As String = sb.ToString
Dim value As Integer = CInt(st)
Dim cmd As OleDbCommand = New OleDbCommand("UPDATE [Roster1] SET [EditTime] = @pw WHERE [Employee ID] = @v", myConnection)
cmd.Parameters.AddWithValue("@pw", "ds")
cmd.Parameters.AddWithValue("@v", value)
cmd.ExecuteNonQuery()
MsgBox("Edit disabled!")
End If


This is my code. Yet, the output seems to be a bit randomized.

Answer

How many rows are in [Roster1] table? Because with statements:

    While d.Read
        edt = d("EditTime").ToString
    End While

in edt you have always the value of last row. Is correct?

EDIT:

Try this code:

    Dim CheckedRows =
        (
            From Rows In AdminTabDisplay.Rows.Cast(Of DataGridViewRow)()
            Where CBool(Rows.Cells("ck").Value) = True
        ).ToList
    If CheckedRows.Count > 0 Then

    For Each row As DataGridViewRow In CheckedRows

        Dim st as String = row.Cells("Employee ID").Value.ToString
        Dim value As Integer = CInt(st)

        Dim en As OleDbCommand = New OleDbCommand("SELECT * FROM [Roster1] WHERE [Employee ID] = @v", myConnection)
        cmd.Parameters.AddWithValue("@v", value)
        Dim d As OleDbDataReader = en.ExecuteReader
        Dim edt As String = d("EditTime").ToString

        If edt = "ds" Then
            Dim cmd As OleDbCommand = New OleDbCommand("UPDATE [Roster1] SET [EditTime] = @pw WHERE [Employee ID] = @v", myConnection)
            cmd.Parameters.AddWithValue("@pw", "en")
            cmd.Parameters.AddWithValue("@v", value)
            cmd.ExecuteNonQuery()
            MsgBox("Edit enabled!")

        ElseIf edt = "en" Then
            Dim st As String = sb.ToString
            Dim value As Integer = CInt(st)
            Dim cmd As OleDbCommand = New OleDbCommand("UPDATE [Roster1] SET [EditTime] = @pw WHERE [Employee ID] = @v", myConnection)
            cmd.Parameters.AddWithValue("@pw", "ds")
            cmd.Parameters.AddWithValue("@v", value)
            cmd.ExecuteNonQuery()
            MsgBox("Edit disabled!")
        End If
    Next

EDIT (I added the WHILE loop in it and edited some variables and it works!):

Dim CheckedRows =
    (
        From Rows In AdminTabDisplay.Rows.Cast(Of DataGridViewRow)()
        Where CBool(Rows.Cells("ck").Value) = True
    ).ToList
    If CheckedRows.Count > 0 Then


        For Each row As DataGridViewRow In CheckedRows

            Dim st As String = row.Cells("Employee ID").Value.ToString
            Dim value As Integer = CInt(st)

            Dim en As OleDbCommand = New OleDbCommand("SELECT * FROM [Roster1] WHERE [Employee ID] = @v", myConnection)
            en.Parameters.AddWithValue("@v", value)
            Dim d As OleDbDataReader = en.ExecuteReader
            Dim edt As String = ""

            While d.Read
                edt = d("EditTime").ToString
            End While

            If edt = "ds" Then
                Dim cmd As OleDbCommand = New OleDbCommand("UPDATE [Roster1] SET [EditTime] = @pw WHERE [Employee ID] = @v", myConnection)
                cmd.Parameters.AddWithValue("@pw", "en")
                cmd.Parameters.AddWithValue("@v", value)
                cmd.ExecuteNonQuery()
                MsgBox("Edit enabled!")

            ElseIf edt = "en" Then
                Dim cmd As OleDbCommand = New OleDbCommand("UPDATE [Roster1] SET [EditTime] = @pw WHERE [Employee ID] = @v", myConnection)
                cmd.Parameters.AddWithValue("@pw", "ds")
                cmd.Parameters.AddWithValue("@v", value)
                cmd.ExecuteNonQuery()
                MsgBox("Edit disabled!")
            End If
        Next

    End If