2013-02-24 56 views
2

這段代碼有什麼問題?我得到了一個錯誤的GDI +,我不知道解決。如何在vb.net中使用數據庫更新鏡像

Private Sub saveEmployee() 
    Dim ms As New MemoryStream 

    PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)--i got error in this line..A generic error occurred in GDI+. 

    Dim arrPic() As Byte = ms.GetBuffer() 

    Dim cmdEmp As New MySqlCommand 
    Dim sqlEmp As String 
    sqlEmp = "update tbl_employee set [email protected] where emID='" & lbl_empID.Text & "'" 

    With cmdEmp 
     .Parameters.AddWithValue("@emPic", arrPic) 
     .ExecuteNonQuery() 
    End With 
End Sub 
+1

你需要學習如何使用調試器。哪條線路導致錯誤?究竟是什麼錯誤?如果不盡快添加更多詳細信息,則您的問題很容易關閉。 – AbZy 2013-02-24 14:17:50

+0

錯誤是在線 PictureBox1.Image.Save(ms,PictureBox1.Image.RawFormat) – 2013-02-24 14:19:59

回答

1

嘗試使用此替換該行:

Dim bm as Bitmap = new Bitmap(PictureBox1.Image) 
bm.Save(ms, PictureBox1.Image.RawFormat) 

原因可能是該圖像是正在使用的PictureBox。它也是很好的做法有以下代替:

Using ms As MemoryStream = New MemoryStream() 

    Dim bm as Bitmap = new Bitmap(PictureBox1.Image) 
    bm.Save(ms, PictureBox1.Image.RawFormat) 

    Dim arrPic() As Byte = ms.GetBuffer() 

    Dim cmdEmp As New MySqlCommand 
    Dim sqlEmp As String 
    sqlEmp = "update tbl_employee set [email protected] where [email protected]" 

    With cmdEmp 
    .Parameters.AddWithValue("@emPic", arrPic) 
    .Parameters.AddWithValue("@emID", int.Parse(lbl_empID.Text)) 
    .ExecuteNonQuery() 
    End With 

End Using 

這種方式使用後MemoryStream將獲得Disposed。並且加入@emID作爲參數更安全。

+0

tnx先生好吧確定:)) – 2013-02-24 14:40:41

+0

@RodmarPusung不客氣,看到我的更新。 – AbZy 2013-02-24 14:43:46

+1

其工作先生:))tnx很多 – 2013-02-24 14:45:15

0

解決! 使用DataBindings部分中的Advance選項卡將圖片框綁定到數據源字段。使用ImageLocation屬性而不是圖像。然後將更新模式更改爲從不。但如何更新?使用文本框並隱藏在圖片框後面[with visible = true]。將文本框綁定到相同的數據源。那是!!!

圖片直接顯示在圖片框中,而TextBox TextChanged屬性處理是否存在UPDATE或NOT。我花了一段時間,但它現在工作正常。祝你好運,因爲你的代碼...〜SAM,GHANA