Monday, June 29, 2009

Using a bit column to hold 30+ Checkbox values


Sometimes your asked to design an application that will contain ‘X’ number of checkboxes.  The DBA is a hardball and tells you no way am I creating 30 columns for your checkboxes, ill create one and you figure it out.  What a punk but lets see what we can do about this.  We could do one of two things, we could have the DBA create a vertical approach where we store the value of each checkbox in its own row, or we can have the DBA create a bit column and we bit flag this baby.  Well being the title of this article has “bit column”, you know what I’m going to talk about.  That’s right, we are creating a bit column.  Just a tad bit about bit flags since we are not really focused on explaining them in this article is that they are powers of 2.  Meaning our values stored as enums will increment to the power of 2 starting with the value 1.


public enum CheckBoxValues
first = 1,
second = 2,
third = 4,
fourth = 8,
fifth = 16,
sixth = 32,
seven = 64,
eight = 128,
nine = 256

So here we show only 9 values, you however would continue on down the path of 30 as per this title.  But for clarity we will keep this simple.  Seeing that our values of the enum go from 1-256 does not mean your values will be stored in the db this way.  These are flag values using the power of 2, our db values will be the int value of the enum.  So “first” int value is 1 and “nine” int value is 9.  Now that we have that clear lets move on.

Ok, so we now have a bit column called “CheckedValues”, sounds good to me.  We need to store all our check box values into this one column.  The way we handle this is we treat the checkbox checked value as its enum value.  So for example we would have a check box called chkFirst and its checked value would be 1, chkNine would have a checked value of 9 and so on.  The checked values must match or be handled as the enum value (such as (int)CheckBoxValues.third will return 3).  This is important because we need to sum these values up.  So assume we are done and have all our checkboxes in place.  We are now ready to store/retrieve these values.  When the save button is hit, your code will then iterate through the checked check boxes and sum up their values.  So lets say first, second, and fourth are checked then our value would be 7 that gets stored in the database (1+2+4 = 7).

Setting the checkbox check state from the db value is just as easy.  Create a simple method like so:

private bool IsChecked(int dbValue, int enumValue)
return ((dbValue & enumValue) == enumValue);

Then you would simply handle the checked value like:

chkFirst.Checked = IsChecked(7, (int)CheckBoxValues.first);
chkSecond.Checked = IsChecked(7, (int)CheckBoxValues.second);
chkThrid.Checked = IsChecked(7, (int)CheckBoxValues.third);
chkFourth.Checked = IsChecked(7, (int)CheckBoxValues.fourth);

I am passing 7 since that is what the DB Value is in our example.  How is this working? Well its actually quite simple.  We are getting the BitAnd value of the enum and checking if its turned on in our db value.  So what is happening in the backend is something like this.  The binary value of 7 is 0111.  The IsChecked method is comparing this value to the passed enum flag value.  So a check looks something like this for second: 0111 & 0010 == 0010.  It passes because the 1 & 0 = 0 and 1 & 1 = 1.  So 0111 & 0010 does in fact = 0010 because starting from right to left we see 1 & 0 = 0, 1 & 1 = 1, 1 & 0 = 0, 0 & 0 = 0, total that up and we get 0010 (Remember I went right to left, so do the same totaling).  So that tells us this flag is on so check the checkbox.  The value 7 would check our 1, 2, and 4 enum values.  The exact ones we checked.  If the db value is 5 then first and fourth would be checked and so on.  This is a 32 bit column so naturally you could only store 32 bits of values in it but 30 checkboxes would easily fit in just one column.


Hope this helps you out there.

Thursday, June 18, 2009

Shrink those Urls


Those using twitter know about the benefits of shrinking a url to reduce character count, a tweet is limited to 140 characters so each letter matters.  Being a tweeter myself I tend to use tinyurl like most people out there.  It is the most popular for shrinking urls on twitter today. 

What if you wanted to not only shrink your urls but also track data about the urls you shrink.  Data such as where the person that clicked your links location is; or how many times they have clicked the link perhaps to show another colleague.  A nice solution that I found out there written by Andy Meadows is  This site is well laid out and tracks some very useful information.  I noticed AMD is one of their customers so I assume I am not alone in liking the service. 

A video is located on their home page which shows us some of the features, you can see it here:

BudURL Demo from Andy Meadows on Vimeo.


This is one of many great sites out there that shrink our urls to save space.  The benefit to is the addition of the marketing data collected that can assist your business in understanding who and where is viewing your site and how many times they have visited.  They have an enterprise version which allows you to use what ever url domain you want which is also very cool.

Tuesday, June 09, 2009

Windows 7 RC Experience Thus Far


I have been using Windows 7 now for about 1 month or so and I must say I really like it.  I was once on Vista and never more after seeing the light of an OS that does not slow me down.  On another note, the system I installed Windows 7 on is my new Alienware Laptop which has also been doing very well after the upgrade.  I have experienced some freezing of the system twice now, albeit that is better than how it was with Vista which was becoming a 3-5 time a day experience.  I haven’t really done much with the XP theme on individual applications nor have I dove into the VM of Windows 7 but I am enjoying the user experience I get with this OS and plan to upgrade the other 3 boxes with it when it comes out, or not.  I still haven’t decided what to do with my older machines.

Monday, June 08, 2009

XAF Custom Rule for Conditionally Required Properties


For those of you using DevExpress XAF (eXpressApp) you may have come across the need for making a Domain Object property conditionally required.  For Example, lets say you have a Domain Object with 3 properties (FirstName, LastName, and Phone) and you want Phone to be required only if FirstName and LastName are not null. How do you do it in XAF? Well I have a solution for you.  Simply download the source and apply the RuleConditionalIsRequiredAttribute to your properties like so:

public class TestRule : BaseObject
public TestRule(Session session) : base(session) { }


private string phone;
[RuleConditionalIsRequired("Phone is Required", DefaultContexts.Save, "!String.IsNullOrEmpty(FirstName) && !String.IsNullOrEmpty(LastName)", "Phone is required when First Name and Last Name are added.")]
public string Phone
return phone;
SetPropertyValue("Phone", ref phone, value);

As you can see you are writing C# code as the condition, I haven't tested in VB but since its using lambda expressions to compile this condition I'm sure it will work (I hope at least for VB coders).  It works great in my C# applications.  Your condition has to return bool and can only be used for properties.  The method CheckCondition will build a lambda expression and execute it against your class instance.  If the condition returns true then this property is required, otherwise it is not required.  The lambda expression class was developed by Microsoft and distributed with Visual Studio 2008 as a Sample application on using LAMDBA Expressions.

Now when we run this we get the following error: ValidationError

The Full Source is attached.

I hope this helps someone.

Happy Coding!!! Download Source Here

Creative Commons License
Blogged Information and Code is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.