in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

sqlite Exception System.InvalidCastException: Specified cast is not valid.

Last post 12-23-2009 10:12 PM by fishnet37222. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 06-17-2009 12:16 PM

    • afshin
    • Not Ranked
    • Joined on 06-17-2009
    • Posts 4

    sqlite Exception System.InvalidCastException: Specified cast is not valid.

     This is the Code in C#:

               SQLiteDataReader rdr;
               SQLiteCommand contentCommand;
               String cmd = String.Format("SELECT filename,content,filesize FROM documents WHERE did={0}",120);

               contentCommand = sqlconn.CreateCommand();
               contentCommand.CommandText = cmd;
               rdr = contentCommand.ExecuteReader();


               rdr.Read();
               MessageBox.Show(rdr.GetFieldType(1).ToString());
               MessageBox.Show(rdr.FieldCount.ToString());

    -->        long len = rdr.GetBytes(1, 0, null, 0, 0);
               Byte[ buf = new Byte[len];
               rdr.GetBytes(1, 0, buf, 0,(int) len);

    I get the Exception there!  sqlite Exception
    System.InvalidCastException: Specified cast is not valid.

    The GetFieldType Shows: System.Byte[

    What is it that I'm doing wrong?
  • 06-21-2009 6:24 AM In reply to

    Re: sqlite Exception System.InvalidCastException: Specified cast is not valid.

    Before calling GetBytes(), first call rdr.IsDBNull(1) to make sure you're not trying to read bytes from a null column.

  • 12-23-2009 10:12 PM In reply to

    Re: sqlite Exception System.InvalidCastException: Specified cast is not valid.

    I'm getting the same exception even with checking for a null column first.  I'm trying to read an image from the database.
Page 1 of 1 (3 items)
Powered by Community Server (Commercial Edition), by Telligent Systems