Blog posts


  • Use default values in Javascript

    January 28, 2011 | Tags: Javascript, Nils, Snippet
    This cool way to use default values for javascript function parameters was provided by Nils:

    <script>

    function(param1) {
    param1 = param1 || "my default value";
    }

    </script>

    Update: Fixed typo in the script above (| should have been ||), thanks Nathan for pointing that out! Took the opportunity to add an alternative way to implement below:
    <script>

    function(param1) {
    param1 = typeof param1 !== 'undefined' ? param1 : "my default value";
    }

    </script>

  • Add Using by shortcut in Visual Studio 2008

    January 20, 2011 | Tags: C#
    There's a nice little keyboard shortcut to resolve namespaces and add the to the using directives.

    When trying to use a class that is outside of the using directives present the editor will display it as an error since it doesn't know of the namespace that the class we want to use.
    There's a little dropdown arrow in the lower right where you can add the using statement. But there's an even faster way.

    While standing on the class name, just press CTRL + . (that's holding down CTRL and then .) and you get up the options for which namespace to use. Often just enter is needed to add the preselected.

  • Major Microsoft code sample library

    January 18, 2011 | Tags: ASP.NET, Windows, Open source, C#, Snippet
    The Microsoft Community team provide an awesome collections of code samples over at CodePlex.

    Among these you can find:

    # ASP.NET Code Samples
    # Silverlight Code Samples
    # Windows Azure Code Samples
    # WPF Code Samples
    # Windows Forms Code Samples
    # Windows 7 Code Samples
    # Data Platform Code Samples
    # Office Development Code Samples
    # Windows Workflow Code Samples
    # Interop and Fusion Code Samples
    # Windows Base and .NET General Code Samples
    # Windows UI Code Samples
    # Security Code Samples
    # Windows Shell Code Samples
    # XML Code Samples
    # COM Code Samples
    # IPC and RPC Code Samples
    # Diagnostics Code Samples
    # Visual Studio Extensibility Code Samples
    # File System Code Samples
    # IIS Code Samples
    # Windows Service Code Samples


    You'll find the samples in the All-In-One Code Framework Sample Catalog.

    Or why not download the complete library?

  • Get version info from SQL backup

    January 17, 2011 | Tags: SQL, Snippet
    This neat little snippet shows you alot of information from a SQL database backup (.bak file)

    RESTORE HEADERONLY

    FROM DISK = N'c:\temp\dbMyDatabase.bak'

    WITH NOUNLOAD;

    If this doesn't work you're probebly trying to run it on an older SQL Server version than the backup was taken from.

    Unfortunately I haven't found a proper lookup table to translate the version number to SQL Server version, but here's two:
    611 = SQL Server 2005
    655 = SQL Server 2008

    If you find a good resource for version lookup, please post it in the comments, thanks!

  • Get Facebook profile picture using API and jQuery

    January 12, 2011 | Tags: Javascript, Facebook, jQuery, Snippet
    This shows how to fetch the users profile information directly to your web page using the Facebook API and jQuery (for updating the HTML).

    First step is to register your web site as a Facebook app. You will need a Facebook account in order to access the development features.



    If you're doing local development be sure to set the URL to your local path, otherwise you'll get an error saying: API Error Code: 191
    API Error Description: The specified URL is not owned by the application


    Once signed up your new Facebook app will get an App ID assigned. You need to copy that into the script below where it says xxxxxxxxxxxxxxxxx.

    Most of the code below should be self explaining, but this is what it does:
    1) Init a connection to Facebook with your app id
    2) Get login status and..
    3a) Show connect button if not connected
    3b) Show profile information if connected

    <div id="fb-root"></div>
    <script src="http://connect.facebook.net/en_US/all.js"></script>

    <h2>Profile</h2>
    <div id="account-info"></div>

    <script>
    function showAccountInfo() {
    FB.api({
    method: 'fql.query',
    query: 'SELECT name, pic FROM user WHERE uid=' + FB.getSession().uid
    },
    function(response) {
    $('#account-info').html('<img src="' + response[0].pic + '">' +
    response[0].name + ' <img onclick="FB.logout()" style="cursor: pointer;" ' +
    'src="https://s-static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">');
    }
    );
    }

    function showLoginButton() {
    $('#account-info').html('<img onclick="FB.login()" style="cursor: pointer;" ' +
    'src="https://s-static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif">');
    }

    // Called when Facebook login status change
    function onStatus(response) {
    if (response.session) {
    showAccountInfo();
    } else {
    showLoginButton();
    }
    }
    FB.init({
    appId: 'xxxxxxxxxxxxxxxxx',
    cookie: true,
    status: true,
    xfbml: true
    });

    FB.getLoginStatus(function(response) {
    onStatus(response); // once on page load
    FB.Event.subscribe('auth.statusChange', onStatus); // every status change
    });

    </script>

    You can access other profile information as well. You'll find all properties on the user object over at Facebook Query Language (FQL) Reference.

    If you're getting an unspecified error when trying to connect a user to your web page, the most likely issue is that you're app is in sandbox mode which blocks the app for other users than the developer. You can turn it off under settings over at your Facebook app page.