Douglas Adams on teaching programming

What I mean is that if you really want to understand something, the best way is to try and explain it to someone else. That forces you to sort it out in your own mind. And the more slow and dim-witted your pupil, the more you have to break things down into more and more simple ideas. And that’s really the essence of programming. By the time you’ve sorted out a complicated idea into little steps that even a stupid machine can deal with, you’ve certainly learned something about it yourself. The teacher usually learns more than the pupil. Isn’t that true?

Douglas Adams in Dirk Gently’s Holistic Detective Agency

Enum friendly names via DisplayAttribute and DescriptionAttribute – MVC 5 edition

Are you using enums in dropdowns or radiobuttons or something similar? Instead of hard coding display names, a way of reusing these names was coined in the MVC 3 days that involved decorating the enum with the DescriptionAttribute. Judging from posts on stack overflow, this was a very popular method.

When MVC 5 came out, it included new helpers for Enums (which was fantastic). However, MS used the typical DisplayAttribute (the DisplayAttribute.Name property to be exact) instead of the DescriptionAttribute to pull this off.

I wrote an Enum extension method that attempts to grab the name from both of these attributes. This method should work great with legacy and the newer MVC apps.

Keep in mind, that it is probably best to be consistent and use only DisplayAttribute in future projects, as this basically deprecates the DescriptionAttribute method but this could be useful for legacy projects that have been updated to MVC 5.

public static class ExtensionMethods
{
    /// <summary>
    /// Returns friendly name for enum as long as enum is decorated with a Display or Description Attribute, otherwise returns Enum.ToString()
    /// </summary>
    /// <param name="value">Enum</param>
    /// <returns>Friendly name via DescriptionAttribute</returns>
    public static string ToFriendlyName(this Enum value)
    {
        Type type = value.GetType();

        // first, try to get [Display(Name="")] attribute and return it if exists
        string displayName = TryGetDisplayAttribute(value, type);
        if (!String.IsNullOrWhiteSpace(displayName))
        {
            return displayName;
        }

        // next, try to get a [Description("")] attribute
        string description = TryGetDescriptionAttribute(value, type);
        if (!String.IsNullOrWhiteSpace(description))
        {
            return description;
        }

        // no attributes found, just tostring the enum :(
        return value.ToString();
    }

    private static string TryGetDescriptionAttribute(Enum value, Type type)
    {
        if (!type.IsEnum) throw new ArgumentException(String.Format("Type '{0}' is not Enum", type));

        string name = Enum.GetName(type, value);
        if (!String.IsNullOrWhiteSpace(name))
        {
            FieldInfo field = type.GetField(name);
            if (field != null)
            {
                DescriptionAttribute attr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
                if (attr != null)
                {
                    return attr.Description;
                }
            }
        }

        return null;
    }

    private static string TryGetDisplayAttribute(Enum value, Type type)
    {
        if (!type.IsEnum) throw new ArgumentException(String.Format("Type '{0}' is not Enum", type));

        MemberInfo[] members = type.GetMember(value.ToString());

        if (members.Length > 0)
        {
            MemberInfo member = members[0];
            var attributes = member.GetCustomAttributes(typeof(DisplayAttribute), false);

            if (attributes.Length > 0)
            {
                DisplayAttribute attribute = (DisplayAttribute)attributes[0];
                return attribute.GetName();
            }
        }

        return null;
    }
}

Using SQL Server Reporting Services (SSRS) in an ASP.NET MVC project

There are a handful of examples out on the internet on how to use SSRS from an ASP.NET website but all of the ones I came across seemed like hacks. They range from throwing an ASPX page with a ReportViewer control to complex JavaScript hacks. The following method is the one I have used for many years that adheres to the true MVC manner. This method connects to SSRS through the web service using Microsoft.Reporting.WebForms assembly.

Continue reading

Podcasts you should listen to right now

