Extract C# exception

Throwing or catching exception strategy on building an application is a very important to get detail exception message, inner exception and detail strack trace where or which part of line of code that fired the exception.

The following snipped code shows how to extract detail exception message, innner excepion and its detail strack trace to be recorded / written on log.

/// <summary>
    /// Extracting <seealso cref="System.Exception"> class include its inner exception
    /// </seealso></summary>
    public sealed class Extractor
    {
        /// <summary>
        /// Parse the specified ex.
        /// </summary>
        /// <param name="exception">Ex.
        public static string Parse(System.Exception exception)
        {
            string errMessage = Environment.NewLine;

            errMessage += FormatErrorDescription(exception);

            while (exception.InnerException != null)
            {
                errMessage += ExtractInnerException(exception.InnerException);
                exception = exception.InnerException;
            }

            errMessage += Environment.NewLine;

            return errMessage;
        }



        /// <summary>
        /// Extract exception message and its strack trace
        /// </summary>
        /// <param name="innerException">inner excepion object
        /// <returns>extracted exception info including message and strack trace</returns>
        static string ExtractInnerException(System.Exception innerException)
        {
            string errMessage = string.Empty;

            errMessage += Environment.NewLine + " InnerException ";
            errMessage += FormatErrorDescription(innerException);

            return errMessage;
        }

        /// <summary>
        /// Formating exception description format
        /// </summary>
        /// <param name="exception">
        /// <returns>Error description</returns>
        static string FormatErrorDescription(System.Exception exception)
        {
            if (exception == null)
                return string.Empty;

            return Environment.NewLine + exception.Message + Environment.NewLine + exception.StackTrace;
        }
    }

Get Excel column name by index and vice versa

I used to got a need to build small and simple plugin for Excel using VSTO. But I won’t write about VSTO in this post. The thing that I want to share is just a simple code to get excel column name by index and vise versa. I felt it was very important for me cause I worked intensively with index at that project.

public class ExcelUtility
    {
        /// <summary>
        /// Convert Excel column name into number/index. Column number start from 1. A equal to 1
        /// </summary>
        /// <param name="columnName">Column name
        /// <returns>Excel column in numeric</returns>
        public static int GetExcelColumnNumber(string columnName)
        {
            if (string.IsNullOrEmpty(columnName))
                throw new ArgumentNullException("Invalid column name parameter");

            columnName = columnName.ToUpperInvariant();

            int sum = 0;

            char ch;
            for (int i = 0; i < columnName.Length; i++)
            {
                ch = columnName[i];

                if (char.IsDigit(ch))
                    throw new ArgumentNullException("Invalid column name parameter on character " + ch);

                sum *= 26;
                sum += (ch - 'A' + 1);
                //sum += (columnName[i] - 'A');
            }

            return sum;
        }

        /// <summary>
        /// Convert Excel column index into name. Column start from 1
        /// </summary>
        /// <param name="columnNumber">Column name/number
        /// <returns>Column name</returns>
        public static string GetExcelColumnName(int columnNumber)
        {
            int dividend = columnNumber;
            string columnName = String.Empty;
            int modulo;

            while (dividend > 0)
            {
                modulo = (dividend - 1) % 26;
                columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
                dividend = (int)((dividend - modulo) / 26);
            }

            return columnName;
        }
    }
class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string columnName = "A";

                int columnIndex = ExcelUtility.GetExcelColumnNumber(columnName);

                Console.WriteLine("Index of {0} is {1} ", columnName, columnIndex);

                //increment columnIndex
                ++columnIndex;

                Console.WriteLine("Increment index = " + columnIndex);

                columnName = ExcelUtility.GetExcelColumnName(columnIndex);
                Console.WriteLine("Column name of index {0} is {1}", columnIndex, columnName);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.Read();

        }
    }

I write the code in my blog just to remind me in the future in case I get the same need. And it would be great if it can help others who have the same need as me :)

Reference

  1. http://stackoverflow.com/questions/181596/how-to-convert-a-column-number-eg-127-into-an-excel-column-eg-aa
  2. http://stackoverflow.com/questions/667802/what-is-the-algorithm-to-convert-an-excel-column-letter-into-its-number

ASP.Net MVC 4 in Xamarin Studio

Xamarin Studio is a great Mono IDE (Integrated Development Environment) in OS X area. At the time of writing this article, the version of Xamarin studio and mono framework I use are :

