Get only modified data at form exit
-
Hi guys I have a form that has like 30 controls on them all of them have data that it loads to from a DB, is there a efficient way to only get the data or control that was modified? Thanks
You might like to take a look at Masks and flags using bit fields in .NET[^]. Assign each control on your form a binary value and set a flag if it's data gets modified. If there were less controls I would suggest an enumeration with the
Flags
attribute but I'm not sure (without looking it up) if an enum can handle that many.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Hi guys I have a form that has like 30 controls on them all of them have data that it loads to from a DB, is there a efficient way to only get the data or control that was modified? Thanks
The last time I needeed to do something like that, I built it into a data class that held all the values. The textboxs were bound to those values through properties on the data class. Each property did its validation and then set a dirty flag if the value actually changed. Once the data object serialized itself, the dirty flags were reset.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
You might like to take a look at Masks and flags using bit fields in .NET[^]. Assign each control on your form a binary value and set a flag if it's data gets modified. If there were less controls I would suggest an enumeration with the
Flags
attribute but I'm not sure (without looking it up) if an enum can handle that many.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
Henry Minute wrote:
I'm not sure if an enum can handle that many.
:confused: "An enumeration is a named constant whose underlying type is any integral type except Char". So if you have it derive from int (the default) or uint, you get 32 flags; from long/ulong 64. That is one for each available bit.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
The last time I needeed to do something like that, I built it into a data class that held all the values. The textboxs were bound to those values through properties on the data class. Each property did its validation and then set a dirty flag if the value actually changed. Once the data object serialized itself, the dirty flags were reset.
A guide to posting questions on CodeProject[^]
Dave KreskowiakThis particular way has the advantage of knowing which control was really modified, what i mean is that if a user has a textfield init with the text "Hello" and user erases/modifies and then retry the same value it shouldn't be marked as modified, doing this way will ensure that it has diferent data, but it seems that the vest way is to copy the init data in a structure/class for each form right?
-
This particular way has the advantage of knowing which control was really modified, what i mean is that if a user has a textfield init with the text "Hello" and user erases/modifies and then retry the same value it shouldn't be marked as modified, doing this way will ensure that it has diferent data, but it seems that the vest way is to copy the init data in a structure/class for each form right?
manchukuo wrote:
the vest way is to copy the init data in a structure/class for each form right?
That's pretty much the standard. You could also compare it against the last value that was stored in the database, but that pretty much defeats the purpose of avoiding a database call. You can also get a hash of the initial data (e.g., GetHashCode) and compare that to the hash code for the new data (this will minimize the amount of memory used and will be correct most of the time). If you combine that with dirty checking, you will have a pretty good way of minimizing data transfer.
-
manchukuo wrote:
the vest way is to copy the init data in a structure/class for each form right?
That's pretty much the standard. You could also compare it against the last value that was stored in the database, but that pretty much defeats the purpose of avoiding a database call. You can also get a hash of the initial data (e.g., GetHashCode) and compare that to the hash code for the new data (this will minimize the amount of memory used and will be correct most of the time). If you combine that with dirty checking, you will have a pretty good way of minimizing data transfer.
-
Henry Minute wrote:
I'm not sure if an enum can handle that many.
:confused: "An enumeration is a named constant whose underlying type is any integral type except Char". So if you have it derive from int (the default) or uint, you get 32 flags; from long/ulong 64. That is one for each available bit.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
I notice that in your quote from my post you carefully avoided including the part in braces. Probably due to my age, as others have pointed out elsewhere, I couldn't remember if enums used a sensible storage strategy or not. Which is why I said 'without looking it up'.
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
I notice that in your quote from my post you carefully avoided including the part in braces. Probably due to my age, as others have pointed out elsewhere, I couldn't remember if enums used a sensible storage strategy or not. Which is why I said 'without looking it up'.
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
Only trying to help. I am convinced you would know when you looked it up, however I wanted to save you the trouble as you were concentrating on your game and/or celebrating your winnings. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.