[Mono-devel-list] Another C# ?: InvalidCastException

Eric Scott scottclansman at cwazy.co.uk
Sun Dec 12 13:59:50 EST 2004

Hey, once again I apologize for plopping newbie C# questions into this 
list; if you know of a list that might be better suited for C# 
newbie-ish stuff, let me know!

So I'm tinkering (again) with PostGreSQL access from C#.  I'm getting 
the data from the database and putting it into my variables.... and 
something goes haywire. Here's the code section:

string sql =
           "SELECT deptime, depqty, depsite, descrip, arvtime, arvqty, 
arvnote " +
           "FROM log";
       dbcmd.CommandText = sql;
       IDataReader reader = dbcmd.ExecuteReader();     


//(In a while loop here)
            deptime = (string) reader["deptime"];
            depqty = (int) reader["depqty"];
            depsite = (string) reader["depsite"];
            descrip = (string) reader["descrip"];
            arvtime = (string) reader["arvtime"];
            arvqty = (int) reader["arvqty"];
            arvnote = (string) reader["arvnote"];

Okay, compiles fine.  But when I run the exe I get:

> Unhandled Exception: System.InvalidCastException: Cannot cast from 
> source type to destination type.
> in <0x0029c> Test:Main (string[])

The "deptim" and "depqty" lines execute fine, as does the "arvqty" line, 
but all the other strings give this error.   All of the values being put 
into strings are varchar(32) in the databased, and all the variables 
have been declared as int or string, respectively.

More information about the Mono-devel-list mailing list