in

code for eternity !!!

community website for .net freaks ;-)

Technology

December 2007 - Posts

  • Developing Facebook Applications using Facebook Developer Toolkit for .NET

    You can now develop windows based or web based Facebook Applications using your favorite .NET language, be it C# or VB.NET. All you need is a Facebook account, Visual Studio Express and the Facebook Developer Toolkit for .NET. Information about the Facebook Developer Toolkit can be found here. The tooIkit makes it incredibly easy to develop Facebook Applications based on the Facebook Platform. The toolkit also includes WPF and LINQ examples. I would strongly recommend you to check out the following links before going ahead and downloading the toolkit:

    1) Video: Facebook Developer Toolkit Walkthrough (Windows Forms)
    2) Video: Facebook Developer Toolkit Walkthrough (ASP.NET)
    3) Facebook Quickstarts

    Detailed information on the Facebook Platform can be found here.

    Cheers,
    Raj

    ~~~ CODING FOR ETERNITY !!! ~~~

  • Handling ASP.NET Session Variables Efficiently

    One of the most common mistakes ASP.NET developers make is while accessing session variables. Have a look at the code below:

    C#:

        // Writing to session variable
        Session["UserCountry"] = ddlUserCountry.SelectedValue;
           
        // Reading from session variable
        string userCountry = Session["UserCountry"].ToString();

    VB:

        ' Writing to session variable
        Session("UserCountry") = ddlUserCountry.SelectedValue

        ' Reading from session variable
        Dim userCountry As String = Session("UserCountry").ToString()

    I have seen such code across multiple aspx and ascx code behind pages and it is a disaster waiting to happen and has many drawbacks as follows:

    1) If a session variable is read before it has been assigned or if the current session times out, it will result in NullReferenceException as no null checking happens before reading from session variable.

    2) A simple spelling mistake of the key name used to identify the session variable ("UserCountry" in above example) will result in NullReferenceException (if no null checking is being done) or incorrect data (if null checking is being done).

    3) In large ASP.NET projects where many developers are coding at the same time, very often two developers end up using the same key names for two different session variables meant for two different purposes. I have seen developers use the wildest key names for session variables like "Name" which is so common, it can be used by one developer to save the user's first name, and by another developer to save the user's user name. When such a situation arises, one of the session variable's key name would have to be changed across multiple aspx and ascx code behind pages, making sure that the other session variable sharing the same key name but meant for a different purpose is not changed by accident. This will result in complete chaos, and debugging and testing for such changes becomes a nightmare.

    4) There is no easy way to track the usage of session variables, their key names, their data types, the approximate memory being used per session, etc.

    However, with just a few extra lines of code and effort, we can get rid of above problems. To address above problems, we can create a single static / shared class (lets call it SessionHandler) which exposes all session variables through strongly typed static / shared properties. No aspx and ascx code behind pages should ever access session variables directly but instead access the strogly typed static / shared properties of the SessionHandler class. All the code to access session variables should exist ONLY in the SessionHandler class. Also, the SessionHandler class should use string variables to save key names of different session variables it exposes in order to avoid spelling mistakes. Have a look at the code below:

    C#:

        // Static / shared class for handling session variables
        public static class SessionHandler
        {
        
            // Declare a string variable to hold the key name of the session variable
            // and use this string variable instead of typing the key name
            // in order to avoid spelling mistakes
            private static string _userCountryKey = "UserCountry";
           
            // Declare a static / shared strongly typed property to expose session variable
            public static string UserCountry
            {
           
                get
                {
               
                    // Check for null first
                    if (HttpContext.Current.Session[SessionHandler._userCountryKey] == null)
                    {
                        // Return an empty string if session variable is null
                        return string.Empty;
                    }
                    else
                    {
                        return HttpContext.Current.Session[SessionHandler._userCountryKey].ToString();
                    }

                }
               
                set
                {
                    HttpContext.Current.Session[SessionHandler._userCountryKey] = value;
                }

            }

        }

    VB:

        ' Static / shared class for handling session variables
        Public Class SessionHandler

            ' Declare a string variable to hold the key name of the session variable
            ' and use this string variable instead of typing the key name
            ' in order to avoid spelling mistakes
            Private Shared _userCountryKey As String = "UserCountry"

            ' Declare a static / shared strongly typed property to expose session variable
            Public Shared Property UserCountry() As String

                Get

                    ' Check for null first
                    If (HttpContext.Current.Session(SessionHandler._userCountryKey) Is Nothing) Then
                        ' Return an empty string if session variable is null
                        Return String.Empty
                    Else
                        Return HttpContext.Current.Session(SessionHandler._userCountryKey).ToString()
                    End If

                End Get

                Set(ByVal value As String)
                    HttpContext.Current.Session(SessionHandler._userCountryKey) = value
                End Set

            End Property

        End Class

    Once we have the SessionHandler class in place, all aspx and ascx code behind pages can access session variables through this class like below. Notice that we dont access session variables directly anymore and let the SessionHandler class do all the required work to access session variables.

    C#:

        // Writing to session variable
        SessionHandler.UserCountry = ddlCountry.SelectedValue;
           
        // Reading from session variable
        string userCountry = SessionHandler.UserCountry;

    VB:

        ' Writing to session variable
        SessionHandler.UserCountry = ddlUserCountry.SelectedValue

        ' Reading from session variable
        Dim userCountry As String = SessionHandler.UserCountry

    Now since all the code to access session variables exists in one single class, it becomes really easy to track their usage, their key names, their data types, the approximate memory being used per session, etc. Also notice how easy it would be to change the key name of a session variable if required. All you would have to do is change the value of one private static / shared variable ("_userCountryKey" in above example). Also, we can be 100% sure that we are not using duplicate key names for multiple session variables by simply checking the value of all existing private static / shared string variables being used to save key names by the SessionHandler class.

    Note: We can use the same above architecture (with slight changes to code) to access data from ASP.NET Cache as well as Application Settings. I will blog about it in a future post.

    Cheers,
    Raj

    ~~~ CODING FOR ETERNITY !!! ~~~

  • Difference between Silverlight Version 1.0 and Silverlight Version 1.1

    A lot of my friends who want to get started with Silverlight development keep asking me what's the difference between Silverlight 1.0 and Silverlight 1.1? Which version should they use? Which version supports what?

    The primary difference between the two versions is: you can only use JavaScript to create Silverlight applications in version 1.0, however you can use your favorite language, be it C#, VB, IronRuby or IronPython to create Silverlight applications in version 1.1.

    For detailed information on the features of Silverlight versions 1.0 and 1.1, click here.

    Cheers,
    Raj

    ~~~ CODING FOR ETERNITY !!! ~~~

  • Nokia SMS Accelerator For N73, N80 and E61

    I use a Nokia N73 mobile handset and after few weeks of usage, sending an sms started taking a lot of time (over a minute) and the handset used to hang until the sms was sent. Time to send an sms kept on increasing after every sms sent. Finally when it became unbearable (over 4 minutes to send an sms), I googled for this problem and found out it’s a bug which exists in Nokia N73, N80 and E61 handsets. Nokia has released a patch for this bug called Nokia SMS Accelerator. However, when I tried to download and install the patch directly from my handset, it did not work for me. Finally, I downloaded the patch on my PC first, connected my handset to my PC, and installed the patch from my PC to my handset using the Nokia PC Suite. The patch fixed this problem and now it only takes about 5 seconds to send an sms. You can download the patch for Nokia N73 from here, for N80 from here and for E61 from here.

    Cheers,
    Raj

    ~~~ CODING FOR ETERNITY !!! ~~~

    Posted Dec 14 2007, 02:41 PM by raj with 26 comment(s)
    Filed under: ,
  • Great SharePoint Server 2007 Training Material

    I downloaded and installed the Microsoft Office SharePoint Server 2007 Training (Standalone Edition) recently and I can easily say its one of the best training resources for beginners to know all that SharePoint offers. You can download the Standalone Edition from here and the Portal Edition (requires SharePoint Server) from here. More info about the training can be viewed here.

    Cheers,
    Raj

    ~~~ CODING FOR ETERNITY !!! ~~~

    Posted Dec 12 2007, 03:15 PM by raj with 1 comment(s)
    Filed under:
  • Viewing the Component Services Manager in Vista

    Recently, I had to configure MSDTC on a Vista machine. However, I could not locate the Component Services Manager in the Control Panel > Administrative Tools console in Vista. After a few minutes of googling, I found out that all I had to do was run c:\windows\system32\comexp.msc

    If you do not have Run on your Start Menu, you can click on Start > All Programs > Accessories > Command Prompt. This would launch the Command Prompt where you can type c:\windows\system32\comexp.msc and press Enter to launch the Component Services Manager :-)

    Cheers,
    Raj

    ~~~ CODING FOR ETERNITY !!! ~~~

    Posted Dec 12 2007, 06:08 AM by raj with 1 comment(s)
    Filed under:
Powered by Community Server (Non-Commercial Edition), by Telligent Systems