Hitting the db each time anything happens, regardless of the event, is
probably a bad idea. The shortcut way would be to define a flag and just
e.Cancel the event if that flag isn't set. When the form is done
initializing then go ahead and set the flag. The order of what happens and
what events fire when the form is initializing isn't a constant thing so
it's pretty hard to handle otherwise. So basically, at the end of the form
loading, set IsLoading to false. Then in your event handler,
if(IsLoading)return;
Ok, now for the real problem. If you are hitting the db each time a combobox
value is selected, you are defintiely wasting a lot of resources and it's a
rare scenario indeed where this is necessary. For instance, if I select the
first value in the combobox, then realize I made a mistake and try to click
the second one, and then realize it was actually the first one after all
taht I wanted, I just made three trips to the DB. This may not appear to be
really wasteful if I only have a few users, but as the user base grows, this
type of approach will not scale. I would consider (depending on the schema
of the db), pulling over the parent and child tables (if they fit that
paradigm) or pulling over the respective tables at once. If they fit the
parent child model, I'd link them w/ a datarelation and set the bindings
accordingly - this way I don't have to make a trip back to the db each time.
http://www.knowdotnet.com/articles/datarelation.html If they don't fit this
model, I'd consider binding to a dataview for instance and setting the
RowFilter property (or use Find or Datatable.Select)
http://www.knowdotnet.com/articles/expressions.html
http://www.knowdotnet.com/articles/adopartiii.html
And even if you must go to the db, I'd use some structure of my own if I
couldn't use a DataTable or a View to hold values that I already retrieved
so that if the users selects soemthing they already selected, I can just
grab it from the local store and save a trip to the db and all of the
overhead associated with it.
HTH,
Bill
--
W.G. Ryan MVP Windows - Embedded
Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups