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.

Example:

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 www.budurl.com.  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 budurl.com 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:

[DefaultClassOptions]
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
{
get
{
return phone;
}
set
{
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

Sunday, May 03, 2009

Alienware Laptop Purchase

Well I got my new laptop, I’ve had it about 3 weeks now and unfortunately its completely frozen up on me about 7 times.  I have made a call to support and they thought maybe the memory was not seated correctly so we went through the routine of stress testing the system checking each memory slot individually.  Nothing came of it, the system only froze a couple of times but not consistently while testing.  Well sadly its still freezing up on me.  I hope the next call is more productive and we get this thing running as it should be.  Don’t get me wrong, this machine is a champ while its running, I would like to keep it running; not asking too much.

Thursday, April 30, 2009

ASP.NET DIY Intellesence

As a time killer I decided to figure out a way to incorporate intellesence into an ASP.NET application.  Note this is something I worked on for a few hours without time to refactor; there could also be better ways to accomplish this.  I used jQuery to handle all my javascript needs and just your basic .NET standard TextBox and ListBox.

What exactly does this do?

Well first of all I must instill again that this is basic; you can take this code and modify it to fit your needs.  This simply renders your basic intellesence from a populated listbox.  Being that it is not complete, if someone would like to take the reigns and send me the code changes to re-publish that would be great too.  My email is gary.l.coxjr at gmail dot com.

Current Known Problems:

  • Intellesence is not showing below the text
  • Intellesence only displays when the OemPeriod is pressed ‘.’ (would be nice to be able to press “Ctrl”+”Shift”+”Space”)
  • Select from list as you type is not perfect (Doesn’t always find the right item)
  • Double-Click on listbox does not post to textbox

Please keep in mind this is NOT production code and shall not be used as such.  You may use this at your own risk.

What can be done to make this more useful?

Since the intellesence works off the listbox, any ListItems you load in there will be displayed when a period is pressed.  Whatever is selected in the listbox when you hit “Space” or “Enter” is what will be entered after the period in the textbox.  If you populate it with a list of object properties, like in my example of class Person, when a user types Person and then hits the period they will be presented with the pre-populated properties that can be selected.

The Javascript can be found in AspxIntellesence.js file located in ~/JS directory.

Here is what we see:

Intellesence

Notice I started to type “S” and the intellesence moved to StreetAddress.  If I had pressed the space bar then you would get Person.StreetAddress as the javascript would handle this.

What is this useful for anyway?

Well I think it could be very useful if your designing a template builder that allows your users to design custom email messages, or campaigns.  This type of tool would allow your customer to build the template and from your backend code process it from a given business object.  Take my example, assume I had a class named Person and the above template was processed.  I could send personalized emails to a select group that may look like:

Welcome Gary Cox,
    We have your current street listed as 123 Some Street.

Simply by parsing out the [object property] and replacing with its value of the current instance.

As always, I hope this helps someone.  Happy Coding!!!

 

Download Source Code

Wednesday, April 22, 2009

A Hidden Gem when it comes to string separation

Most developers based on code I have seen separate strings like so:

for (int i=0; i<someData.Count; i++)
myStringBuilder.AppendFormat("{0},", someData[i]);

Then after constructing this string they need to remove the last separator

myStringBuilder = myStringBuilder.Remove(myStringBuilder.Length-1, 1);

I too have used this concept. However, there is another way to do this that a lot of people didn’t even know existed. The String.Join class; it takes 2 params, the separator and the array.

ArrayList<String> myList = new ArrayList<String>();
for (int i=0; i<someData.Count; i++)
myList.Add(someData[i]);
return String.Join(",", myList.ToArray());


This would return a string separated by a comma such as “Text,MoreText,EvenMore,AndLast”

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