繁体   English   中英

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

[英]How do i set the decimal values to be allowed?

基本上,我在数据库中插入了一个值,其中的值可以是十进制的任何值,但是我想阻止用户输入不以.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

我将其放在您的TextBox的textChanged事件中。 我不确定您是否使用了“提交”按钮或其他功能,但这只是验证输入的内容是数字(即只有一个小数点,没有字母),并且可以被.50整除。

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

另外,您应该按照SLak的建议查看SQL Injection漏洞。 使用参数会更好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM