in Education by
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. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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

Related questions

0 votes
    My If Else statement in VB.net is randomly displaying either of the If or ElseIf condition that I made ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    In VB.NET is there a library of template dialogs I can use? It's easy to create a custom dialog ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 30, 2022 in Education by JackTerrance
0 votes
    In VB.NET is there a library of template dialogs I can use? It's easy to create a custom dialog ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 30, 2022 in Education by JackTerrance
0 votes
    In VB.NET is there a library of template dialogs I can use? It's easy to create a custom dialog ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 29, 2022 in Education by JackTerrance
0 votes
    In VB.NET is there a library of template dialogs I can use? It's easy to create a custom dialog ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 29, 2022 in Education by JackTerrance
0 votes
    In VB.NET is there a library of template dialogs I can use? It's easy to create a custom dialog ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    I have a VB6 program that someone recently helped me convert to VB.NET In the program, when saving ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    I have a VB6 program that someone recently helped me convert to VB.NET In the program, when saving ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    I don't seem to be able to close the OledbDataReader object after reading data from it. Here is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I don't seem to be able to close the OledbDataReader object after reading data from it. Here is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I don't seem to be able to close the OledbDataReader object after reading data from it. Here is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I know that this should be easy but how do I export/save a DataGridView to excel? JavaScript questions ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I don't seem to be able to close the OledbDataReader object after reading data from it. Here is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I'm still learning ASP.NET and I often see code like this throughout parts of our framework: Public ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I'm still learning ASP.NET and I often see code like this throughout parts of our framework: Public ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
...