combo box edit best practice

Hi, I am using c# combo box controls where I would like to allow the users to enter new values. i.e I have the DropDownStyle = DropDown. For example, I have a combo box that allows users to select/enter the country code. While loading the form I set the dataset for the combobox by querying database for the code and displaytext for the countries. User can either select an existing country code or enter a new one. Please don't ask why I can't pre populate the combo box with all possible country codes as it is a finite set with infrequent changes, it is the customer requirement and besides there are several controls like this (test center, etc).

Now the way currently the combo box works is, if the user types in a new entry, I add the new value to the database and refresh the dataset. I do this so the new entry has a proper unique code in the database. This slows down the application a bit though it is not a frequent operation. Currently I also set the AutoCompleteMode property to SuggestAppend. This is to prevent users from adding a duplicate entry or entering misspellings of existing entries.

What is the best practice in this regard? Is it better to add new entries in memory and add them to the database at a later stage? It is a multi-user application and it is also possible someone else could also be adding the same entry though it is rare.
ol muserTechnology GeneralistAsked:
Who is Participating?
AngryBinaryConnect With a Mentor Commented:
OK, though I don't think that changes the concept much.

So, they add a new value, and you're saying that there's, for instance, a hiccup in the UI as a database call is made? You can make the call asynchronously, or on a different thread. But, I'm doubtful as to whether that's even advisable. At some point, I imagine the user will have to commit the values that they've entered to a database, so that would be the ideal moment to insert a new value.
Is this an Internet or an intranet application?

For intranet, I think what you are doing is fine. You may want to prevent race conditions on inserts by doing new value inserts via a stored proc that validates that the value doesn't exist and inserts atomically.

Storing the values in memory for later add because it adds a bit of complication to the application. You would have to implement, for instance, an IHttpModule Dispose event to ensure delivery of values, and even then that isn't reliable in the case of a server crash or hardware failure (which, based on the description of the client's requirement, seems like it would be a reasonably acceptable loss). It also implies you would have to maintain a cache in server memory of all country code values, otherwise building the data set would be complicated if you had to refer to two different data sources (one from the database, one from memory). IMO, this is more engineering than should be required for a simple feature like this.

It's a little difficult to believe that the slowdown is that significant as a result of adding a country-code value. What is more likely is that it adds an additional post back to the process of filling out a form, and this can likely be avoided by either performing the new value insert along with the rest of form processing, or by doing so via AJAX.
ol muserTechnology GeneralistAuthor Commented:
Sorry I forgot to add this. This is a windows forms application running on WinXP possibly running on Win7 someday, both 32 bit.
AndyAinscowFreelance programmer / ConsultantCommented:
Have you asked the client how they intend to cope with incorrect entries?

You have En  England as a choice but someone accidentally types in Em then adds a new entry for England.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.