Results 1 to 5 of 5

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

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2016
    Posts
    346

    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
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,477

    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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,636

    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
    Hyperactive Member
    Join Date
    Aug 2016
    Posts
    346

    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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,636

    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