I'm trying to get values from DataGridView to the another form in textboxes and in Picturebox ,data in textboxes are there ok, but when I try to get a picture in a picturebox from DB it gives me this error.

Buffer cannot be null
Parameter name :buffer

and here is the code

 Private Sub dgv1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV1.CellDoubleClick
        Dim frm As New Edit_Employee()
        frm.MdiParent = Mainfrm
        frm.Show()

        frm.TextBox1.Text = DGV1.Rows(e.RowIndex).Cells(1).Value.ToString
        frm.TextBox2.Text = DGV1.CurrentRow.Cells(2).Value.ToString
        frm.TextBox3.Text = DGV1.CurrentRow.Cells(3).Value.ToString
        frm.TextBox4.Text = DGV1.CurrentRow.Cells(4).Value.ToString
        frm.TextBox5.Text = DGV1.CurrentRow.Cells(5).Value.ToString
        frm.TextBox6.Text = DGV1.CurrentRow.Cells(6).Value.ToString
        frm.TextBox7.Text = DGV1.CurrentRow.Cells(7).Value.ToString
        frm.TextBox8.Text = DGV1.CurrentRow.Cells(8).Value.ToString
        frm.DateTimePicker1.Text = DGV1.CurrentRow.Cells(9).Value.ToString
        frm.TextBox9.Text = DGV1.CurrentRow.Cells(10).Value.ToString
        frm.TextBox10.Text = DGV1.CurrentRow.Cells(11).Value.ToString
        frm.TextBox11.Text = DGV1.CurrentRow.Cells(12).Value.ToString
        frm.TextBox12.Text = DGV1.CurrentRow.Cells(13).Value.ToString
        frm.TextBox13.Text = DGV1.CurrentRow.Cells(14).Value.ToString
        frm.TextBox14.Text = DGV1.CurrentRow.Cells(15).Value.ToString
        frm.TextBox15.Text = DGV1.CurrentRow.Cells(16).Value.ToString
        frm.DateTimePicker2.Text = DGV1.CurrentRow.Cells(17).Value.ToString
        frm.DateTimePicker3.Text = DGV1.CurrentRow.Cells(18).Value.ToString
        frm.TextBox16.Text = DGV1.CurrentRow.Cells(19).Value.ToString
        frm.TextBox17.Text = DGV1.CurrentRow.Cells(20).Value.ToString
        frm.TextBox18.Text = DGV1.CurrentRow.Cells(21).Value.ToString
        frm.TextBox19.Text = DGV1.CurrentRow.Cells(22).Value.ToString
        frm.Txtbonus.Text = DGV1.CurrentRow.Cells(23).Value.ToString
        frm.TextBox21.Text = DGV1.CurrentRow.Cells(24).Value.ToString


        Try
            Dim connection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Danial\documents\visual studio 2010\Projects\ESI_PF_Payroll_V1\ESI_PF_Payroll_V1\Pay.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
            Dim command As New SqlCommand("SELECT Imagedata FROM Employee WHERE Firstname = '" & TextBox1.Text & "'", connection)

            connection.Open()

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

            connection.Close()

            Dim picture As Image = Nothing

            'Create a stream in memory containing the bytes that comprise the image.
            Using stream As New IO.MemoryStream(pictureData)
                'Read the stream and create an Image object from the data.
                picture = Image.FromStream(stream)

                frm.PB1.Image = (picture)
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


        frm.Txtfilepath1.Text = DGV1.CurrentRow.Cells(27).Value.ToString
        frm.Txtfilename.Text = DGV1.CurrentRow.Cells(26).Value.ToString
        frm.TxtGross.Text = DGV1.CurrentRow.Cells(28).Value.ToString
        frm.RTB1.Text = DGV1.CurrentRow.Cells(29).Value.ToString
    End Sub

Try this to see if the byte value is generating an image correctly:

Using msStream As New IO.MemoryStream(pictureData)
    picture = Image.FromStream(msStream)
    If IsNothing(picture) = False Then
        frm.PB1.Image = picture
    Else
        MsgBox("Picture returned nothing")
    End If
End Using

The error message indicates that "pictureData" is null (Nothing).

Make sure your query is correct and check for null before converting to an Image.

If pictureData IsNot Nothing Then

Another issue that you may run into using the ExecuteScalar command is the size limit that it can return. From the documentation on the return value:

The first column of the first row in the result set, or a null reference (Nothing in Visual Basic) if the result set is empty. Returns a maximum of 2033 characters

You may want to consider using a datareader instead and use reader.GetValue(0).

With this code added provided by "Begginnerdev"

 Try
            Dim connection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Danial\documents\visual studio 2010\Projects\ESI_PF_Payroll_V1\ESI_PF_Payroll_V1\Pay.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
            Dim command As New SqlCommand("SELECT Imagedata FROM Employee WHERE Firstname = '" & TextBox1.Text & "'", connection)

            connection.Open()

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

            connection.Close()

            Dim picture As Image = Nothing

            'Create a stream in memory containing the bytes that comprise the image.
            Using msStream As New IO.MemoryStream(pictureData)
                picture = Image.FromStream(msStream)
                If IsNothing(picture) = False Then
                    frm.PB1.Image = picture
                Else
                    MsgBox("Picture returned nothing")
                End If
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)



        End Try

It is the same error as I posted above .

What did you not understand about the information that I provided you? You still have the issues no matter which method you use to convert the byte array to an image. If the the byte array is null, it will fail.

commented: Some people will never learn. +8

My where clause has a problem.
I have changed it to this

where Firstname = '" & _
                  DGV1.CurrentRow.Cells(1).Value() & "'"

and after that it works fine,
Thank you all who halped me in this thread,'TnTinMN'I have understood what you were saying ,but I'm not a great coder (for now)so problem was how to execute it .
so I give it a good thought and the solution come to my mind was this.

Thank you all .

I am glad that you figured it out.

Now that that is out of the way, I suggest that you modify your code to dispose of the PictureBox image before assigning a new one to it.

            If IsNothing(picture) = False Then

                If frm.PB1.Image IsNot Nothing Then
                    frm.PB1.Image.Dispose()
                    frm.PB1.Image = Nothing
                End If
                frm.PB1.Image = picture
            Else
                MsgBox("Picture returned nothing")
            End If

This will release the previous image for garbage collection. If you don't your program will continue to use more and more memory.

I hope this is what you are suggesting?

 Dim pictureData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())

        Dim picture As Image = Nothing

        'Create a stream in memory containing the bytes that comprise the image.
        Using stream As New IO.MemoryStream(pictureData)

            'Read the stream and create an Image object from the data.
            picture = Image.FromStream(stream)
            'PictureBox1 = Image.FromStream(stream)

            If IsNothing(picture) = False Then
                If frm.PB1.Image IsNot Nothing Then
                    frm.PB1.Image.Dispose()
                    frm.PB1.Image = Nothing
                End If
                frm.PB1.Image = picture
            Else
                MsgBox("Picture returned nothing")
            End If

            'frm.PB1.Image = (picture)
        End Using

You got it.

Thanks a lot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.