Solved

C# - Validate form items

Posted on 2013-01-18
4
360 Views
Last Modified: 2013-03-05
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
Comment
Question by:ttta83
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 38793113
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
 

Author Comment

by:ttta83
ID: 38793365
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
 
LVL 6

Accepted Solution

by:
esolve earned 250 total points
ID: 38797901
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
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 250 total points
ID: 38837196
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
encoding an href string 2 24
jquery datatables, asp.net MVC 9 51
How come this regular expression allows "e"? 14 45
How can I do Unit Testing with Session ? 1 10
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question