views:

1002

answers:

3

When an exception occurs to in an Asp.Net web page, an error message is displayed with the complete stack trace.

Example below:

Stack Trace:
IndexOutOfRangeException: Index was outside the bounds of the array.

MyNameSpace.SPAPP.ViewDetailsCodeBehind.LoadView() +5112 MyNameSpace.SPAPP.ViewDetailsCodeBehind.Page_Load(Object sender, EventArgs e) +67
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +13
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +43
System.Web.UI.Control.OnLoad(EventArgs e) +98
... ...

The problem is that the Line number displayed does not correspond to the line in my code that originated the exception.
In the example above, the stack shows line number 5111, yet my code behind .cs file only has 250 lines!

The aspx page is stored in a SharePoint site and the Assembly with the code behind has been deployed to GAC. Also, I've compiled in Debug mode.

Given the settings above, how can I find out what line in my code caused the Exception?



Clarification as pointed out by strelokstrelok:

In Release mode the number in front of the exception is NOT the line of code. Instead it's an offset to the native compiled code, which doesn't have any meaning to humans. More about this here: http://odetocode.com/Blogs/scott/archive/2005/01/24/963.aspx

In debug mode the PDB file will automatically map the native code offset to your .cs line in code and the number displayed WILL be the corresponding line in code.

+3  A: 

Your code behind file is not the complete class, it's only a portion that is used when the class as a whole is compiled by ASP.NET. To find what is truly on that line, take a look at the compiled class / assembly using a tool like Reflector.

matt b
I might be wrong here but AFAIK, in ASP.Net the stack always gives you the correct line, however, in Asp.Net + SharePoint this doesn't seem to be the case.Anyway, I use red-gate .Net reflector but I couldn't find anything that would help me identify the line of code that caused the exception.
Henrique Zacchi
This is incorrect. Those numbers are NOT line numbers and not even offsets into IL.
Strelok
A: 

Maybe the running code is not what you see on your screen. Some mate might have refactored it for you. :)

Szundi
wtf is the problem? it happened with me that my friend altered the code that i was debugging remotely on the server - funny results...
Szundi
+2  A: 

Those numbers are NOT line numbers. In Release mode the stack trace contains the offsets into the native compiled code instead of line numbers. You can read some more about it here: http://odetocode.com/Blogs/scott/archive/2005/01/24/963.aspx

The only way to get line numbers in a stack trace is if you built you code in debug mode with the PDB files available.

Strelok
Thanks for pointing me to the right direction. Now, my assembly was deployed to GAC therefore the PDB doesn't get copied over. I tried copying the pdb file to C:\Windows\assembly\GAC_MSIL\myassembly\1.0xxxxx but still doesn't display the correct line number. tx
Henrique Zacchi