Microsoft Pivot Blog Viewer

26. July 2010

Ok, I built something I am excited about. The one biggest gripe I’ve had with blogs is that it’s often hard to sort and filter the posts to find the posts I’m most interested in. In comes Microsoft Pivot. Microsoft Pivot allows for viewing data in an intuitive filterable manner in a completely visual context. Incredibly cool for filtering images, magazines, or even the magic the gathering card collection. Take that single column blog lists!

My idea last night is to view blogs through Pivot. I set about programming the cXML schema into a series of .Net classes, and then built an input mechanism to build a static cXML file with my blogs postings. It’s still in the early phase of just the roughest information, mainly blog title and descriptions, but given another day of polishing will have additional meta data with appropriate blog links. I also realize how terrible my categories are since the Pivot category filtering is very limited with my junk data input.

Below is a screen shot of the very rough data output I created with cXML for viewing in Microsoft Pivot. Now that I have my framework together I can expand the idea to anything with categories.

PivotBlog

I will keep the cXML path displayed in the Pivot screenshot so if you’re interested you can also view the generated blog Pivot output as it goes through the transition to getting cleaned up totally.

Programming, .Net, Third Party Controls

VB.Net inline XML loop

20. July 2010

Coming from a C# background I was a little jealous of VB literals (still hate VB.) Recently I was flung into the VB.Net world on a new project, and my only consolation was finally being able to work with VB literals first hand. The first thing I needed to do was iterate a collection of objects to custom build an XML message. I did a little Google, and Bing action to no real results on looping a collection within XML literals. Eventually I found the solution as XML literals do not directly support loops as we know it. Fortunately that is where LINQ steps up to the plate:

   1:  Dim xml = <Processes>
   2:                 <%= From process in Processes 
   3:                       Select <Process><%= process.Name %></Process>
   4:                 %>
   5:            </Processes>

In this example I’ve created an XElement variable called “xml” that contains the literal XML. Using a LINQ query within the “Processes” element the select query will iterate through the items in the processes collection object. Each item will output a process element that will be included within the processes element.

.Net, Programming

.Net Extension Methods – Extending ViewState With Generics

15. May 2010

I may be a little late to the party on mentioning how much extension methods have made my life easier. Extension methods tend to fill that gap between wanting to add an extra little tweak to an existing class without actually having to implement a customized inherited version. Previously whenever you wanted to use one extra tweak or feature you’d be responsible for making sure to replace all instances of the inherited class with your new version. That gets into supporting the new class when all you wanted to do was change or add one thing to an existing class!

In my case I just really wanted to stop having to check whether a ViewState value was null before trying to unbox the value into whatever value type or class I thought I stored in that key’s value field. In my case I really wanted to add generic typing support to the ViewState property of the page with as little work as possible. That is exactly where Extension methods come to the rescue.

For example normally one would see the following code to access a ViewState property:

   1:  private int GetGroupId()
   2:  {
   3:      int groupId = -1;
   4:   
   5:      if(ViewState["CurrentGroupId"] != null)
   6:      {
   7:          groupId = (int)ViewState["CurrentGroupId"];
   8:      }
   9:   
  10:      return groupId;
  11:  }

Ideally I would like to do the following:

   1:  private int GetGroupId()
   2:  {
   3:      return ViewState.Get<int>("CurrentGroupId", delegate { return -1; });
   4:  }

To do this I’d use a .Net extension method to extend the ViewState property through it’s StateBag class. Below you’ll find the ViewStateHelper class with all the required code to extend the StateBag class, thereby extending the ViewState property, and adding support for generic Get/Set functions. In order to use it just add the namespace where the static class is contained to the “usings” on the code-behind file of the page needing the ViewState extension support.

   1:      public static class ViewStateHelper
   2:      {
   3:          /// <summary>
   4:          /// Returns an object from view state
   5:          /// </summary>
   6:          /// <typeparam name="T">Type of object</typeparam>
   7:          /// <param name="key">View state key of object</param>
   8:          /// <param name="delegateDefault">If view state is empty, a delegate to return a value to put in view state.</param>
   9:          /// <returns></returns>
  10:          public static T Get<T>(this StateBag viewState, string key, DefaultMethod<T> delegateDefault)
  11:          {
  12:              T viewStateValue;
  13:              
  14:              if (viewState != null &&
  15:                  viewState[key] != null &&
  16:                  viewState[key].GetType() == typeof(T))
  17:              {
  18:                  viewStateValue = (T)viewState[key];
  19:              }
  20:              else
  21:              {
  22:                  viewStateValue = delegateDefault();
  23:                  viewState.Set<T>(key, viewStateValue);
  24:              }
  25:   
  26:              return viewStateValue;
  27:          }
  28:   
  29:          /// <summary>
  30:          /// Put an object in view state
  31:          /// </summary>
  32:          /// <typeparam name="T">Type of object to put in view state</typeparam>
  33:          /// <param name="key">Key of object to put in view state</param>
  34:          /// <param name="value">Object to put in view state</param>
  35:          public static void Set<T>(this StateBag viewState, string key, T value)
  36:          {
  37:              if (value != null)
  38:                  viewState[key] = value;
  39:              else
  40:                  viewState.Remove(key);
  41:          }        
  42:      }

