Today was actually the first time (that I can remember) I have come across the following error message:

"Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack"

Unfortuntely, today also happened to be a very long day following a very long night (working until 5am last night).

So when this error appeared in a fresh page, based 90% on my previous code I was stumped, angry and frustrated.  Thankfully for me the error was simple to rectify, however it is important to note the error has a purpose rather than it being a bug.

This error crops up when terminating the Response to a page either through Response.End, Response.Redirect or Server.Trasnfer

You may not even notice the error 95% of the time that it occurs.

I had the lethal combination of a try-catch block and a Response.Redirect.  I think I'll explain the significance of the error first and then the simple work-around.

Basically, the Response object is a .NET wrapped set of code that has a certain life-path.  This path is supposed to end with a Response.End (either explicit or implied) that passes execution to the Application_EndRequest method.  When the correct path is not followed, a ThreadAbortException is thrown.

I moved my Response.Redirect out of a try-catch block and appended the optional "terminate execution" parameter of "false" as follows:

Response.Redirect("going-somewhere.aspx", false);

This alleviated my problem and I was able to go back to swearing about something else that wasn't working as expected.

Microsoft's explanation and fixes are listed here: ThreadAbortException when using Response.Redirect

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

If you've taken a look around my site you will notice I use a Captcha on the comments and contact pages.

I wrote this recently and once I have sorted out a few other things will post a link on this blog.

It works very simply and I'll include all the necessary code when I post the link.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

The code for calculating the distance between points that I have posted in C# was based upon the work published at: http://www.movable-type.co.uk/scripts/latlong.html

Anyone interested in this should also look for a downloadable list of UK postal OUT codes which is freely available (If I find the link then I will include it here).

All these things can be put together with something like Google Maps API to display routes, POIs and locations.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

The following is an extract of C# code I have written that takes the Longitude and Latitude of two points to find the distance between them.

My example uses the TLocation object (a custom object). The distance can be returned in km, m, miles.

I've also included functions to return the distance between two points given in m relative to a false origin (this is the way to Ordinance Survey co-ordinates work).

Enjoy.

#region CoOrdinates and Distance
public Double DistanceInKMLL(TLocation Loc1, TLocation Loc2)
{
Double Lat1 = Convert.ToDouble(Loc1.Latitude); Double Lon1 = Convert.ToDouble(Loc1.Longitude);
Double Lat2 = Convert.ToDouble(Loc2.Latitude); Double Lon2 = Convert.ToDouble(Loc2.Longitude);

Double R = 6371; //km

Double dLat = ToRad(Lat2 - Lat1);
Double dLon = ToRad(Lon2 - Lon1);
Double rLat1 = ToRad(Lat1);
Double rLat2 = ToRad(Lat2);

Double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
Math.Cos(rLat1) * Math.Cos(rLat2) *
Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
Double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
Double d = R * c;

return d;
}
public Double DistanceInMilesLL(TLocation Loc1, TLocation Loc2)
{
Double _km = DistanceInKMLL(Loc1, Loc2);

return (_km * 0.621371192);
}
public Double DistanceInMetresLL(TLocation Loc1, TLocation Loc2)
{
Double _km = DistanceInKMLL(Loc1, Loc2);

return (_km * 1000);
}
public Double DistanceInMetresXY(TLocation Loc1, TLocation Loc2)
{
Double x1 = Loc1.M_From_OriginX; Double y1 = Loc1.M_From_OriginY;
Double x2 = Loc2.M_From_OriginX; Double y2 = Loc2.M_From_OriginY;

Double dx = x1 - x2;
Double dy = y1 - y2;

Double z = Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));

return z;
}
public Double DistanceInMilesXY(TLocation Loc1, TLocation Loc2)
{
Double _m = DistanceInMetresXY(Loc1, Loc2);

return ((_m / 1000)* 0.621371192);
}
public Double DistanceInKMXY(TLocation Loc1, TLocation Loc2)
{
Double _m = DistanceInMetresXY(Loc1, Loc2);

return (_m / 1000);
}
public Double ToBearing(Double _Radians)
{
return ((ToDegrees(_Radians) + 360) % 360);
}
public Double ToRad(Double _Degrees)
{
return (_Degrees * (Math.PI / 180));
}
public Double ToDegrees(Double _Radians)
{
return (_Radians * (180 / Math.PI));
}
#endregion

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Whilest using XML serialisation to keep a set of Tasks in my Customer/Project Management App I came across a series of strange occurences which I tied down the the error: "There is an error in XML document."

Basically, the XML parser has come across a character that it doesn't expect to see given the specified encoding. In my case I was using UTF8 encoding.

Now I was puzzled that although I was using UTF8 encoding, the GBP sign (£) was causing the error. It turns out that I was encoding using UTF8 but the deserialisation was using "Default" encoding - OOPS.

Anyway, once I'd discovered that and changed so that UTF8 was the encoding for serialisation AND deserialisation we were good to go.

So if you get this error, check your encoding.

My serialisation code is:

public static bool SaveWorkToXML(TWork[] workArray, TTask thisTask)
{
string output = "";
try
{
XmlSerializer ser = new XmlSerializer(typeof(TWork[]));
Stream s = new MemoryStream();
XmlTextWriter xmlWriter = new XmlTextWriter(s, Encoding.UTF8);
ser.Serialize(xmlWriter, workArray);
TextReader rdr = new StreamReader(s);
s.Position = 0;
output = rdr.ReadToEnd().Replace("\"",'"'.ToString());
xmlWriter.Close();
s.Close();
return SaveWork(thisTask, output); // SaveWork supplies SqlParameters to a stored procedure and returns T/F if inserted/updated OK
}
catch { }
return false;
}

and the Deserialisation code:
public static TWork[] LoadWork(TTask thisTask)
{
TWork[] workArray = null;
try
{
string sql = "SELECT * FROM tblWork WHERE [work_guid] = '" +
thisTask.Task_GUID + "'";
DataTable dt = GetDataTable(sql);
string xmlText = "";
if (dt != null && dt.Rows != null)
{
foreach (DataRow dr in dt.Rows)
{
xmlText = dr["work_details"].ToString();
break;
}
}
dt.Dispose();
XmlSerializer ser = new XmlSerializer(typeof(TWork[]));
Stream s = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(xmlText));
workArray = (TWork[])ser.Deserialize(s);
}
catch
{ }
return workArray;
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

AaronReynolds.co.uk

Aaron Reynolds - for C# and VB .NET, HTML, PHP, CSS, ASP, DNS and BIND, Windows Server 2003 etc...