Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: ASP.NET validation
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;
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 22:17pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


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.

-Amit Gajjar
Dinesh Ambaliya at 3-Sep-12 0:35am
Thanks! It's working now.
@amitgajjar at 3-Sep-12 0:36am
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
0 Sergey Alexandrovich Kryukov 223
1 Abhinav S 170
2 Richard Deeming 155
3 CHill60 150
4 Maciej Los 145
0 Sergey Alexandrovich Kryukov 7,176
1 OriginalGriff 6,410
2 Maciej Los 2,877
3 Abhinav S 2,732
4 Peter Leow 2,704

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100