2016-05-16 49 views
0

不好意思再問一次這個問題.. 我只是簡化了這個問題.. 這是我的問題:無法投射'System.DBNull'類型的對象來爲MySQL圖像鍵入'System.Byte []'

的錯誤觀點是

Dim image As Byte() = DirectCast(command.ExecuteScalar(), Byte()) 

的代碼是

Dim stream As New MemoryStream() 
    Dim command As New MySqlCommand("select Imageblob from employeedetail where EmployeeID = '" + TextBoxEmployeeID.Text + "'", cn)   
Dim image As Byte() = DirectCast(command.ExecuteScalar(), Byte()) 
stream.Write(image, 0, image.Length) 

感謝您的幫助!

回答

0

DBNull是您的查詢將返回如果表中的字段爲空值。你不能將一個DBNull對象轉換爲另一種類型。你可以做到以下幾點,以避免錯誤

Dim image As Byte() 
If Not ISDBNull(DirectCast(command.ExecuteScalar(), Byte())) Then image = DirectCast(command.ExecuteScalar(), Byte()) 

這只是檢查,如果一個字段的類型爲DBNull其分配給image

+0

非常感謝您之前!這有助於我解決這個問題! :D再次感謝很多p3tch –

相關問題