Xamarin Studio Version 4.2.1 (build 1) Runtime: Mono 3.2.5 ((no/964e8f0) GTK+ 2.24.20 theme: Raleigh GTK# (2.12.0.0) Package version: 302050000

Xamarin Studio includes with ASP.NET MVC 2 and 3 project template. While trying to create ASP.NET MVC 3 project, I got couples of errors such as authorization access etc. The good news is even though there is no project template for ASP.NET MVC 4, we still can convert our ASP.NET MVC 3 to run as ASP.NET MVC 4. The steps are :

  1. Create an ASP.NET MVC 3 project either with aspx or razor view engine
  2. Run the project via Xamarin studio menu (Run > Run With > Mono Soft Debugger for ASP.NET. While running the project, we may got an error message :
          System.UnauthorizedAccessException
          Access to the path "/Library/Frameworks/Mono.framework/Versions/3.2.5/etc/mono/registry" is denied.
    
          Description: HTTP 500.Error processing request.
          Details: Non-web exception. Exception origin (name of application or object): mscorlib.
          Exception stack trace:
    
          ......
    
        

    To solve the error, run the following command in terminal :
          sudo mkdir /Library/Frameworks/Mono.framework/Versions/3.2.5/etc/mono/registry
          sudo chmod g+rwx /Library/Frameworks/Mono.framework/Versions/3.2.5/etc/mono/registry
        
    </code>
  3. Re-run again the ASP.NET MVC project. We may get another error message.
          System.IO.FileNotFoundException
          Could not load file or assembly 'System.Web.WebPages, Version=1.0.0.0, Culture=neutral,   PublicKeyToken=31bf3856ad364e35'  or one of its dependencies. The system cannot find the file specified.
    
          Description: HTTP 500.Error processing request.
          Details: Non-web exception. Exception origin (name of application or object): mscorlib.
          Exception stack trace:
    
           ...
        
    To solve the error message, just use NuGet package manager, search for “ASP.NET MVC 4″ and add reference to it and its dependencies.
  4. Re-run the ASP.NET MVC again. Now we may get another error message as following :
        System.InvalidOperationException
        Conflicting versions of ASP.NET Web Pages detected: specified version is "1.0.0.0", but the version in bin is "2.0.0.0".    To continue, remove files from the application's bin directory or remove the version specification in web.config.
    
        Description: HTTP 500.Error processing request.
        Details: Non-web exception. Exception origin (name of application or object): System.Web.WebPages.Deployment.
        Exception stack trace:
    
        ...
    
        
    As the error message, we need to make our ASP.NET Web Pages version to 2.0.0.0 in web.config. So just open web.config and change webpages:Version to 2.0.0.0. as follow :
        <add key="webpages:Version" value="2.0.0.0">
        </add>
        
  5. Change assembly version declared in root web.config and /View/web.config of the following


    assembly from to

    System.Web.WebPages.Razor 1.0.0.0 2.0.0.0
    System.Web.Mvc 3.0.0.0 4.0.0.0

Now re-run the project again via Xamarin Studio and our project should run well as expected.

References

  1. http://curtis.schlak.com/2013/09/29/setup-asp-net-mvc-4-on-monodevelop.html

Xcode failed to download use the purchases page to try again

The following day after the release of iOS 7, I started update my iphone 4s to get to know how exactly the ios 7 is. My iphone successfully updated even though it was quite annoying with slow internet connection :( .

The next thing is updating my Xcode, since I got a notification to update my xcode from version 4.6.3 to version 5.0 via app store of version 1.2.2. My OSX version is 10.8.5. While updatig process, several times I got error message Xcode failed to download use the purchases page to try again, and my download prosess suddenly stopped. Googling on internet and asking forum got many different suggestion and solution. Thankfully one of them success that is by clearing appstore download cache.

The steps to get things to work are quite simple.

  1. Keep the AppStore App open. Open terminal and type ``` bash cd /private/var/folders/ ``` Once there, search for com.apple.appstore ``` bash find . | grep com.apple.appstore ``` You will find folder structure like this ./40/lhn22jn901zdw2bpf82hkggw0000gn/C/com.apple.appstore
  2. Once inside the folder, open it in finder ``` bash open . ```
  3. While keeping AppStore open, remove this folder ``` bash rm -rf * ```
  4. Now, go back to AppStore and click on Download again.
  5. If download/update disappear, close the appstore then reopen it.

That steps worked well to me. Good luck and have a nice day.

References

  1. http://apple.stackexchange.com/questions/61646/xcode-failed-to-download-use-the-purchases-page-to-try-again/71202#71202

Unsupported IClasspathEntry in Eclipse-STS

I often got annoying pup up error message. An internal error occurred during updating Maven project. The message was Unsupported IClasspathEntry kind=…

Googling for that problem finally got a trick how to resolve it.

  1. Right-click on your project, select Maven > Disable Maven Nature
  2. Open your terminal, go to your project folder and do
       mvn eclipse:clean
  3. Right click on your Project and select Configure > Convert into Maven Project

After converting into maven project, I can update my maven project and everything running well :)