When a form is being submitted, I need to check if a duplicate already exists. If it does, it stops the form from being submitted and allows the person to edit the fields again. I have never really used ajax before, but I have read that for something like this, it would be the best to use. I also can not use PHP, but can use javascript and asp. Right now, I am able to check the database if duplicate values already exist by doing the following:
var SQL = " SELECT COUNT(LeaveID) AS Dup, personalID, StartDate, EndDate, StartTime, EndTime, LeaveType";
SQL += " FROM onLeave";
SQL += " WHERE personalID = " + ipersonalID;
SQL += " GROUP BY personalID, StartDate, EndDate, StartTime, EndTime, LeaveType";
SQL += " HAVING COUNT(LeaveID) > 1";
SQL += " ORDER BY StartDate DESC";
var rs = Connection.Execute(SQL);
writeTable(rs)
writetable(rs) just outputs the data into a html table and ipersonalID is the current ID of the person viewing the page. The submit is just a simple input like below:
<input type="button" name="btnSubmit" value="Submit" onclick="submitSkedChange()" id="btnSubmit"/>
I agree with the comment from @asawyer. A better approach is to add the unique constraint to the table and let it fail. On code behind you simply catch the Exception and display the error to the user. You can specifically catch the
Violation of unique constrainterror and act accordingly. For any other error, you’d have to decide what to do.The above approach has several benefits IMO: