0

there's an error showing up after i update the data in my datagridview and after i click the row of updated data in my datagridview this error will shown up

enter image description here

Private Sub DataGridView2_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellClick
    Dim i As Integer

    i = DataGridView2.CurrentRow.Index

    Me.txtEmployeeID.Text = DataGridView2.Item(0, i).Value
    Me.txtFirstName.Text = DataGridView2.Item(1, i).Value
    Me.txtMiddleName.Text = DataGridView2.Item(2, i).Value
    Me.txtLastName.Text = DataGridView2.Item(3, i).Value
    Me.txtGender.Text = DataGridView2.Item(4, i).Value
    Me.txtContactNumber.Text = DataGridView2.Item(5, i).Value
    Me.txtupAge.Text = DataGridView2.Item(6, i).Value
    Me.txtupAddress.Text = DataGridView2.Item(7, i).Value
    Me.Bdate.Text = DataGridView2.Item(8, i).Value
    Me.txtPos.Text = DataGridView2.Item(9, i).Value

    If DataGridView2.Item(10, i).Value Is Nothing Then Return
    Using m As MemoryStream = New MemoryStream(CType(DataGridView2.Item(10, i).Value, Byte()))
       PictureBox2.Image = CType(Image.FromStream(m).Clone(), Image)
    End Using
End Sub

**EDIT**

when im updating, the image type in my database will change into 0x which is no image are saved.

this is the code of my update

   cn.Close()
    cn.Open()
    With cmd
        .Connection = cn
        .CommandText = ("Update TBL_EMPLOYEE SET FirstName= '" & txtFirstName.Text & "', MiddleName='" & txtMiddleName.Text & "',LastName='" & txtLastName.Text & "', Gender='" & txtGender.Text & "',Age='" & txtupAge.Text & "' ,Address='" & txtupAddress.Text & "', Position='" & txtPos.Text & "',BirthDate='" & Bdate.Value.Date & "', [Picture]=@PID where [EmployeeID]=@EID ")
        .Parameters.AddWithValue("@EID", CInt(txtEmployeeID.Text))
        .Parameters.Add("PID", SqlDbType.Image).Value = ms.ToArray()
        .ExecuteNonQuery()
        .Dispose()
        .Parameters.Clear()
        txtFirstName.Text = ""
        txtMiddleName.Text = ""
        txtLastName.Text = ""
        txtGender.Text = ""
        txtContactNumber.Text = ""
        txtupAge.Text = ""
        txtupAddress.Text = ""
        txtPos.Text = ""
        Bdate.Text = ""
        PictureBox2.Image = Nothing


        MsgBox("Employee Updated", vbInformation, "Information Message")
        datagridshow()

    End With
End If

enter image description here

when i update this image the image will not save. there's wrong syntax in my update

enter image description here

2 Answers2

1

im missing this code : PictureBox2.Image.Save(ms, PictureBox2.Image.RawFormat) my bad. :'(

  • Good. Take care of that stream too. Don't leave it un-Disposed. It'll weight on you your app. Depending on how many `Select/Update` you'll perform. – Jimi Aug 07 '18 at 13:16
0

If you're sure DataGridView2.Item(10, i).Value has a value here then probably the MemoryStream is being Disposed too quickly. I'd suggest trying:

m As MemoryStream = New MemoryStream(CType(DataGridView2.Item(10, i).Value, Byte()))
PictureBox2.Image = Image.FromStream(m)

without Using

Vincent Vancalbergh
  • 3,267
  • 2
  • 22
  • 25
  • there's an error saying New cannot be used on a class that is declared 'MustInherit'. – Eugene Santos Aug 07 '18 at 10:32
  • Darn my translated C# to VB.Net :) Updated answer. – Vincent Vancalbergh Aug 07 '18 at 10:37
  • the error still same. ArgumentException was unhandled , Parameter is not valid. – Eugene Santos Aug 07 '18 at 10:45
  • Another idea: don't dispose the `MemoryStream` – Vincent Vancalbergh Aug 07 '18 at 11:07
  • The error happens before the memory stream is disposed, so it can't possibly be the problem. Besides, the OP is cloning the image so it doesn't matter if the stream is disposed. Keep the `Using` block. Something is not right with the image data. – Visual Vincent Aug 07 '18 at 11:14
  • when i update the image. the image type in my database will be going 0x which is no image are saved. that's what i am thinking wrong. – Eugene Santos Aug 07 '18 at 11:27
  • @VisualVincent Same can be said in this Question https://stackoverflow.com/questions/12680618/exception-parameter-is-not-valid-on-passing-new-image-to-picturebox the cause was the MemoryStream disposing too soon, but the error happened at a "weird" location. – Vincent Vancalbergh Aug 07 '18 at 11:58
  • 1
    I suggested the OP that method, [here](https://stackoverflow.com/questions/51704762/vb-net-how-to-call-image-in-datagridview-to-picturebox?answertab=active#tab-top). The problem is not the disposing of the stream. As @Visual Vincent already noted, the Image is cloned, thus no reference to the source stream (different from the post linked, where the image stream is directly assigned to a control's property - you never do that). There might be an error in the data (a Try/Catch would be good anyway) or the stream is off position. Try inserting a `m.Position = 0` before `Clone()`. – Jimi Aug 07 '18 at 12:12
  • 1
    It is not clear in that question where the actual exception happens, but if I interpreted _"I catch the exception only on line where Form1 was started"_ correctly, it would show on either `Form1.ShowDialog();` or `Application.Run(new Form1());` (most likely the latter), either of which is not weird at all, because it cannot show the internal line of code that caused the error, so it shows the current modal/message loop instead. Either way it doesn't just show a random location, and certainly not the one where the image is created. – Visual Vincent Aug 07 '18 at 12:15
  • This can be easily verified by the OP placing a breakpoint on `End Using` and see if it ever gets hit. -- Also, as I said before the image is cloned, thus the resulting image no longer references the `MemoryStream`, which means disposing it will not affect the image. – Visual Vincent Aug 07 '18 at 12:16
  • I can see now in the OP question, that an Image is not saved/loaded correctly, since a value of `0x0` is shown for the Image byte array. This will not, of course, generate a valid Image. This method just passes the Image to a `PictureBox`, is not responsible for the data *treatment*. The Update query for the Image is `.Parameters.Add("PID", SqlDbType.Image).Value = ms.ToArray()`. Both the syntax and the `ms` stream ought to be checked. – Jimi Aug 07 '18 at 12:35