Generic jQuery Function to Remove CSS Classes
I've been using a lot of jQuery lately in a new project and am falling in love with it! It is a wonder why I have never used it before but am glad I was kind of forced to learn it ;)
As with anything new there is a bit of a learning curve. The application I am working on boasts a large number of tabs for different sections of the site. One of the requirements is to toggle the "active" tab via CSS class. Easy right? This was easy to do in vanilla JavaScript so it should be super-easy to do with jQuery. It is!
$('#FooItem1').removeClass('active');
$('#FooItem2').removeClass('active');
$('#FooItem3').removeClass('active');
$('#FooItem4').removeClass('active');
$('#FooItem5').removeClass('active');
That works great but there is one caveat: adding new tabs. If the requirements of the UI changed and we were to have to add a new tab (or 6 more) then we would have to not only change the view (or in this case partial view ;) ) but also the JavaScript functions dealing with these tabs. Since all of our tabs are UL with stylized LI's containing anchor tags I decided to create something like this:
function
RemoveActiveClassFromListItemControlByID(controlID) {
$("#" + controlID).children().each(function() {
$(this).children("a").removeClass("active");});
}
Now this isn't that generic since I have the "a" and "active" strings hard-coded. In this case it works for us since all the tabs are the same format and all we really need is the id of the control. This little function will enumerate the children of the control (in this case all LI's and then enumerate the A children and remove the active css class. Simple. To extend this to be even more generic you could do the following:
function
RemoveClassNamesFromChildElementByControlID(controlID, childElement, className) {
$("#" + controlID).children().each(function() {
$(this).children(childElement).removeClass(className);});
}
Here's an example of how you could use the function above. Let's say that you want to enumerate all children in a OL with an id of "fooList". Each children has a span tag with a css class of BAR and you want to reset all of them when a user clicks on a hyperlink. This would be extremely simple:
<
a href="javascript:(RemoveClassNamesFromChildElementByControlID('fooList', 'span', 'BAR'));">CLICK HERE</a>
Easy. I'm really starting to love this jQuery business.
jQuery + MVC = love

SQL Server 2008 Save Not Permitted Dialog Box
I was creating some tables this evening in a SQL Server 2008 database this evening so that I could sandbox some MVC functionality for a current project. I had designed the tables according to the tutorial only to find out that this wasn't the case. I'll just log back in to SQL and make the necessary changes. Easy.
When I added the missing column I went ahead and reordered to match the same order of the tutorial because, well; I am a little OCD at times. I remember doing this in the past pre-SQL 2008 with no trouble. This time I was hit with this:

Weird. Again, I had never seen this before so I was a bit surprised.
According to SQL Server 2008 Books Online this can be caused by any of the following:
- Adding a new column to the middle of the table
- Dropping a column
- Changing column nullability
- Changing the order of the columns
- Changing the data type of a column
Really? These all seem like fairly common tasks when working within a database. As odd as this may seem (maybe there is a good reason for this?), there is a very easy fix.
From the Tools menu click on Options, expand Designers, click on Table and Database Designers. Select or clear the Prevent saving changes that require table re-creation option.
Much better. Back to work.

SQL 2008 and The Setup Failed to Read IIsMimeMap Table
Decided to install SQL 2008 recently because of it being 2009 and all. What an adventure. Although not near as bad as upgrading from Vista Home Premium to Vista Ultimate, it sucked pretty bad.
I downloaded the 120-day trial Developer Edition as a self-extracting executable because I didn't feel like burning a DVD / using Daemon Toolz. The download was the smoothest part of this whole deal.
From the installer screen I chose the Upgrade from SQL 2000 / 2005 option because I have SQL 2005 Developer Edition installed so this made the most sense. Miserable fail. I kept erroring out when trying to install SQL Reporting Services. The exact error(s) escape me but I remember it having something to do with authentication and the report service. Naturally I thought to manually stop and start the service to see if there was any issue. Good thing because manually starting the service failed. It then dawned on me that between the time I had initially installed SQL 2005 and that very instant I had changed my Windows password. Right-click, 'Properties' and changed it to my current password, restart the service: SUCCESS!
Now let's try upgrading again: FAIL!
After hitting the Google fairly hard I read a couple of blog entries detailing how uninstalling SQL Server 2005 entirely from the Control Panel would remedy the situation. I tried this and received the following error when attempting to uninstall SQL 2005 Reporting Services:

