?
Solved

C# - Validate form items

Posted on 2013-01-18
4
Medium Priority
?
363 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

762 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