2012-02-19 63 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

相關問題