I always gets the error
unable to cast object of type 'system.byte ' to type 'system.iconvertible"
with my code of retrieving image form DB to a picturebox through a 'SelectedIndexChanged' event of a listview.
Here is my code:
foreach (ListViewItem LVI in lvwInventory.SelectedItems)
{
////CONNECTION STRING TO THE DATABASE (USED FOR SAVING/UPLOADING IMAGE)
//System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection("Provider=microsoft.jet.oledb.4.0; data source=..\\dbMyDVDOrganizer.mdb");
con.Open();
//OLEDB COMMAND FOR RETRIEVING IMAGE FROM THE DATABASE
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("SELECT DVDImage FROM tblDVDInventory WHERE ItemCode='" + lvwInventory.SelectedItems[0].Text + "'");
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
Byte bits = Convert.ToByte(ds.Tables[0].Rows[0][0]);
MemoryStream memoryBits = new MemoryStream(bits);
Bitmap bitmap = new Bitmap(memoryBits);
//BITMAP HAS THE IMAGE NOW.
pctImage.Image = bitmap;
}
Where did I make a mistake?
Just a wild guess: I'd image DVDImage
contains more than just a Byte
... maybe a byte array ( Byte[]
)? Replace
Byte bits = Convert.ToByte(ds.Tables[0].Rows[0][0]);
with
Byte[] bits = ds.Tables[0].Rows[0].Field<Byte[]>("DVDImage");
(or
Byte[] bits = (byte[])(ds.Tables[0].Rows[0][0]);
if you are using an old version of the .NET framework).
Try replacing
Byte bits = Convert.ToByte(ds.Tables[0].Rows[0][0]);
with
Byte[] bits = (Byte[])ds.Tables[0].Rows[0][0];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.