.Net, ASP.Net, Programming

Visual Studio 2010 Beta 2 Needs Work

16. January 2010

I’ve been trying to use nothing but Visual Studio 2010 for my at-home development uses. For the most part it works decently until it decides to be not be so cordial. I’ve seen things from group boxes all of a sudden only half the text on any contained controls displays. Restarting Visual Studio does nothing to redisplay the text even though it’s clearly in the Text property window. The only solution to that seems to be to add or change one character on any of the contained controls and auto-magically the rest of the text in all the controls displays properly again within the group box. That one has happened several times already.

There has also been the fancy quirks of leaving Visual Studio 2010 running and using it through a remote desktop connection. That one causes some strange behavior from getting stuck in a frozen state, not releasing locks to referenced assemblies, which that assembly part sucks when said assemblies solution is open in another Visual Studio instance you want to compile. That may have just been something weird between Visual Studio 2010 as the main windows form project and Visual Studio 2005 as the assembly project. The worst being when switching between code pages where the code-behind looses the cursor. Just stops showing up, don’t know where you are, but it can still move and select text using the mouse or keyboard. Just the funny thing is the text doesn’t show itself being selected either, but you can definitely ctrl-c it and it copies find to the clipboard.

Who knows, but the oddities are adding up quickly. I just had Visual Studio 2010 crash for the third time for unknown reasons as a result of adding an About Form to the project and modifying the About Form’s form wide text. Open the project, close all code behind, open the About Form, right-click to view code, and then it prompts to restart itself. I think that means go to bed and shut down for the night.

Programming, .Net

DataGridView Drag and Drop with Multiple Files

13. January 2010

I was working on a project tonight and I noticed something kind of odd. There are a ton of references out there for how to handle the DragDrop event with a single file, but I wasn’t able to find really anything about handling multiple files. Needless to say I figured I’d make a post to remedy this problem.

First thing to note is to select the DataGridView in the designer and from the properties window set “Allow Drop” to “True" to enable the events. Second is to add an event handler to the DataGridView DragEnter event. Within the event simply set e.Effect to one of the desired DragDropEffects enumeration values as such:

private void dgvImageFiles_DragEnter(object sender, DragEventArgs e)
{
     e.Effect = DragDropEffects.Copy;
}

The next step is to simply add an event handler for the DragDrop event and use e.Data.GetData(“FileDrop”) to get a string array containing the full file paths to each file dropped into the DataGridView control. That was at least my desire when building this code, but you could just as easily handle any of the other data format values available from e.Data.GetFormats() function. Below is the DragDrop event I’ve used to get the files dropped into the DataGridView control:

        private void dgvImageFiles_DragDrop(object sender, DragEventArgs e)
        {            
            if (e.Data.GetDataPresent("FileDrop"))
            {
                string[] files = (e.Data.GetData("FileDrop") as string[]);
 
                foreach (string file in files)
                {
                    . . . 
                }
            }
        }

That is how to handle drag and drop with the DataGridView with multiple files. Easy enough eh? Well then back to programming I go.

.Net, Programming

Blog Engine Spam Cleanup Utility

9. January 2010

While this is totally a reactive approach to dealing with the spam bots posting comments I thought I’d share the clean-up project. In the end I have to give the bots credit for being so kind in nature of appreciative comments. It’s easy to get a big head reading the comments even though they are clearly bots with scripted content.

Attached is the project along with the XML block list. The block list is not complete with equal email, author, IP, and website listing for all the comment postings I received. It was sufficient enough for my needs to clean the comments up. The list may vary depending on the different bots that have visited the blog site, but should allow anyone in the same situation to clean up the mess. The code is roughly written and meant for a one-off cleanup, but below is the code along with the full project that includes the XML block list.

Blog Comment Cleaner Utility

Please note the project was created with Visual Studio 2010 beta 2 in case of difficulty opening the solution file.

Example Command Line:

