简体   繁体   中英

How do i set the decimal values to be allowed?

Basically I have a insert into the database where the values can be anything that's a decimal but I want to stop the user entering anything that doesn't end in a .50 or .00 ( eg if Qtytxt1.text = "2.34" then error ) but if it is ending in .50 or .00 then it will allow my insert ( eg if Qtytxt1.text = "2.50" then insert ) here is a codesnippet :

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

So I know it should be something like :

 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

I would put this in the textChanged event of your TextBox. I am not sure if you are using a Submit button or something, but this just verifies that what is entered is numeric (ie only one decimal point, no alphas) and that it is divisible by .50.

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

Also, you should look at SQL Injection vulnerabilities as SLaks suggested. Using parameters would be better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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