多个if语句缺少数据

时间:2012-11-24 15:02:13

标签: mysql vb.net if-statement datareader

只想问为什么这对我不起作用?

我正在尝试带回预订系统的数据(或类似的东西)。

问题是它只发送一个if语句而忽略第二个语句。

使用VB.net并尝试连接到MySQL数据库。

谢谢大家......

彼得

感谢答案的家伙我会尝试所有的问题是,如果它不是=让周一说,那么它应该使复选框的颜色为白色。

我会尝试选择案例并查看。

我做了一些更改添加复选框列表,使其更容易。问题是在不同的日子清除复选框。

    CheckBoxListMon.BackColor = Drawing.Color.White
    CheckBoxListMon.Enabled = True
    CheckBoxListMon.ClearSelection()

这使复选框启用而未被选中,但我仍然无法点击它们被禁用。

任何人都有一些想法?

    Try
        strQuery = "SELECT BookingDate, BookingTime,BookRegUserID,Booked FROM bookings"

        MySQLCmd = New MySqlCommand(strQuery, dbCon)

        dbCon.Open()

        DR = MySQLCmd.ExecuteReader

        While DR.Read

            bookDate = DR.Item("BookingDate")
            bookTime = DR.Item("BookingTime")
            bookRegID = DR.Item("BookRegUserID")
            booked = DR.Item("Booked")

            Select Case True

                Case bookDate = lblMonday.Text And    CheckBoxListMon.Items.FindByValue(test) IsNot Nothing

                    CheckBoxListMon.Items.FindByValue(bookTime).Enabled = False
                    CheckBoxListMon.Items.FindByValue(bookTime).Selected = True
                    CheckBoxListMon.Items.FindByValue(bookTime).Attributes.Add("Style", "color: red;")


                Case bookDate = lblTuesday.Text And CheckBoxListTue.Items.FindByValue(test) IsNot Nothing

                    CheckBoxListTue.Items.FindByValue(bookTime).Enabled = False
                    CheckBoxListTue.Items.FindByValue(bookTime).Selected = True
                    CheckBoxListTue.Items.FindByValue(bookTime).Attributes.Add("Style", "color: red;")
        End While
        DR.Close()
        dbCon.Close()
    Catch ex As Exception

    End Try

3 个答案:

答案 0 :(得分:1)

我认为我现在遇到了问题。如果它们不匹配,你不想将组合框翻转回去。假设您在结果集中返回星期一。第一行将星期一翻转为真,星期二为假,第二行将星期一设置为假 - 我认为你不想 - 并将星期二设置为真,留下一切由最后一行决定。答案是在处理行之前将它们全部设置为false,并且只有在得到匹配时才设置为true,否则忽略它。

   CbMon10.BackColor = Drawing.Color.White
    CbMon10.Checked = False
    CbMon10.Enabled = True
    CbTue10.BackColor = Drawing.Color.White
    CbTue10.Checked = False
    CbTue10.Enabled = True

DR.Read

bookDate = DR.Item("BookingDate")
bookTime = DR.Item("BookingTime")
bookRegID = DR.Item("BookRegUserID")
booked = DR.Item("Booked")

    Select Case True
        Case bookDate = lblMonday.Text And bookTime = CbMon10.Text
            CbMon10.BackColor = Drawing.Color.Red
            CbMon10.Checked = True
            CbMon10.Enabled = False

        Case bookDate = lblTuesday.Text And bookTime = CbTue10.Text
            CbTue10.BackColor = Drawing.Color.Red
            CbTue10.Checked = True
            CbTue10.Enabled = False

    End Select

End While

答案 1 :(得分:0)

您应该使用if-else-if这样的梯子:

 If bookDate = lblMonday.Text And bookTime = CbMon10.Text Then
        CbMon10.BackColor = Drawing.Color.Red
        CbMon10.Checked = True
        CbMon10.Enabled = False
 Else If bookDate = lblTuesday.Text And bookTime = CbTue10.Text Then
        CbTue10.BackColor = Drawing.Color.Red
        CbTue10.Checked = True
        CbTue10.Enabled = False
 Else
        CbTue10.BackColor = Drawing.Color.White
        CbTue10.Checked = False
        CbTue10.Enabled = True
 End If

答案 2 :(得分:0)

而不是多个if,你可以像这样使用它: -

  If (bookDate = lblMonday.Text And bookTime = CbMon10.Text) or (bookDate = lblTuesday.Text And bookTime = CbTue10.Text) Then
            CbMon10.BackColor = Drawing.Color.Red
            CbMon10.Checked = True
            CbMon10.Enabled = False
        Else

            CbMon10.BackColor = Drawing.Color.White
            CbMon10.Checked = False
            CbMon10.Enabled = True
        End If