2012-02-19 59 views
2

我总是得到错误无法转换的类型“system.byte”对象键入“system.iconvertible”

无法投型“system.byte”的对象键入“system.iconvertible” 。

与我通过列表视图的“的SelectedIndexChanged”事件检索图像格式DB来一个图片的代码

这里是我的代码:

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; 
} 

我在哪里犯了一个错误?

+1

是例外抛出在哪一行更换

Byte bits = Convert.ToByte(ds.Tables[0].Rows[0][0]); 

? – thecoop 2012-02-19 11:01:26

+0

Byte bits = Convert.ToByte(ds.Tables [0] .Rows [0] [0]); – 2012-02-19 11:05:04

+2

我很确定你的位图没有存储在单个字节中... – Douglas 2012-02-19 11:05:54

回答

4

只是一个疯狂的猜测:我想像DVDImage不仅包含一个Byte ......也许是一个字节数组(Byte[])?更换

Byte bits = Convert.ToByte(ds.Tables[0].Rows[0][0]); 

Byte[] bits = ds.Tables[0].Rows[0].Field<Byte[]>("DVDImage"); 

(或

Byte[] bits = (byte[])(ds.Tables[0].Rows[0][0]); 

,如果你使用的是旧版本的.NET框架)。

+0

参数无效。它说.. :( – 2012-02-19 11:09:26

+0

这正是我所说的。 – Purplegoldfish 2012-02-19 11:10:48

+0

@JamesAndrewCruz确保你传入的是convert.tobyte方法接受的类型,列表如下:http://msdn.microsoft。 com/en-us/library/system.convert.tobyte.aspx – Purplegoldfish 2012-02-19 11:12:31

0

尝试

Byte[] bits = (Byte[])ds.Tables[0].Rows[0][0]; 
+0

'参数无效'它说... :( – 2012-02-19 11:10:40

+0

在哪条线上说的? – Douglas 2012-02-19 11:14:45

+0

Byte [] bits =(Byte []) ds.Tables [0] .Rows [0] [0]; on this sir .. – 2012-02-19 11:17:20

相关问题