skip to Main Content

Don’t hire ghost hunters who are afraid of ghosts

Your house is haunted. You can’t stand living with the ghosts anymore and decide to call in the professional, a ghost hunter. The ghost hunter comes, nicely equipped with all kinds of shiny ghost hunting equipment, and proceeds to immediately pass out and fall to to the floor with a loud thud at first sight of the ghosts.

Well, in a nutshell, that is the story of the new Event Viewer in Windows Vista and Windows Server 2008. The new Event Viewer looks very nice and all. Trouble is, it now won’t work when there is a syntax error in machine.config or other .NET Framework problems. Apparently, in Windows Vista and Windows 2008, someone at Microsoft decided to rewrite the Event Viewer utility (a perfectly usable and solid utility in previous versions of Windows) in .NET.

MMC could not create the snap-in. The snap-in might not have been installed correctly. Name: Event Viewer.

So, the lesson I learned from this is: don’t hire ghost hunters who are afraid of ghosts. Or, don’t write a utility designed to view monitoring and troubleshooting messages that can fail due to unnecessary dependencies. A critical system troubleshooting utility such as Event Viewer should be the last thing that fails.

What to do when you cannot run Event Viewer in Windows Vista/Windows Server 2008/or Windows 7?

If you get the following error:

MMC could not create the snap-in. The snap-in might now have been installed correctly. Name: Event Viewer. CLSID: FX:{b05566ad-fe9c-4363-be05-7a4cbb7cb510}

Try the following:

  • Fix any syntax errors in machine.config. Restore to a previously known working copy.
  • Rename EventVwr.exe.config in %WINDIR%\System32.

Using a Different Configured Binding in WCF Client

To programmatically switch bindings on the fly, you can do it via the constructor of the generated client:

var client = new WeatherClient(“MyEndpoint”);

“MyEndpoint” is the name of the endpoint defined in your config file:

<client>
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration="http1" contract="MyContract" name="MyEndpoint" />
</client>

WCF Client Error “The connection was closed unexpectedly” Calling Java/WebSphere 7 Web Service

If you get the following exception calling a WebSphere web service from your .NET WCF Client (service reference):

System.ServiceModel.CommunicationException: The underlying connection was closed: The connection was closed unexpectedly. —>  System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.

Try adding this code before the service call:

System.Net.ServicePointManager.Expect100Continue = false;

More info on the 100-Continue behavior from MSDN.

Convert List<T>/IEnumerable to DataTable/DataView

Greetings visitor from the year 2025! You can get the source code for this from my Github repo here. Thanks for visiting.

Here’s a method to convert a generic List<T> to a DataTable. This can be used with ObjectDataSource so you get automatic sorting, etc.

using statement: using System.Reflection;
...

/// <summary>
/// Convert a List{T} to a DataTable.
/// </summary>
public DataTable ToDataTable<T>(List<T> items)
{
    var tb = new DataTable(typeof (T).Name);

    PropertyInfo[] props = typeof (T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

    foreach (PropertyInfo prop in props)
    {
        Type t = GetCoreType(prop.PropertyType);
        tb.Columns.Add(prop.Name, t);
    }

    foreach (T item in items)
    {
        var values = new object[props.Length];

        for (int i = 0; i < props.Length; i++)
        {
            values[i] = props[i].GetValue(item, null);
        }

        tb.Rows.Add(values);
    }

    return tb;
}

/// <summary>
/// Determine of specified type is nullable
/// </summary>
public static bool IsNullable(Type t)
{
    return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
}

/// <summary>
/// Return underlying type if type is Nullable otherwise return the type
/// </summary>
public static Type GetCoreType(Type t)
{
    if (t != null && IsNullable(t))
    {
        if (!t.IsValueType)
        {
            return t;
        }
        else
        {
            return Nullable.GetUnderlyingType(t);
        }
    }
    else
    {
        return t;
    }
}

Problem with jqModal/jQuery JavaScript Intellisense and Workaround

Is anyone else experiencing a problem with Visual Studio 2008 Intellisense with jQuery and jqModal?

It seems that jqModal (a jQuery plugin for modal dialogs) is breaking jQuery Intellisense on my Visual Studio 2008 setup. Without a reference to jqModal.js, jQuery Intellisense works fine:

image

As soon as I add the reference to jqModal.js, the Intellisense stops working with this error:

Warning    1    Error updating JScript IntelliSense: …\scripts\jquery-1.3.2-vsdoc.js: ‘jQuery.support.htmlSerialize’ is null or not an object @ 1430:4

Workaround

To work around the problem, generate the script include tag dynamically so that the JavaScript Intellisense engine doesn’t see the jqModal reference at design time.

<head runat="server">
    <title>My Web</title>
    <asp:Literal ID="LitJqModalScript" runat="server"></asp:Literal>
    <script src="../scripts/jquery-1.3.2.js" type="text/javascript"></script>
</head>

 

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
 
    LitJqModalScript.Text = @"<script src=""../scripts/jqModal.js"" type=""text/javascript""></script>";
}
Back To Top