Awesome. Have no idea what this means so I searched Google for it. Apparently no one else does either. There were tons of suggestions as to how to go about remedy'ing this so I tried a couple. These solutions ranged from reconfiguring Reporting Services to uninstalling and reinstalling IIS7. I opted to try and reconfigure Reporting Services only to find that the configuration tools no longer existed on my machine seeing as how that was successfully uninstalled before receiving the error message above. I then tried to uninstall again and received this error message.
Same error, different error code. Weird.
The final solution was to install each component of SQL 2005 one-by-one from Control Panel. As crazy as that sounds it actually ended up being successful.
So, if you are getting these error messages try uninstalling each and every SQL 2005 component one-by-one and then do a clean install of SQL 2008. It worked for me.

LAMP Developer Needed
Checked an e-mail account that has apparently been exluded from my main Send/Receive group in Outlook today and found this e-mail in the Junk E-mail folder.
Maybe I should use this to setup a rule...
;)

I have learned a bunch of exciting things over the past couple of months but unfortunately it's proprietary and I can't blog about it!
I was never one to blog about where I work or what projects I have been working or else I'd have a ton of new entries.

Microsoft Office Interop Outlook & C# For Outlook Searches
I had a need yesterday to query a bunch of Outlook e-mails: 44,743 to be exact. This was the result of a clients mailing list account being setup incorrectly and we just found out about it! An e-mail account was setup strictly for the mailing list and it was never properly setup as an Outlook account locally. No big deal. When the hosting provider contacted us to clean it up I noticed that there were a BUNCH of bad e-mail addresses (Return to sender, bad account, etc...). At first I started to click on individual e-mails, copy the bad e-mail address to a .txt file, then search that folder for the same e-mail address and delete the results. That takes FOOOOORRREEEEVVVVEEEEERRRRRRR. Then I realized I could dork around with Outlook via a C# console application and harvest the e-mails and output them in a sorted Excel spreadsheet.
Enter Microsoft.Office.Core and Microsoft.Office.Interop.Outlook namespaces. I decided it would be best to create a little C# console application utilizing these namespaces to search the e-mail, write a little regular expression pattern (by write I mean copy & paste one from the interwebs) to parse out e-mail addresses, and add e-mail addresses that didn't have the host name or the reply e-mail account in it to a List<string> (if it didn't already contain it!). It was pretty easy.
First:
using
Outlook = Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Interop.Outlook;
I'm not going to bore you with the entire application unless you really want it. I'll just get you to the point where you can start to do stuff with your e-mails.
Now you need to instantiate your Outlook object.
Outlook.Application application = new Application();
Outlook.NameSpace nameSpace = application.GetNamespace("MAPI");
Outlook.MAPIFolder mapiFolder = nameSpace.GetDefaultFolder(OlDefaultFolders.olFolderJunk);
It might be worth mentioning the olFolderJunk business you see there. In the interest of time I didn't want to mess around with trying to navigate to the folder that I actually stored these e-mails in since I have a bunch of subfolders in Outlook due to using multiple accounts and a handful of rules and what-not. So in the interest of time I moved all the e-mails to the junk folder since I could easily access it. I think the correct syntax would be something like:
nameSpace.Folders["FOLDER"].Folders["SUB"].Folders["YOU_GET_IT"];
Moving on. Once you finally get access to your designated folder you need to start being able to do stuff with the items in that folder. It's as easy as enumerating the, you guessed it; MailItem objects a la:
foreach
(MailItem mailItem in mapiFolder.Items)
{
// do stuff
}
Easy. The properties are fairly braindead as well. MailItem Object Members
Have fun. I'll probably use this in the future as well.

Dilbert & Agile Programming
Just saw this cartoon this morning. Late pass I'm sure ;)