Programming Related Podcasts

  • This Developer’s Life – Podcast in the style of This American Life dealing with programming related topics. An all time favorite of mine.
  • Hanselminutes – Scott Hanselman talks about web development, usually dealing with C#, javascript, ASP.NET, and other MS technologies.
  • Herding Code – K Scott Allen, John Galloway, and others talk about programming topics.
  • Programming Throwdown – The hosts pick a random technology and give you a broad overview of it followed by its advantages and disadvantages.
  • Floating Point – News, tech, programming, random topics are discussed.
  • Javascript Jabber – The JavaScript frameworks that are released approximately 20 minutes are discussed here.
  • Developer Tea – Podcast covering a diverse amount of topics relating to being a developer.
  • Iterate – UX, design, and related ideas are discussed in this podcast.
  • Debug – Mobile development is discussed in depth, mostly on iPlatforms.
  • /dev/hell – Uncensored ramblings of two insane developers.

Hall of Fame – My favorite non-programming podcasts

  • 99% Invisible – The best podcast ever? Short, well produced episodes dealing with design.
  • TED Radio Hour – TED Talks are a must.
  • Radiolab – The best production of any podcast ever made. Interesting and seemingly random discussions.
  • This American Life – I think this has been cited as the most popular podcast ever until Serial came along… which was also created by them.
  • Reply All – Random things relating to the internet are discussed. Gimlet Media is worth paying attention to as every podcast they release is solid gold.
  • Startup – A podcast documenting company creation from brainstorming, soliciting investors, creating a startup, running it, and more. Interesting look at something not normally public.
  • Freakonomics – Economists can be interesting too.
  • You Are Not So Smart – Self delusion… and cookies?

MSI Web Setup Project Error: Unable to build project output group ‘Content Files from SOMEWEB (Active)’

I stumbled onto this error and thanks to Scott Hanselman’s blog I was able to find a solution to it.

The only problem? My project had a TON of files marked as Content (such as multiple versions of angular) and I didn’t want to spend all morning going through the countless folders to figure out which files were missing in order to resolve the conflict. If only Visual Studio has some way to verify this and let you know which files were missing…

Oh wait, it does! Its just buried and very difficult to find.

You can get visual studio to display all these “yellow icon” missing files by using the Publish Solution option. For those of you unfamiliar with the publish wizard, here is what I did:

  • Right Click your website project and click publish.
  • On the publish wizard that pops up, select Custom and type in any name for the profile.
  • For publish method, I selected File System and chose a directory to publish to.
  • Click the Publish button and it will error and show you exactly which files were missing. You may have to do this multiple times until you get all the missing files resolved.

WebGL 3D Wireframe using GLGE

webgl

This is the same as the 3D wireframe example which was done in OpenGL, only ported to the new technology known as WebGL. WebGL is still a new technology that uses GPU acceleration to render 3D graphics in a web browser and it has shown vast potential to modernize the web.

Many browsers do not even fully support WebGL at the moment. As such, to view this example you will need an updated, modern browser such as Firefox, Chome, or Opera. Internet Explorer does not support WebGL rendering at the moment unless you install a plugin.

This demo relies upon the GLGE and GLGE_math libaries in order to render.

GridView Column Grouping in ASP.NET

If you are writing code to show a large amount of records in an ASP.NET GridView control, you should do your best to make them easily readable since it can be overwhelming. I’ve ran across a few different hacks using JQuery to enhance the presentation of GridView records, but this seems to lag the user’s browser when a large number of rows is encountered. I have wrote a small function that performs this action server-side and rids the user of the content-rendering JQuery lag spike.

Continue reading

SAS Data File to PostgreSQL friendly CSV using Perl

In my database course at Marshall University, I helped my professor with a side project involving Cancer research data that has been collected since 1973 by the National Cancer Institute. My first job was to take the SEER data and load it into a postgreSQL database.

The job was simple enough. The only problem? The data files were in SAS format. Basically the format was CRLF delimited entries composed of fixed-length fields. Here is a list of the fields and lengths that I used for the script.

Continue reading