[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

C# - Validate form items

I'm working on a C# page that has a lot of textboxes, drop downs, text areas.

Last Name:
First Name:
Contract #:
Phone:
Email:
Birth date:
Location:
Start Date:
End Date:
Salary:

......

The data will be stored in 3 different tables
EMPLOYEE (EmplID, Last Name, First Name, Phone, Email, Birthdate...)
EMPL_LOC (EmplID, ContractNbr, Location...)
EMPL_SAL (EmplID, StartDate, EndDate, Salary...)

Items are not in order.  #1 (LastName),#2 (FirstName),#4,#5,#6 - Table EMPLOYEE, #3, #7 - Table EMPL_LOC...

Everytime the user clicks on Update button, I want to check to see which item(s) got changed to update appropriate table(s).  For example, if the user only changes "location", I just want to update only EMPL_LOC table.

On PageLoad, I can get all the values on the form

formcount = document.forms(0).length;
            for (i = 0; i < formcount - 1; i++) {
                itemType = document.forms(0).item(i).type;
                switch (itemType) {
                           case "text":
                                          iALL_Items_Values = iALL_Items_Values + document.forms(0).item(i).value.toString().trim();
                                         break;
                           case "select":
                                          iALL_Items_Values = iALL_Items_Values + document.forms(0).item(i).value.toString().trim();
                                         break;
                           case "radio":
                                          iALL_Items_Values = iALL_Items_Values + document.forms(0).item(i).value.toString().trim();
                                         break;
                           case "checkbox":
                                          iALL_Items_Values = iALL_Items_Values + document.forms(0).item(i).value.toString().trim();
                                         break;
}
}                        

When the user clicks on the Update button, I call the function above again to get the values on the screen and compare to the values that I captured on PageLoad to see if there's any changes.

How do I do it for each table, only check #1,#2,#4,#5,#6... to see if table EMPLOYEE needs to be updated, #3,#7... to see if EMPL_LOC needs to be update, and so on...

Thank you very much for your help.
0
ttta83
Asked:
ttta83
  • 2
2 Solutions
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Honestly, to call the update again is not that bad.  

If you were really dead set on this what I would do is store the original values in the sesssion on load.


so when loading:

Session["Firstname"] = firstname;


Then you can check the textbox values on the update.

bool updEMPLOYEE  = false;

if (Session["FirstName"].ToString() != txtFirstName.Text || Session["LastName"].ToString() != txtLastName.Text)  // add other checks as needed.
   updEMPLOYEE  = true;


if (updEmployee)
   // update statement.
0
 
ttta83Author Commented:
Thanks ged325,

I  don't want to update again 'cause we keep track of all the changes.  The trigger will insert a record into history table eveytime we update the main table.

Is there a way that we can compare like your way but without specify the name of the item 'cause I have about 50 ietms on the form.

Thanks again for your help.
0
 
esolveCommented:
The easiest option is to put the logic into a stored procedure rather than the interface. The reason I am saying this is theres a lot of work to be done either in code or in the SP to check what have changed and what did not.

If you do this in code your opsions are to:
1. Save the data (data used to populate the controls) in session on the page so that you can check if the values when trying to save back to the database. (Page size gets too big so not a good approach)
2. Retrieve all the data again and use code to iterate over the data just before updating. (best  options but this can slow the page down if theres a lot of data to compare)

If you have a lot of data to compare just put it in the proc and only call update on the table if needed.

DECLARE @UpdateNeeded BIT
SET @UpdateNeeded = 0 --No update needed
IF EXISTS (SELECT 1 FROM Table WHERE ID = @ID and FirstName = @FirstName)
BEGIN
  SET @UpdateNeeded = 1
END

---do this for each parameter

IF(@UpdateNeeded)
BEGIN
 UPDATE Table SET FirstName = @FirstName where ID = @ID
END

--else do nothing update won't be called
--return a value if you want to inform UI nothing happend or ID was updated
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
IMO:

I would actually update the trigger so that only fields that were actually changed are written.

consider:
update table set col1 = col1;

Why fill up the logs with a whole bunch of non-changes?

Sample audit:

http://www.codeproject.com/Articles/441498/Quick-SQL-Server-Auditing-Setup
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now