Results 1 to 5 of 5

Thread: [RESOLVED] System.InvalidCastException: System.Data.SQLite.SQLiteDataReader cast to byte[]

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Resolved [RESOLVED] System.InvalidCastException: System.Data.SQLite.SQLiteDataReader cast to byte[]

    My Sqlite DB column ImageBlob is Image type, can be NULL. When the image is NULL, I get System.InvalidCastException. How to check whether it is NULL before doing the cast?
    if (reader!= DBNull.Value && reader!= null) is not enough.


    Code:
    public static object ExecutSqlCommand(string cmdText)
    if (sqliteConn!= null) 
    {
    	using (DbCommand dbCommand = sqliteConn.CreateCommand())
    	{
    		dbCommand.CommandText = cmdText;
    		return dbCommand.ExecuteReader();
    	}
            return null;
    }
    
    
    
    object reader= null;
    reader= ExecutSqlCommand("SELECT ImageBlob FROM MySettings WHERE F='cmd'");
    
    string bitmapUTF8String;
     if (reader!= DBNull.Value && reader!= null)
    {
        byte[] blobs = (byte[])reader; //<--- error System.InvalidCastException
        bitmapUTF8String = Encoding.UTF8.GetString(blobs);
    }

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: System.InvalidCastException: System.Data.SQLite.SQLiteDataReader cast to byte[]

    You need to check if the column is null, not the reader itself.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: System.InvalidCastException: System.Data.SQLite.SQLiteDataReader cast to byte[]

    vb.net Code:
    1. if (reader.Read() && !reader.IsDBNull(reader.GetOrdinal("ColumnName")))
    2. {
    3.     var data = (byte())reader["ColumnName"];
    4.  
    5.     // Use data here.
    6. }

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    597

    Re: System.InvalidCastException: System.Data.SQLite.SQLiteDataReader cast to byte[]

    Quote Originally Posted by jmcilhinney View Post
    vb.net Code:
    1. if (reader.Read() && !reader.IsDBNull(reader.GetOrdinal("ColumnName")))
    2. {
    3.     var data = (byte())reader["ColumnName"];
    4.  
    5.     // Use data here.
    6. }
    Thank you. (byte())reader["ColumnName"] solve my problem.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] System.InvalidCastException: System.Data.SQLite.SQLiteDataReader cast

    If you're only getting one value then you shouldn't be calling ExecuteReader in the first place. ExecuteScalar exists for that purpose. You can call ExecuteScalar and it will return the value as an Object reference or DBNull.Value if there is not data. E.g.
    vb.net Code:
    1. var data = myCommand.ExecuteScalar() as byte[];
    2.  
    3. if (data != null)
    4. {
    5.     // Use data here.
    6. }
    as performs a conditional cast, so data will be null if ExecuteScalar returns DBNull.Value.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width