Wednesday, April 14, 2010

Assembly Uses 'Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' which has a higher version than referenced assembly 'Microsoft.ReportViewer.WinForms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'




Problem:
Build a project that uses ReportViewer and you get the following error:
Assembly Uses 'Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' which has a higher version than referenced assembly 'Microsoft.ReportViewer.WinForms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
Solution:
Before you start, make sure the project is actually referencing Microsoft.ReportViewer.WinForms version 9.0. For some reason re-adding this in the references does not change the number in the project file. So after you are sure you're using version 9.0 do the following steps...

1. Open up the location of your project file. If you're using C# this is ProjectName.csproj
2. Open the project file with Notepad
3. Find the node containing Microsoft.ReportViewer.WinForms
4. Change the version number from 8.0.0.0 to 9.0.0.0 like the following picture:

References
1. http://davidsandor.com/blogs/tipsandtricks/archive/2008/07/16/solved-the-base-class-includes-the-field-reportviewer1-but-its-type-microsoft-reporting-webforms-reportviewer-is-not-compatible-with-the-type-of-control-microsoft-reporting-webforms-reportviewer.aspx  


 

Tuesday, April 6, 2010

How to select multiple values in an enum

Lets say you have an enum called DaysOfWeek, because you need to keep track of multiple days a user may select, because they may select different combos of days. The most efficient to store this is by using an enum with [Flag] attribute.

Like this:













Notice the values are are 2^x. This is because this is actually an 8-bit field.
We have [0000000], with a bit for each day.

So lets say you have Monday and Saturday chosen. This would be:
[1000010] = 64 + 2 = 66

Ok so how do you use this value?

First we need to STORE the values. We do this by using a bitwise OR.
Example: if we chose Monday and Wednesday we have Monday = [10] and Wednesday = [1000]. ORing these together 10 | 1000 = 1010, which in decimal is 10.

Next we need to LOAD the values. We do this by using a bitwise AND.
Example: To see if Monday was chosen we would do the total bit representation [1010] AND Monday [10] so 1010 & 10, which will return [10], which in this case is Monday.

How to programmatically check items in CheckedListBox

You can simply use CheckedItemBox.SetItemChecked(int index, bool value).

If you need to set the value based on the values from an enum with the [Flag] attribute you can do this:

cbDays.SetItemChecked(0, (curSchedule.DaysOfWeek & DaysOfWeek.Monday) == DaysOfWeek.Monday);
Where cbDays is a CheckedItemBox.
0 means we are looking at the first item in the CheckedItemBox. In this case it's "Monday".

The next part, curSchedule.DaysOfWeek & DaysOfWeek.Monday) == DaysOfWeek.Monday is doing a bitwise AND to see if Monday is a selected value.


References:
1. http://msdn.microsoft.com/en-us/library/t0zxd5z5%28vs.71%29.aspx

How to get CheckedListBox selected values

Problem:
You want to get the selected checkbox values from a CheckedListBox in a Winform project.

Solution:
Here's a simple application to illustrate how to get the values from a CheckedListBox.

1. UI pre-select

















2. UI post-select


3. Here's the simple C# source for the Process button, which gets the values from the CheckedListBox and puts them in the "You selected" part



























So all you need to get the value is "CheckedItems".

If you want to store these values in a database you'll need to use an enum with the [Flags] attribute. See my other post for more details on this:
http://makguidetosoft.blogspot.com/2009/08/calculate-next-time-job-scheduled-for.html
There was an error in this gadget