Do you get errors in the mapscript c# tutorial?…

Filed on February 29th, 2008 at 9:48 pm under , , , , , ,  

…well, this is the solution to your problems (at least I hope so).

I am continuing receiving emails from people in despair that cannot successfully use this tutorial. I am sorry but I cannot answer to all this emails, so I thought to write this post in order to allow people to configure it correctly.

BTW, this demo is still working since 2 years without any problems, and the code there is just the same you can download from this blog. So it must work also for you ;-)

So if you just cannot use it, this is a check list:

If you receive the "Unable to load dll (mapscript)" error, look at this article from Tamas Szekeres (the mapscript c# mantainer).

If you cannot compile the solution, be sure to have installed MapServer and the needed dlls. You need to set the path to them, for example in my windows box in my PATH environment variable there is: c:\ms4w\Apache\cgi-bin\mapscript and c:\ms4w\Apache\cgi-bin\mapscript\csharp.
If you are still receiving compile errors consider to copy all the dlls in the bin solution's folder (as in the source code you can download here). You need to put in the PATH or copy in the bin folder the dlls of the MapServer version you need.

And don't forget to take a look to this important article still from Tamas.

Error in c# MapScript Tutorial

If you can compile the solution, but you receive a blank image as this one:

  • in the mapfile check that the path to layers is the one where YOU effectively have your shapefiles, PostGIS layer, etc…
  • still in the mapfile check if you have the needed fonts. If you are not sure about that, just try the mapfile without using the LABEL sections. After it is working concentrate on fonts
  • note that the mapscript errors are not intercepted by default from Visual Studio, so the code will continue to execute even if mapserver throws errors. A good way to understand the source of the error is making some error handling strategy - and you should always do, like for example in this method (the one that effectively generates the map):
/// <summary>
/// Refresh MapServer map and send the image stream to output
/// </summary>
private void RefreshMap()
{
	try
	{
		using(imageObj image = map.draw())
		{
			byte[] img = image.getBytes();
			using (MemoryStream ms = new MemoryStream(img))
			{
				System.Drawing.Image mapimage = System.Drawing.Image.FromStream(ms);	
				Bitmap bitmap = (Bitmap)mapimage;
				bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
			}				
		}
	}
	catch (Exception e)
	{
		System.Diagnostics.Debug.WriteLine(e.Message);
	}
}

Hope it will work for you now! ;-)

Comments

11 Responses to “Do you get errors in the mapscript c# tutorial?…”

  1. sim on March 1st, 2008 2:37 am

    "Do you get errors…" hmm..typo in the title ;)

  2. lixiaoshan on March 2nd, 2008 10:35 am

    thank you ,i will read it very carefully

  3. Paolo Corti on March 5th, 2008 1:02 pm

    oops… mapscritp=mapscript, now not typo :-)
    thanks sim!

  4. vnts7 on March 10th, 2008 11:24 am

    Hi Paolocorti! Could you show me the way to pan map and interact with ajax? thank very much!

  5. Yatendra Jaiswal on April 22nd, 2008 3:25 pm

    Dear Paolo,
    I have followed your tutorial and sucessfully built the web site using C#.
    Next i want to develop same tutorial on .net usinf Visual Basic language instead of C#.
    Before doing that i have some doubt in my mind.
    You have used "mapscript_csharp.dll".
    For visual basic language, mapscript_vb.dll required. or any dll supports visual basic language.
    Please Reply.
    Thanks Regards
    Yatendra Jaiswal
    yatsrock99@gmail.com

  6. Paolo Corti on April 28th, 2008 2:49 pm

    of course you may do the same with vb .net, cobol .net, etc etc (whatever .net language is fine). You will still need to use the mapscript_csharp.dll ;-)

  7. he yanjie on July 9th, 2008 11:06 am

    hi,Paolo,
    do mapserver has the function of panning?
    it's a important function of viewing a map!

  8. Vagner on July 18th, 2008 9:49 pm

    Hi! Very useful tut.. I begun with MapServer, GIS, etc, just 3 days ago and I've been in a challenging role for an inexperienced performer.

    That try/catch snippet above helped me a lot. Actually, I'm afraid the verdana.ttf is missing in the data.zip file, so the execution flow breaks at the map.draw() line, throwing nothing, exactly like you discribed.

    Thank you very much.
    Best regards from Brazil!

  9. TrongKhoi on July 21st, 2008 9:25 am

    Dear Mr.Paolo Corti
    I have folowed your tutorial but now, I have a problem .It was complie successfull but the map wasn't display.I have checked the path to the source (*.shp file)in the map file. Everything is OK , Could you help me, please

  10. keith on September 17th, 2008 7:48 pm

    Hi Paolo,

    I don't have problem to run the tutorial with shapefile. But got problem to work with
    PostgreSQL (version 8.3)/PostGIS. I did all as required in the tutorial for PostGIS. But when running
    imgeObj image = map.draw() in REfreshMap(), it failed with the errors

    "System.ApplicationException: msPOSTGISLayerRetrievePGVersion(): Query error. Error executing POSTGIS statement (msPOSTGISLayerRetrievePGVersion():select substring(version() from 12 for (position('on' in version()) - 13));msPOSTGISLayerRetrievePGVersion(): Query error. Error executing POSTGIS statement (msPOSTGISLayerRetrievePGVersion():select substring(version() from 12 for (position('on' in version()) - 13));msPOSTGISLayerRetrievePGVersion(): Query error. Error executing POSTGIS statement (msPOSTGISLayerRetrievePGVersion():select substring(version() from 12 for (position('on' in version()) - 13));msPOSTGISLayerRetrievePGVersion(): Query error. Error executing POSTGIS statement (msPOSTGISLayerRetrievePGVersion():select substring(version() from 12 for (position('on' in version()) - 13))\r\n at mapObj.draw()\r\n at TutorialMapServer.MapStream.RefreshMap() in C:\\app\\GIS\\MapServer\\CSharp Sample\\TutorialMapServer\\MapStream.aspx.cs:line 85"

  11. Paolo Corti on September 17th, 2008 9:43 pm

    Hi Keith,
    try to post that error to the mapserver group. Also look at the log file if you get something more helpful (ie: the SQL that postgres do NOT like)

Leave a Reply




Comments for this post will be closed on 23 February 2009.