2

what is the problem of this code?? i got a error for the GDI+ and i dont know to solve.

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  emPic=@emPic where emID='" & lbl_empID.Text & "'"

   With cmdEmp
      .Parameters.AddWithValue("@emPic", arrPic)
      .ExecuteNonQuery()
   End With
End Sub
2
  • 1
    You need to learn how to use the debugger. Which line is causing the error? what is the error exactly ? Your question is prone to closing if you don't add more details soon. Commented Feb 24, 2013 at 14:17
  • the error is in thus line PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat) Commented Feb 24, 2013 at 14:19

2 Answers 2

1

Try replacing that line with this :

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

The reason could be that the Image is being used by the PictureBox. Its also good practice to have the following instead :

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  emPic=@emPic where emID=@emID"

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

End Using

This way the MemoryStream will get Disposed after being used. And adding @emID as a parameter is safer.

Sign up to request clarification or add additional context in comments.

Comments

0

RESOLVED!!! Bind picture box to the datasource field using the Advance tab in the DataBindings section. Use the ImageLocation property instead of image. Then change the Update mode to NEVER. but how abt the update ??? Use a textbox and hide behind the Picture box [with visible = true]. Bind textbox to same datasource. THAT IS!!!

image displays directly in picture box whilst the textbox TextChanged property handles whether there is UPDATE or Not. took me a while but it works fine now. good luck as u code... ~ SAM, GHANA

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.