如何设置允许的十进制值?

时间:2014-06-10 14:27:01

标签: vb.net

基本上我有一个插入到数据库中的值,其中值可以是小数,但我想阻止用户输入任何不以.50或.00结尾的内容(例如,如果Qtytxt1 .text =" 2.34"然后错误)但是如果它以.50或.00结尾那么它将允许我的插入(例如,如果Qtytxt1.text =" 2.50"然后插入这里是一个代码片段:

If Qtytxt1.Text <> "" And Qtytxt1.Text <> "0" And Count = 1 Then
                            command = New SqlCommand("INSERT INTO GRN([POD Id], [Delivered     Qty], [Delivered Date], [Delivery No], [User]) " & _
                                              "VALUES ('" + PodId.Value + "','" + Qtytxt1.Text + "','" + ReceivedDate1.Text + "','" + DeliveryNo1.Text + "','" + Uname.ToString + "')", connection)
                            command.ExecuteNonQuery()
 End If
                    Else
                        Messagebox.Show("Cannot Save, Need To Fill The Received Date , Delivery No Or Qty exceedes the Bal...!")
                        Exit For
                    End If
crow = crow + 1
            End If
        Catch ex As Exception
            Messagebox.Show("Error in Inserting the Value in GRN Table....!")
            Exit Sub
        Finally
            connection.Close()
        End Try
    Next
    If crow >= 1 Then
        Messagebox.Show("GRN has been updated successfully")
        save = True
    End If

所以我知道它应该是这样的:

 If Qtytxt1.Text <> "" And Qtytxt1.Text <> "0" And Count = 1 Then
 If Qtytxt1.Text Ends in .50 or .00 Then 
  Messagebox.Show("Please check Delivered Quantity ends in .50 or .00!") 
 Exit Sub
                        Else    command = New SqlCommand("INSERT INTO GRN([POD Id], [Delivered     Qty], [Delivered Date], [Delivery No], [User]) " & _
                                              "VALUES ('" + PodId.Value + "','" + Qtytxt1.Text + "','" + ReceivedDate1.Text + "','" + DeliveryNo1.Text + "','" + Uname.ToString + "')", connection)
                            command.ExecuteNonQuery()
 End If
 End If
                    Else
                        Messagebox.Show("Cannot Save, Need To Fill The Received Date , Delivery No Or Qty exceedes the Bal...!")
                        Exit For
                    End If
crow = crow + 1
            End If
        Catch ex As Exception
            Messagebox.Show("Error in Inserting the Value in GRN Table....!")
            Exit Sub
        Finally
            connection.Close()
        End Try
    Next
    If crow >= 1 Then
        Messagebox.Show("GRN has been updated successfully")
        save = True
    End If

1 个答案:

答案 0 :(得分:0)

我会把它放在TextBox的textChanged事件中。我不确定你是否正在使用提交按钮或其他东西,但这只是验证输入的是数字(即只有一个小数点,没有alpha),并且可以被.50整除。

If IsNumeric(Qtytxt1.text) AndAlso CDec(Qtytxt1.Text) Mod .50 = 0 Then
    Me.btnSubmit.Enabled = True
Else
    Me.btnSubmit.Enabled = False
End If

此外,您应该像SLaks建议的那样查看SQL Injection漏洞。使用参数会更好。