Silverlight ObservableCollection Bug Will Be the Death of Me
BUG: Items are sorted correctly sometimes, other times not so much.
FREQUENCY: Intermittent, never when debugging and stepping through everything, always seems to happen when I'm not.
RATING: Probably the most painful bug I've worked on in some time.
So I've got an ItemsControl in a custom XAML user control. All this does is display an IEnumerable<T> of video items. Simple. To provide a better user experience we implemented an ObservableCollection<T> as the source of the ItemsControl with a Collection_Changed event so we can add one item at a time to the collection so as to not wait for the entire collection to populate before displaying to the user. Now we're able to display the first 2 items and the scrollbar height shrinks as items are being added. It's a nice user-experience.
Anyway, so for the implementation. When the VideoRetrieveComplete event fires (after making a web service request for the data) we call the UpdateVideoCollection method.
UIThread
.Run(delegate
{
videoListControl.UpdateVideos(args.VideoItems);
videoListControl.itemsControlVideoListing.DataContext = new VideoItem();
}
Notice that we're doing this in the UI Thread. The code above is located in our Controller class. The args.VideoItems is an IEnumerable<VideoItem> and they are sorted in the order they need to be in. I have verified this time and time again with painful debugging.
Now time for VideoList controls properties, objects, and methods:
public
int CurrentPageIndex { get; set; }
public ObservableCollection<VideoItem> ObservableCollection { get; set; }
private List<VideoItem> VideoItems = new List<VideoItem>();
Constructor where everything is initialized. Probably important to include this.
public
VideoList()
{
CurrentPageIndex = 0;
InitializeComponent();
ObservableCollection = new ObservableCollection<VideoItem>();
itemsControlVideoListing.ItemsSource = ObservableCollection;
ObservableCollection.CollectionChanged += CollectionChanged;
}
public
void UpdateVideos(List<VideoItem> videoItems)
{
UIThread.Run(delegate
{
VideoItems.AddRange(videoItems);
ObservableCollection.Clear();
if (VideoItems != null && VideoItems.Count > 0)
{
VideoItem videoItem = VideoItems[0];
VideoItems.RemoveAt(0);
ObservableCollection.Add(videoItem);
}
});
}
void
CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (VideoItems.Count > 0)
UIThread.Run(delegate
{
VideoItem videoItem = VideoItems[0];
VideoItems.RemoveAt(0);
ObservableCollection.Add(videoItem);
});
}
The reason for removing an item, and then re-adding it is that this is the only way we can get our Converters to fire off, otherwise the collection hasn't changed and UI elements don't update. For example, switching between tabs the user can add videos to their playlist. Each time the tabs are selected and the VideoListing populates the VideoItems within that listing needs to show either an ADD / DELETE FROM PLAYLIST button depending on whether or not the item is in the playlist. If there's another way to get those Converters to fire off I'm all ears.
So the problem is that more often than not the videos, which come in sorted correctly, are not always displayed that way. For example, if I have 5 videos with dates 10/1/2008, 10/1/2008, 9/30/2008, 9/29/2008, 9/28/2008 sometimes the sort will be:
10/1/2008
9/30/2008
9/29/2008
9/28/2008
10/1/2008
or:
9/30/2008
9/29/2008
10/1/2008
9/28/2008
10/1/2008
You get the idea. Could it be that we are not implementing this correctly? Do we need to rebind the ItemsControl or is this correct?
I've been working on this bug for a day and a half now and have nothing. I'm sure it's probably something stupid.
I'm all ears if you have anything. ANYTHING!
Oh, should probably add that if I page to the next listing of videos and then back it'll remedy itself!

What's Wrong With This Picture?
Tried to debug a new Silverlight application today. By new I mean it was hosted in a different web project then the one I had been previously working on due to the fact that I needed to test some stuff locally. I was trying for about half an hour to get this thing to debug. The application would start to load up and then just hang on the loading animation. A co-worker told me to make sure I had the Silverlight debugger checkbox checked. I then go into the appropriate property tab and am greeted with this:

I remember this debugger option used to be located in the appropriate Debuggers group box. Apparently when I performed my Visual Studio 2008 and .NET Framework updates my Visual Studio 2008 UI ended up getting mutated.
Very strange.

Way to Go Vista File Transfers!
Better grab a lunch, or 9.


The Zune Update Totally Sucks
Updated the Zune application software this morning at about 7:00am. Since I had no choice in the matter, I went ahead and did it ;)
Wow. Talk about a change... for the worst. The application seems to lag a lot even when I have nothing else open. If I try to listen to it with Visual Studio 2008, SQL Management Studio, and Outlook forget about it. Too bad because those are all the things I use on a daily basis for work. That's also when I listen to the most of my music.
Luckily I've got 4 stereos in the house wired up to my Zune player so depending on where in the house I'm working (laptop) I can listen to my music. I've also got 2 30GB zunes for this and am thinking of buying another when the new line is released (if it hasn't been already).
Like any Microsoft product there will be a patch released for the Zune software if enough people are experiencing these symptoms and at the end of the day I'm still glad I don't use any Apple products.
Does anyone else find that the Zune software responds a lot like a poorly-written RIA (rich internet application)? To me it responds like Silverlight applications do when they are first written; before any story board animations / overlays are added to let the user know that SOMETHING is actually happening. Just a thought....
If anyone from Zune reads this you might want to update the Zune Card widget (see left navigation column) to utilize Silverlight instead of Flash. It would probably be a good look since, you know; you're Microsoft and you have this new technology you want to promote.
Looks like I'll try this again later. Maybe rebooting (again) will help.
UPDATE
Unresponsiveness of the application aside, the hardware update seems to be pretty sweet! Now I can play games while listening to tasty licks by The Chameleons. The wireless feature is pretty sweet. I wonder if I have to be at one of 9,800 McDonald's restaurants to be able to download? I'd guess not...
