Get source file, method and line in exception

I am trying to get the details from an exception that occurred in my asp.net C # web application (4.0). I have a .pdb file in a bin folder. The following code does not work as expected -

protected void Button1_Click(object sender, EventArgs e) { try { //throwing Exception using (SqlConnection connection=new SqlConnection("")) { connection.Open(); } } catch (Exception exception) { //Get a StackTrace object for the exception StackTrace st = new StackTrace(exception, true); //Get the first stack frame StackFrame frame = st.GetFrame(0); //returns {PermissionDemand at offset 5316022 in file:line:column <filename unknown>:0:0} //Get the file name string fileName = frame.GetFileName(); //returns null //Get the method name string methodName = frame.GetMethod().Name; //returns PermissionDemand //Get the line number from the stack frame int line = frame.GetFileLineNumber(); //returns 0 //Get the column number int col = frame.GetFileColumnNumber(); //returns 0 } } 

What is wrong here?

update: "StackFrame frame = st.GetFrame (st.FrameCount - 1)", solved this problem.

+7
c # exception
source share
1 answer
 //Remove this line StackFrame frame = st.GetFrame(0); //Add this line StackFrame frame = st.GetFrame(st.FrameCount - 1); 
+6
source share

All Articles