Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Sep 11, 2012 05:26 AM by hassanmehmood
Sep 11, 2012 01:24 AM|LINK
What's the best way of detecting whether the user has made any changes to the form?
Let's say I have 5 text boxes, a check box, and a drop down list with prepopulated data, ticked box, and selected index for a drop down list. User makes changes (edit), then submits. How can I figure out which fields have been altered?
The reason I'm asking this is because I need to know which properties I have to update in the database.
Sep 11, 2012 04:10 AM|LINK
You have two ways,
1. When u load the data on screen. keep it saved locally in session / lists or your own data structures etc. When user submit the page, Compare the data from the page with the one u saved before locally. This is how u can get which fields are changed and
which needs to be updated.
2. Dont store any data locally. When user submits the page, query from database again to match which fields are changed and which needs to be updated in the database.
It seems you dont have much controls or data on your page. so i feel there's no need to check if user has made any changes or not (if this is not the requirement). So, just update whatever values are there on the form.
Hope this helps..
Sep 11, 2012 05:12 AM|LINK
I was basically implementing the first method, but now that you mentioned it, maybe I should retrieve the "old" value from the database once the postback is occurred. The reason I say that is, the user might update the form after the session is expired,
so it is best to populate a new custom object from the database rather than using the saved session object.
And the main reason I have to check for changes is that I have to update a logging table if and only if a change is requested, and if I go ahead and do the update regardless of any new changes or not, I will be polluting the logging table with spurious rows
where in reality, the logging table is supposed to just keep track of "changes."
Thanks for the input.
Sep 11, 2012 05:16 AM|LINK
I think u should not be worry about which data has been changed or not, if form is in edit mode it is pretty sure that user want to update it ,
so just update it using simple update command
if u r using same button to insert and update then use
if exists( use select stmt with condition)
Sep 11, 2012 05:26 AM|LINK
main reason I have to check for changes is that I have to update a logging table if and only if a change is requested, and if I go ahead and do the update regardless of any new changes or not, I will be polluting the logging table with spurious rows where in
reality, the logging table is supposed to just keep track of "changes."
you could develop this thing at your architecture level. Then you might not have any need to worry about what user has changed.
Jsut for instance, at architecture level (like in BL or DAL) : you could do this:
1. Create class for updating / inserting logging table
2. Pass sufficient arguments to this class's method. So the method (before the actual insertion / updation to subjected table), can get the old values object / entity (which it will get from DB) and also the new values object / entity (which u passed from
form), and it will then match all fields , if no field is matched it will create loggin table entry using logging table data structure. And your front end classes even ur BL classes wont even know what has just happend :)
3. Another hint, you can use Generics for matching the fields as there will be lots of entities / objects. You cant make method of logging table for each of them. Right?