Getting error line nuber in ASP.Net - C#

Posted on 2009-02-19
Last Modified: 2013-12-17

How do I get the exact line number that has resulted in an exception?
Question by:vj_mi
    LVL 39

    Expert Comment

    You don't. You can use the .Stacktrace to find it, and the .Source to know the right file. There are tools that fill this gap, though.
    LVL 39

    Accepted Solution

    Sorry, my mistake, I answered too quickly. Here's a sample code in C# that does what you want:

        // Some code that can cause an exception.
        throw new Exception("An error has happened");
    catch (Exception ex)
        System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
        MessageBox.Show("Line: " + trace.GetFrame(0).GetFileLineNumber());
        MessageBox.Show("Column: " + trace.GetFrame(0).GetFileColumnNumber());

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now