Solved

C# - Validate form items

Posted on 2013-01-18
4
357 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 39

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 39

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now