KoenV

  • Visual Studio 2010 tip: Cut empty lines

    How many times you wanted to move 2 lines by cut and pasting them, but the line you cut last is actually a blank line and your actual code is removed from the clipboard? Visual Studio 2010 has an option that keeps cutting blank lines from overwriting the clipboard. Go and uncheck this one: Tools » Options » Text Editor » All Languages » General » Apply Cut or Copy commands to blank lines when there is no selection

    image

    Extra (related) tip

    The (free) Visual Studio 2010 extension Visual Studio 2010 Pro Power Tools contains (apart from a bunch of other handy features) the commands Edit.MoveLineUp and Edit.MoveLineDown to do whatever they say they do and maps them automatically to keyboard shortcuts Alt+Up & Alt+Down.

    Resharper (not-free) has similar commands for moving lines, by default mapped to Ctrl+Alt+Shift+Up/Down.

    Read more...

  • Generically correcting data before save with Entity Framework

    Been working with Entity Framework (.NET 4.0) for a week now for a data migration job and needed some code that generically corrects string values in the database. You probably also have seen things like empty strings instead of NULL or non-trimmed texts ("United States       ") in "old" databases, and you don't want to apply a correcting function on every column you migrate.

    Here's how I've done this (extending the partial class of my ObjectContext):

    public partial class MyDatacontext
    {
        partial void OnContextCreated()
        {
            SavingChanges += OnSavingChanges;
        }
     
        private void OnSavingChanges(object sender, EventArgs e)
        {
            foreach (var entity in GetPersistingEntities(sender))
            {
                foreach (var propertyInfo in GetStringProperties(entity))
                {
                    var value = (string)propertyInfo.GetValue(entity, null);
     
                    if (value == null)
                    {
                        continue;
                    }
     
                    if (value.Trim().Length == 0 && IsNullable(propertyInfo))
                    {
                        propertyInfo.SetValue(entity, null, null);
                    }
                    else if (value != value.Trim())
                    {
                        propertyInfo.SetValue(entity, value.Trim(), null);
                    }
                }
            }
        }

        private IEnumerable<object> GetPersistingEntities(object sender)
        {
            return ((ObjectContext)sender).ObjectStateManager
                .GetObjectStateEntries(EntityState.Added | EntityState.Modified)

                .Select(e => e.Entity);
        }

       
    private IEnumerable<PropertyInfo> GetStringProperties(object entity)
        {
            return entity.GetType().GetProperties()
               
    .Where(pi => pi.PropertyType == typeof(string));
        }

       
    private bool IsNullable(PropertyInfo propertyInfo)
        {
            return ((EdmScalarPropertyAttribute)propertyInfo

                .GetCustomAttributes(typeof(EdmScalarPropertyAttribute), false)
                .Single()).IsNullable;
        }
    }

     

    Obviously you can use similar code for other generic corrections.

    Read more...

  • Error pages in ASP.NET

    In ASP.NET you can retrieve the last unhandled exception via:

    (HttpContext.Current.)Server.GetLastError() // Server object is available as a property in Page and UserControl context

    This obviously only works in the same roundtrip. If you want to retrieve this information in your error page, you got a problem because the error page is not returned in the same roundtrip. The server responds with a redirect response and a new request to the error page is automatically sent. A common workaround would be to store the exception in your Session state from the Application_Error event in Global.asax.

    From ASP.NET 3.5 you can configure the redirect mode for error pages:

    <customErrors mode="On" defaultRedirect="~/Error.aspx" redirectMode="ResponseRewrite" />

    This way the redirect response is not sent and the error page is returned right away. That implies that the browser is not aware of a page change and cannot reflect it in the address bar, so your original URL is not replaced with the URL of the error page, which might be what you actually want…

    Read more...

  • LINQPad

    Joseph Albahari created this great tool to execute LINQ (but also SQL) queries and review the results. In case of LINQ you also get to see the corresponding compiler translation and SQL query.

    Some pro's:

    • Great for testing LINQ without having to build and debug your entire application in Visual Studio...
    • No installation (just exe)
    • Self updating
    • You can choose between C# and VB
    • Includes query organizer
    • ...

     LINQPad

    Get it here: www.linqpad.net

    PS: Also take the LINQ quiz here: www.albahari.com/nutshell/linqquiz.html

    Read more...