Bug 4570 - Invalid cast exception on datareader.getbytes from sqlite's blob field
Summary: Invalid cast exception on datareader.getbytes from sqlite's blob field
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: Mono.Data.Sqlite ()
Version: 2.10.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-04-20 14:28 UTC by Natalia Portillo
Modified: 2015-01-28 13:24 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 4570 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
NEW

Description Natalia Portillo 2012-04-20 14:28:49 UTC
When trying to use SqliteDataReader.GetBytes() on a BLOB field inside a SQLite 3 database, an invalid cast exception is thrown.

Example code:
With schema "CREATE TABLE my_table (my_id INTEGER PRIMARY KEY, my_blob BLOB);"

string sql = "SELECT * FROM my_table";
SqliteCommand dbcmd = _dbcon.CreateCommand ();
dbcmd.CommandText = sql;
SqliteDataReader reader = dbcmd.ExecuteReader ();
while(reader.Read ())
{
    long blob_size = reader.GetBytes (reader.GetOrdinal("my_blob"), 0, null, 0, 0);
}

throws:
Exception Cannot cast from source type to destination type. happened.
Exception source: Mono.Data.Sqlite.
 Exception stack trace:   at Mono.Data.Sqlite.SqliteDataReader.VerifyType (Int32 i, DbType typ) [0x0011d] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLiteDataReader.cs:278 
  at Mono.Data.Sqlite.SqliteDataReader.GetBytes (Int32 i, Int64 fieldOffset, System.Byte[] buffer, Int32 bufferoffset, Int32 length) [0x00031] in /private/tmp/monobuild/build/BUILD/mono-2.10.9/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLiteDataReader.cs:326 

Expected results would be blob_size to contain size of BLOB field in bytes.
Comment 1 Tyson 2014-07-08 16:45:00 UTC
What is the status of this bug?  It's still marked "NEW", but its over two years old.  And I just got the same error..


System.InvalidCastException: Cannot cast from source type to destination type.
  at at Mono.Data.Sqlite.SqliteDataReader.VerifyType (int,System.Data.DbType) <IL 0x00120, 0x009ac>
  at at Mono.Data.Sqlite.SqliteDataReader.GetBytes (int,long,byte[],int,int) <IL 0x00034, 0x001df>

private byte[] GetBytes(SqliteDataReader reader){
   const int CHUNK_SIZE = 2 * 1024;
   byte[] buffer = new byte[CHUNK_SIZE];
   long bytesRead;
   long fieldOffset = 0;
   using (MemoryStream stream = new MemoryStream()){
      //Error occurs on the next line..
      while ((bytesRead = reader.GetBytes(0, fieldOffset, buffer, 0, buffer.Length)) > 0){
          stream.Write(buffer, 0, (int)bytesRead);
          fieldOffset += bytesRead;
      }
      return stream.ToArray();
   }
}
Comment 2 Tyson 2015-01-28 13:24:08 UTC
I should add that I'm still getting this bug six months later, no one is assigned it, and I'm using Windows 7 (since i noticed this bug was marked for OSX)....