比较标签上的数字

时间:2014-01-30 09:51:33

标签: vb.net numbers

我正在检查例如数字组之间是否存在数字。如果x小于或等于300但大于或等于60,这里是我的代码

Private Sub CheckMode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckMode.Click
    cn = New ADODB.Connection
    rs = New ADODB.Recordset
    conDB()
    cn.Open()
    rs.Open("Select * From tb_client_mode", cn, 0, 3)
    Dim strClass = GetSessionClass.Text
    Dim strFree = GetSessionFree.Text
    Do While Not rs.EOF
        If rs("st_mode").Value = strClass And TimePerDay.Text = "0" Then
            ClassSession()
            ModeCatchTimer.Stop()
            ModeCatchTimer.Enabled = False
            ModeLabel.Text = "classtime"
            MsgBox("Class Session, Your time will not be deducted", MsgBoxStyle.Information, "")
            Me.Select()
            Exit Sub
        ElseIf rs("st_mode").Value = strFree And TimePerDay.Text = "0" Then
            ModeCatchTimer.Stop()
            ModeCatchTimer.Enabled = False
            cn.BeginTrans()
            cn.Execute("UPDATE tb_registration SET st_log='out' WHERE st_acc_number='" & id_lbl.Text & "'")
            cn.CommitTrans()
            cn.Close()
            Using ClientLockx As New ClientLock
                ClientLock.ShowDialog()
            End Using
            Me.Close()
            Exit Sub
        ElseIf rs("st_mode").Value = strClass Then
            ClassSession()
            ModeCatchTimer.Stop()
            ModeCatchTimer.Enabled = False
            ModeLabel.Text = "classtime"
            MsgBox("Class Session, Your time will not be deducted", MsgBoxStyle.Information, "")
            Me.Select()
            Exit Sub
        ElseIf rs("st_mode").Value = strFree And (TimePerDay.Text <= "298") & TimePerDay.Text >= "59" Then
            FreeSessionMinutesLeft()
            ModeCatchTimer.Stop()
            ModeCatchTimer.Enabled = False
            ModeLabels.Text = "freetime"
            MsgBox("Free Session, Your Time will be deducted", vbOKOnly + vbSystemModal, "")
            Me.Select()
            Exit Sub
        ElseIf rs("st_mode").Value = strFree And (TimePerDay.Text <= "58") & TimePerDay.Text >= "1" Then
            FreeSessionSecondsLeft()
            ModeCatchTimer.Stop()
            ModeCatchTimer.Enabled = False
            ModeLabel.Text = "freetime"
            MsgBox("Free Session, Your Time will be deducted", vbOKOnly + vbSystemModal, "")
            Me.Select()
            Exit Sub
        Else
            rs.MoveNext()
        End If
    Loop
    Exit Sub
End Sub

我得到了这个错误 从字符串“true288”到“Double”类型的转换无效。 我究竟做错了什么。我是vb.net的新手,请建议

1 个答案:

答案 0 :(得分:0)

首先,String不是数字。所以这不是一个好主意:

TimePerDay.Text <= "298"

这会按字典顺序而非数字进行比较,因此"9"大于"10"

您需要使用IntegerInt32.Parse将字符串转换为Int32.TryParse(以处理无效输入)。

Dim timePerDay As Int32
If Int32.TryParse(TimePerDay.Text, timePerDay) AndAlso timePerDay <= 298 Then
    ' ... '
Else
    MesageBox.Show("Invalid number")
End If

除此之外,您对sql-injection开放。请改用sql-parameters。您需要使用ADO instead of ADO.NET吗?