BlogCommentCleaner.exe “c:\blog\App_Data\posts” “c:\blog\App_Data\block.xml”

Programming, Programs & Utilities, .Net

Blog Engine Spam Cleanup

7. January 2010

After reviewing my web logs I’ve noticed a big uptick in the number of external links to spam sites. I knew about the spam bots posting tens of hundreds of comments on this blog and I promptly shut down comment postings. The bad thing is I authorized all of them to appear not realizing I had set comments to being moderated, and promptly forgot to authorize any comments. That’s why nobody ever saw or heard from me in reply to legit comment postings. ~Sorry~

I took it upon myself to write a blog engine comment spam cleanup utility. It’s basically a console application that just runs through the XML postings (not database driven) using LINQ to XML to query comments looking for criteria on Author, Email, IP, and Website. These elements exist within the posting files within the comment element, and was core to the filtering process.

Essentially I just created a new XML filter file called ‘blocked.xml’ that I created using the following structure:

   1:  <block>
   2:      <emails>
   3:          <email>...</email>
   4:      </emails>
   5:      <authors>
   6:          <author>...</author>
   7:      </authors>
   8:      <ips>
   9:          <ip>...</ip>
  10:      </ips>
  11:      <websites>
  12:          <website>...</website>
  13:      </websites>
  14:  </block>

From there I load each section into a generic list, using LINQ, and loop through creating a new XDocument for each posting file looking for the comments element’s child comment elements. It queries each comment section in each post looking for any comment that contains emails/ips/author/ or websites contained in the generic lists created from the block list file. Upon finding an offending element it removes its parent element, being the comment element, and removes it from the comments element. Thus removing forever the spam that currently exists from all posting files based on the block list.

The problem is building out the block list. Don’t want to arbitrarily remove valid comments. Luckily most of the spam bots that have taken root on the block share many of the some websites even though email addresses are often random for the same bots. I’m still building out my block list and at this point only have a hundred or so blocked elements added. I will need to most likely script out the creation of the block list so I don’t have to manually build the list. Still so far after manually building the list for an hour it’s dropped the existing spam count by a quarter of what it used to be. Not finished, but it’s getting there.

All of this work brought out an interesting fact that there is no option to remove all comments from view in the blog comment settings, which would have been nice to reduce external linking to websites with T & A related content. You’d think since it can enable and disable allowing comments there would equally be a show/hide all comments setting for those not looking to ever allow comments. Probably pretty niche, but still would be nice for just such an spam infestation occasion.

Programming, Programs & Utilities

Weird Programmers?

18. October 2009

Came across an article where the question was asked whether software developers are naturally weird. That is a tough question. When you examine our occupation its not hard to see that it’s very difficult to be a software developer. It’s down right exhausting, irritating, and puts us in a constant state of tension trying to create works of art at fast-food speeds.

I suppose a programmer that seeds in little comments here and there of song lyrics in the code seems like a lesser evil than say a disenfranchised employee coming in and shooting up the place. Programming at it’s most basic state is equivalent to writing a novel every day, not even a novel you might be interested in, but a novel none the less. It’s constantly coming back day and day again and writing more paragraphs. Even writers get writers blocks, and at least get to pick the stories they’re interested in working on. Software developers have to sit down and work on a book someone else is dictating to us, and they expect it to be a totally flawless story written exactly how the end user pictures it.

Day in and day out we’re writing another chapter, fixing a paragraph here and there, and worse yet sometimes have to edit another persons story. That’s the worst part when working on someone else’s contribution because maybe they were spinning off the story in a totally different way than they should have. Basically have to go back in, figure out the plot, and start fresh with a new middle or end. To top it off some stories require you use a different typewriter (language) so you’re constantly changing the equipment used to write these stories. We’re like doctors in that we constantly have to train until the day we leave the practice on new techniques and procedures, but without the glory or the respect.

Yah, i suppose we’d have to be weird to put ourselves into this position. A persons natural approach to things beyond their control is to either laugh or cry about it. I think as a profession we tend to take the humor approach by creating humor that others of our own types can take solace in. A funny saying here or there, seeding in a special action if someone searches for “Chuck Norris” on a site (not naming names here), or even song lyrics near code that should be written so clear real comments were not even necessary..

I suppose yes, we are a little weird, but at the same time we’re able to do something most people cant. I think we deal with our stress by approaching life and work with a greater sense of humor than any other profession. Those who can’t see this probably spend too much of their day taking things a little too seriously for their own good. Still there is a balance in that we can be quirky and funny here and there, but we’re still professionals. There are times to act professional.

Programming, Offbeat