PostGis and NTS Problem

Topics: Data Access, SharpMap Project, SharpMap v0.9 / v1.x, SharpMap v2.0
Apr 6, 2008 at 8:49 AM
Edited Apr 6, 2008 at 8:53 AM
Hi
i download the latest version of sharp map and compile it
and i download the nts_demo from extesion section

i change add layer section for get map from postgis
but show error in postgis.cs
in function GetFeatureCount()

public int GetFeatureCount()
{
int count = 0;
using (Npgsql.NpgsqlConnection conn = new Npgsql.NpgsqlConnection(_ConnectionString))
{

string strSQL = "SELECT COUNT(*) FROM " + this.Table;
if (!String.IsNullOrEmpty(_defintionQuery))
strSQL += " WHERE " + this.DefinitionQuery;
using (Npgsql.NpgsqlCommand command = new Npgsql.NpgsqlCommand(strSQL, conn))
{
conn.Open(); ///////////////////////**********************************//////////////////////Specified cast is not valid.
count = (int)command.ExecuteScalar();
conn.Close();
}
}
return count;
}

i hav'nt any idea
i 'm newbe
please help me
Developer
Apr 8, 2008 at 6:42 PM
Edited Apr 8, 2008 at 8:07 PM
Hey, this was an actual bug, thanks for reporting it.

ExecuteScalar internally creates an Int64, but returns it as Object. You can cast the Object back to Int64 and an Int64 to an int, but by doing the (int) cast directly onto the Object things go wrong.

It could be solved like this:
count = (int)((Int64)command.ExecuteScalar());
I did it like this:
count = Convert.ToInt32(command.ExecuteScalar());

Change Set 32763 should solve your problem.