Click here to Skip to main content
13,301,619 members (65,469 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


19 bookmarked
Posted 10 Nov 2008

Managing Enums and their Database Equivalents

, 10 Nov 2008
Rate this:
Please Sign up or sign in to vote.
Make enums easier to coordinate with database values


I like enumerations. They keep me safe, reduce error, and make the code more readable. However, since I do a lot of database work, they also cause synchronization issues. That is, my enums often have to match my database since I use them to represent values in data objects. It is also not uncommon to want to populate a list with values from an enum. I formerly used the database values to populate the list, but now I use the enum as my master and populate my lists from it.

The class I supply in this article solves some common problems:

  1. It synchronizes an enum with the database.
  2. It allows me to populate a list directly from the enum.

Using the Code

EnumManager provides three static methods:

  • UpdateEnum()
  • GetListFromEnum()
  • SeparatedValue()


UpdateEnum() iterates through the values of an enum, and updates a database with the changed values. This means that the values named in the code match the names of the values derived from reports generated from the database. UpdateEnum() does not remove values, since that would generate an error when foreign keys were involved. Of course, care must be taken not to change the values arbitrarily since it could lead to incorrect data, but it does guarantee that what the code says is what the database says.

I usually run the update at initialization time, such as in Application_Start for ASP.NET. If you have a lot of enums to update, it would be best to do it in a separate thread to allow the application to start more quickly.

The parameters are pretty obvious:

UpdateEnum(Type type, string connectionString, string table, 
    string idColumn, string nameColumn)
  • type is the enum created by typeof(MyEnum)
  • connectionString should be obvious
  • table is the string name of the table to be updated. It must exist already.
  • idColumn is the value column for the numeric part of the enum
  • nameColumn is the name of the column that contains the string part

Of course, other columns may exist in the table, but there must be a numeric column and a string column that represent the enum values.


In order to bind a list such as a listbox or dropdown, we need a data source. List<> makes a great choice. Using generics, I return a KeyValue<> pair for each enum value. This makes binding to Key and Value very easy. For a listbox, it might look like this:

ListBox1.DataSource = GetListFromEnum<MyEnum>();
ListBox1.DataTextField = "Value";
ListBox1.DataValueField = "Key";

EnumManager works one other bit of magic in providing the values for the list. It splits words out of the enum value at capital letters. Thus, “ValueOne” becomes “Value One” when bound this way. Enums don't allow spaces in names, but lists for human consumption should be separated. SeparatedValue() accomplishes this little trick.


Of course, SeparatedValue() can be used independently of lists and GetListFromEnum(). It simply takes an Enum (the value of on enum) and splits its name.


  • 10th November, 2008: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionHow to bind with description? Pin
asp_crazy_guy12-Sep-12 21:16
memberasp_crazy_guy12-Sep-12 21:16 
GeneralMy Way Pin
Dmitri Nesteruk11-Nov-08 10:35
memberDmitri Nesteruk11-Nov-08 10:35 
GeneralMy way Pin
PIEBALDconsult11-Nov-08 5:19
memberPIEBALDconsult11-Nov-08 5:19 
GeneralRe: My way Pin
LaurenCL11-Nov-08 7:13
memberLaurenCL11-Nov-08 7:13 
GeneralHmmm... Pin
Paul Conrad10-Nov-08 19:08
memberPaul Conrad10-Nov-08 19:08 
GeneralRe: Hmmm... Pin
Neil de Weerdt10-Nov-08 21:20
memberNeil de Weerdt10-Nov-08 21:20 
GeneralRe: Hmmm... Pin
LaurenCL11-Nov-08 7:15
memberLaurenCL11-Nov-08 7:15 
GeneralRe: Hmmm... Pin
PIEBALDconsult11-Nov-08 8:49
memberPIEBALDconsult11-Nov-08 8:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 10 Nov 2008
Article Copyright 2008 by LaurenCL
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid