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

Posted by on February 29, 2008

…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! ;-)

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • LinkedIn
  • Reddit
  • StumbleUpon
14 Comments on Do you get errors in the mapscript c# tutorial?…

Closed

  1. sim says:

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

  2. lixiaoshan says:

    thank you ,i will read it very carefully

  3. Paolo Corti says:

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

  4. vnts7 says:

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

  5. Yatendra Jaiswal says:

    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 says:

    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 says:

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

  8. Vagner says:

    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 says:

    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 says:

    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 says:

    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)

  12. palpandian says:

    hi,

    just 1 month i am working in Mapserver using Gis controls and Techniques.

    could you pl tell me pan(Hand
    Tool) coding in Mapserver

  13. palpandian says:

    hi,

    i saw your Demo in MapServer using Gis, Its very very useful to me,

    i am following to that coding,

    but zoomin,zoomout, fullextend coding ok for me

    But i dont know Pan(HandTool)Coding,
    pls send me pan coding in Mapserver

  14. Thang says:

    how to delete one point on layer POI??? Help me.