locked
Buffer cannot be null RRS feed

  • Question

  • 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

    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

    Tuesday, January 22, 2013 4:26 PM

Answers

  • You may also want to consider making your query a parameterized query, as well.   Right now, you're setting yourself up for SQL injection issues, as well as escaping issues, as you're building the SQL by hand.

    That being said, I suspect Blackwood is correct - pictureData is likely Nothing, which will cause the error you're receiving.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Mike Feng Thursday, January 24, 2013 3:06 AM
    • Marked as answer by Mike Feng Friday, January 25, 2013 11:24 AM
    Tuesday, January 22, 2013 4:58 PM
  • So my where clause has a problem....so I changed it to this

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

    and now it worked fine ,in this process I learned that what are parameterized query,I used this before in my code ,but I don't know what they are called .Thanks to ' Reed Copsey, Jr'and few other things.I hope I will keep that all in my mind for future.

    Thank you all who helped me in this thread.

    • Marked as answer by chdboy Friday, January 25, 2013 10:09 AM
    Friday, January 25, 2013 10:08 AM

All replies

  • You didn't sat which statement got the error. Was it this one?

    Using stream As New IO.MemoryStream(pictureData)

    If so, then pictureData must be null. Go back to the command.ExecuteScalar statement and find out why it is not returning what you expect.

    • Proposed as answer by Mike Feng Wednesday, January 23, 2013 11:40 AM
    Tuesday, January 22, 2013 4:50 PM
  • You may also want to consider making your query a parameterized query, as well.   Right now, you're setting yourself up for SQL injection issues, as well as escaping issues, as you're building the SQL by hand.

    That being said, I suspect Blackwood is correct - pictureData is likely Nothing, which will cause the error you're receiving.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Mike Feng Thursday, January 24, 2013 3:06 AM
    • Marked as answer by Mike Feng Friday, January 25, 2013 11:24 AM
    Tuesday, January 22, 2013 4:58 PM
  • You didn't sat which statement got the error. Was it this one?

    Using stream As New IO.MemoryStream(pictureData)

    If so, then pictureData must be null. Go back to the command.ExecuteScalar statement and find out why it is not returning what you expect.


    Yes that is the line which gives me the error.
    Wednesday, January 23, 2013 11:10 AM
  • You didn't sat which statement got the error. Was it this one?

    Using stream As New IO.MemoryStream(pictureData)

    If so, then pictureData must be null. Go back to the command.ExecuteScalar statement and find out why it is not returning what you expect.


    Yes that is the line which gives me the error.

    Hi Chdboy,

    So please check the pictureData variable, does it nothing?

    If so, you need to check your sql query, rather than focus on this error line.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 23, 2013 11:41 AM
  • You didn't sat which statement got the error. Was it this one?

    Using stream As New IO.MemoryStream(pictureData)

    If so, then pictureData must be null. Go back to the command.ExecuteScalar statement and find out why it is not returning what you expect.


    Yes that is the line which gives me the error.

    Hi Chdboy,

    So please check the pictureData variable, does it nothing?

    If so, you need to check your sql query, rather than focus on this error line.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    I have changed the Sql queryI have deleted the where clause ,now the picture is coming but not matching(According) the name(s).

    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")
            connection.Open()
            Dim command As New SqlCommand("SELECT Imagedata FROM Employee ", connection)
    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

    Wednesday, January 23, 2013 2:35 PM
  • You need the Where clause, but you should use a parameterized query, as I posted before. 

    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, January 23, 2013 5:01 PM
  • So my where clause has a problem....so I changed it to this

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

    and now it worked fine ,in this process I learned that what are parameterized query,I used this before in my code ,but I don't know what they are called .Thanks to ' Reed Copsey, Jr'and few other things.I hope I will keep that all in my mind for future.

    Thank you all who helped me in this thread.

    • Marked as answer by chdboy Friday, January 25, 2013 10:09 AM
    Friday, January 25, 2013 10:08 AM