ASP.NET Controls – Problem sorting GridView with SqlDataSource control

Let me start by saying that Microsoft don't consider this issue as a problem, as you can see here this is a “by design” behavior.

The problem is well described in the referred Connect feedback and it contains a workaround.

Although simple, the workaround requires you to always register the GridView Sorting event and make the tweak according to the current GridView settings. Well, if are like me you will forget to do it half the times needed.

So, I made a not so simple workaround that will take care of the issue for me.

I override the OnSorting method from GridView so I can handle the GridViewEventArgs instance and override its SortDirection value.

To turn this into a general solution I partially reproduce the ParseSortString method from DataTable to find out if the current SortExpression contains either the ASC or DESC keywords.

Here is the code:

public class GridView : global::System.Web.UI.WebControls.GridView
{
    protected override void OnSorting(GridViewSortEventArgs e)
    {
        if (!string.IsNullOrEmpty(this.SortExpression))
        {
            if (this.SortExpression.Equals(this.SortExpression))
            {
                bool isMultipleSortExpression;
                SortDirection? sortDirection = GetSortDirection(this.SortExpression, out isMultipleSortExpression);
                if (sortDirection.HasValue)
                {
                    // To undo bug in GridView.HandleSort(string sortExpression) and then in GridView.CreateDataSourceSelectArguments()
                    e.SortDirection = SortDirection.Ascending;
                }
            }
        }
        base.OnSorting(e);
    }

    private SortDirection? GetSortDirection(string sortExpression, out bool isMultipleSortExpression)
    {
        SortDirection? sortDirection = null;
        isMultipleSortExpression = false;

        string[] strArray = sortExpression.Split(new char[] { ',' });
        for (int i = 0; i < strArray.Length; i++)
        {
            string strA = strArray[i].Trim();
            int length = strA.Length;
            if ((length >= 5) && (string.Compare(strA, length - 4, " ASC", 0, 4, StringComparison.OrdinalIgnoreCase) == 0))
            {
                sortDirection = SortDirection.Ascending;
            }
            else if ((length >= 6) && (string.Compare(strA, length - 5, " DESC", 0, 5, StringComparison.OrdinalIgnoreCase) == 0))
            {
                sortDirection = SortDirection.Descending;
            }
            if (!sortDirection.HasValue)
            {
                break;
            }
        }
        if (sortDirection.HasValue)
        {
            if (strArray.Length > 1)
            {
                isMultipleSortExpression = true;
            }
        }
        return sortDirection;
    }
}

Enjoy it.

.NET – Determine Whether an Assembly was compiled in Debug Mode

Finding out whether an assembly was compiled in Debug or Release mode is a task we must do from time to time.

I know two ways of accomplish this:

Either attributes are applied to Assemblies and can be found in the Assembly Manifest but there are a major difference between them:

  • AssemblyConfigurationAttribute must be added by the programmer but is human readable.
  • DebuggableAttribute is added automatically and is always present but is not human readable

You can easily get the Assembly Manifest by using the amazing ILDASM from your Visual Studio Studio Command Prompt:

ildasm_1

ildasm_2

And if you double click the MANIFEST item you will get all manifest data.

Looking carefully you will find the DebuggableAttribute:

ildasm_3

And perhaps the AssemblyConfigurationAttribute:

ildasm_4 

AssemblyConfigurationAttribute

Locate the AssemblyConfigurationAttribute – this attribute can be easily interpreted: its value can either be Debug or Release

ildasm_5

DebuggableAttribute

If AssemblyConfigurationAttribute is not present then we must use the DebuggableAttribute to get our goal.

Since we cannot understood the DebuggableAttribute value we have to open the assembly from another tool and read this attribute content. There’s no such tool available out-of-the-box but we can easily create a Command Line tool and use a method similar to:

private bool IsAssemblyDebugBuild(string filepath) {
    return IsAssemblyDebugBuild(Assembly.LoadFile(Path.GetFullPath(filepath)));
}
private bool IsAssemblyDebugBuild(Assembly assembly) {
    foreach (var attribute in assembly.GetCustomAttributes(false)) {
        var debuggableAttribute = attribute as DebuggableAttribute;
        if (debuggableAttribute != null) {
            return debuggableAttribute.IsJITTrackingEnabled;
		}

    return false;
}

or (if you prefer LINQ)

private bool IsAssemblyDebugBuild(Assembly assembly) {
    return assembly.GetCustomAttributes(false).Any(x => (x as DebuggableAttribute) != null ? (x as DebuggableAttribute).IsJITTrackingEnabled : false);
}

As you can see … it’s pretty simple.

Note:

Tipically I add a pre-build task to my build server so that the AssemblyConfigurationAttribute is added to the CommonAssemblyInfo file with the appropriated value: either “Debug” or “Release”. This way anyone can easily see, using only the ILDASM, which king of compilation was used to generate my assemblies.