0

I have this working code that displays an image in the other form when datagridview is selected

Private Sub dgv1_DoubleClick(sender As Object, e As EventArgs) Handles dgv1.DoubleClick
            frmEdit.Show()
            Dim ms As New MemoryStream(changephoto(CInt(dgv1.SelectedCells(0).Value)))
            frmEdit.PictureBox1.Image = Image.FromStream(ms)
    End Sub


        Function changephoto(ByVal photo As Integer) As Byte()
            cn.Open()

            With cmd
                .Connection = cn
                .CommandText = "SELECT studImage FROM studentInformation WHERE Studentid = " & dgv1.SelectedRows(0).Cells(0).Value
            End With
            Dim myPhoto() As Byte = CType(cmd.ExecuteScalar(), Byte())
            cn.Close()
            Return myPhoto
        End Function

The problem is when i select a record that has no image,I get this error:

Error I want the user to select records even if it is no image in the database without having error. Can anyone help me how to fix this. thanks

5
  • try like this - hastebin.com/bajasiliwo.vbs
    – Vivek S.
    Commented Dec 28, 2015 at 7:46
  • @wingedpanther How can i apply that to my code that even it has no image, it will not produce no error? Commented Dec 28, 2015 at 7:56
  • What you want to do if there is no image?? a guess show a message that says "No Photo is defined" ?
    – Vivek S.
    Commented Dec 28, 2015 at 9:10
  • Check this - hastebin.com/uyuvojimir.vbs (untested)
    – Vivek S.
    Commented Dec 28, 2015 at 9:20
  • @wingedpanther I want the user to can still select records even if it is no image in the database without having error. Commented Dec 28, 2015 at 9:40

1 Answer 1

1

Decalre myPhoto() outside of the Try...Catch block. Additionally you should close the connection in error and non-error case (Finally or alternatively with the Using keyword).

Dim myPhoto() as Byte = Nothing
With cmd
    .Connection = cn
    .CommandText = "SELECT studImage FROM studentInformation WHERE Studentid = " & dgv1.SelectedRows(0).Cells(0).Value
End With

Try
  cn.Open()
  myPhoto = CType(cmd.ExecuteScalar(), Byte())
Catch ex as Exception

Finally
  If cn IsNot Nothing
     cn.Close()
  End If
End Try
return myPhoto

EDIT:

Private Sub dgv1_DoubleClick(sender As Object, e As EventArgs) Handles dgv1.DoubleClick
    frmEdit.Show()
    Dim ms As MemoryStream
    Dim photo as Byte() = changephoto(CInt(dgv1.SelectedCells(0).Value))
    If photo IsNot Nothing
       ms = new MemoryStream(photo)
       frmEdit.PictureBox1.Image = Image.FromStream(ms)
    End If
End Sub
5
  • thanks for commenting but using your code i get different error now. It says ArgumentNullException was unhandled. Buffer cannot be a null Commented Dec 28, 2015 at 8:15
  • Dim ms As New MemoryStream(changephoto(CInt(dgv1.SelectedCells(0).Value))) This line Commented Dec 28, 2015 at 8:20
  • This loc is not in your example thus I don´t know what your trying to do. My guess is that you have to check the return value of function changephoto for null values after you called it.
    – Alex B.
    Commented Dec 28, 2015 at 8:22
  • That code is in my example at the top inside the Private Sub dgv1_DoubleClick - line 2 Commented Dec 28, 2015 at 8:26
  • See my edit. You have to handle null values before setting your picture.
    – Alex B.
    Commented Dec 28, 2015 at 9:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.