Reputation: 194
I'm trying to use LinqToSql
in Visual Studio 2010(Asp.net, C#).
The insert/delete/update records works just fine but if I write letters in a int
field, the program breaks in a non-pretty way.
In my DataClasses.Designer
I have:
partial void Insertproduct(product instance);
and I add other class with:
public partial class product
{
partial void OnPriceChanging(string value)
{
Regex Price = new Regex("^[A-Z0-9 a-z]*$");
if (Precio.IsMatch(value) == false)
{
throw new Exception("Just numbers");
}
}
}
I don't know what I'm missing.
Upvotes: 0
Views: 57
Reputation: 997
The regular expression you have used is not correct for validating 'only numbers'. Use this:
public partial class Product
{
partial void OnPriceChanging(string value)
{
Regex price = new Regex("^[0-9]*$");
if (!price.IsMatch(value))
{
throw new Exception("Just numbers");
}
}
}
Upvotes: 1
Reputation: 26
Use int.TryParse it will return a 0 if the string cannot be converted to an int.
int number;
bool IsNumber = int.TryParse("someString", out number);
Upvotes: 1