[Mono-list] Npgsql error

Emil Emilov emil at emilov.de
Thu Aug 4 14:45:50 EDT 2005


Check the lines:
string operatingsystem = (string) reader["operatingsystem"];
string nodeid = (string) reader["nodeid"];

What happens of the results are NULL?
You should check for NULL first.

Then, are you sure that operatingsystem and nodeid are varchars or some other type that translate into string?
If not then you would get that exception.

Cheers,
Emil

Fabian Salamanca Dominguez wrote:
> 
> Hi
> 
> I tried to compile a simple C# program and access a Postgresql DB but I 
> got this error in runtime (it compiled with no errors) :
> 
> [fsalaman at ipdserver Mono]$ mcs dbaccess.cs  -r:Npgsql.dll -r:System.Data.dll
> [fsalaman at ipdserver Mono]$ mono dbaccess.exe
> 
> Unhandled Exception: System.InvalidCastException: Cannot cast from 
> source type to destination type.
> in <0x000e1> dbAccess:Main (System.String[] args)
> 
> ****************
> 
> This is the code:
> 
> using System;
> using System.Data;
> using Npgsql;
> public class dbAccess
>  {
>     public static void Main(string[] args)
>     {
>        string connectionString =
>           "Server=localhost;" +
>           "Database=opennms;" +
>           "User ID=opennms;" +
>           "Password=opennms;";
>        IDbConnection dbcon;
>        dbcon = new NpgsqlConnection(connectionString);
>        dbcon.Open();
>        IDbCommand dbcmd = dbcon.CreateCommand();
>        string sql =
>            "SELECT operatingsystem, nodeid " +
>            "FROM assets";
>        dbcmd.CommandText = sql;
>        IDataReader reader = dbcmd.ExecuteReader();
>        while(reader.Read()) {
>             string operatingsystem = (string) reader["operatingsystem"];
>             string nodeid = (string) reader["nodeid"];
>             Console.WriteLine("OS: " +
>                  operatingsystem + " " + nodeid);
>        }
>        // clean up
>        reader.Close();
>        reader = null;
>        dbcmd.Dispose();
>        dbcmd = null;
>        dbcon.Close();
>        dbcon = null;
>     }
>  }
> 
> What am I doing wrong?
> 
> Thanks!!!
> -- 
> Fabian
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list

-- 
Emil R. Emilov
-----------------------------------------------------------------------
mailto:emil at emilov.de
http://www.emilov.de


More information about the Mono-list mailing list