1

I have a field on a form that saves to a database field that is decimal 9,5 I am unaware of any validation tools in .net that does this for me. Is there an easy way to accomplish this without using substrings to determine if the value is to long?

user867621
  • 1,147
  • 4
  • 16
  • 34

2 Answers2

3

You can use Decimal.TryParse and SqlDecimal.ConvertToPrecScale to check the input:

Dim decValue As Decimal
If Decimal.TryParse(txt.Text, decValue) Then
    'validate if precision is 9 and scale is 5'
    Dim sqlDec As SqlTypes.SqlDecimal = New SqlTypes.SqlDecimal(decValue)
    Dim isValidDecimal As Boolean = True
    Try
        sqlDec = SqlTypes.SqlDecimal.ConvertToPrecScale(sqlDec, 9, 5)
    Catch exception As SqlTypes.SqlTruncateException
        isValidDecimal = False
    End Try
End If
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

You can use Integer.Parse or Integer.tryparse

    Try 
         Integer.Parse("Bad text")
    catch ex as exception
   'Do whatever 
    End Try 

There's also Integer.TryParse - a better way? to help you out if you don't like that method.

Community
  • 1
  • 1
Kat
  • 2,460
  • 2
  • 36
  • 70
  • OP wants to validate if a string could be parsed to a decimal with taking the precision and scale into account of the deicmal field in the database. – Tim Schmelter Oct 15 '14 at 14:01