Friday, March 29, 2013

Show line number in exception handling using C#

Here i will show how to display Exception Line Number in C#.Net as shown below......
The Exception in this Out is due to SQL Connection....
  1. First create a class called ExceptionHelper.cs 
  2. Write a  public static method as shown below..........
Out Put :





C# Code :





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace BAL
{
    public static class ExceptionHelper
    {
        public static int LineNumber(this Exception e)
        {
            int linenum = 0;
            try
            {
                linenum = Convert.ToInt32(e.StackTrace.Substring(e.StackTrace.LastIndexOf(":line") + 5));
                HttpContext.Current.Session["ErrorLineNo"] = linenum.ToString();
            }
            catch
            {
                //Stack trace is not available!
            }
            return linenum;
        }
    }
}

No comments:

Post a Comment