Click here to Skip to main content
Sign Up to vote bad
good
See more: ASP.NETvalidation
I have a button to inser records in database and a customeValidation function to check the fromDate and toDate's dateDifference must be less than 31 days. So I writted custCheckDates_ServerValidate event like this.
DateTime dt1 = new DateTime(year,month, day);
DateTime dt2 = new DateTime(year1, month1, day1);
TimeSpan diffRes = dt2.Subtract(dt1);
if (diffRes.Days > 31)
{
args.IsValid = false;
return;
}
else
args.IsValid = true;
It's working fine but when validation false it displays error msg but also insert records in the database.
 
So how I can stop the execution of inserting records in database if validation is false. I know that if I use ClientSideValidationFunction then writting 'return false' stop it but I want it server side.
Posted 31 Aug '12 - 21:17


1 solution

Hi,
 
You can use Page.IsValid[^] property to check if validation succeed. if it returns false then you should not insert your record in database.
 
So this property will be checked before insert function.
 
Thanks
-Amit Gajjar
  Permalink  
Comments
Dinesh Ambaliya - 3 Sep '12 - 0:35
Thanks! It's working now.
@amitgajjar - 3 Sep '12 - 0:36
Great, mark it as answered if it works for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 405
1 OriginalGriff 315
2 Maciej Los 240
3 Slacker007 240
4 Aarti Meswania 210
0 Sergey Alexandrovich Kryukov 8,953
1 OriginalGriff 7,134
2 CPallini 3,758
3 Rohan Leuva 3,036
4 Maciej Los 2,488


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 1 Sep 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid