Results 1 to 12 of 12

Thread: [RESOLVED] Buffer cannot be null

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Resolved [RESOLVED] Buffer cannot be null

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

    Name:  Buffer_Error.jpg
Views: 1997
Size:  14.4 KB

    and here is the code

    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

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Buffer cannot be null

    It would be nice if you temporarily removed your Try Catch code so that you could tell us which line you're getting the error. But I suspect that you get it on your Using stream line and that your pictureData is null.

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Buffer cannot be null

    Clearly pictureData is null causing the failure of the Stream.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: Buffer cannot be null

    Quote Originally Posted by Joacim Andersson View Post
    It would be nice if you temporarily removed your Try Catch code so that you could tell us which line you're getting the error. But I suspect that you get it on your Using stream line and that your pictureData is null.
    Yes you are right I'm getting an error on that line.
    here is the screen shot without Try and catch

    Name:  Buffer_error_1.png
Views: 1758
Size:  11.4 KB
    How can I change the code to make it work?

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Buffer cannot be null

    Yes, so obviously your query does not return a byte array.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: Buffer cannot be null

    ok my sql query has a problem ?

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Buffer cannot be null

    Yes, obviously this call returns null.
    Code:
     Dim pictureData As Byte() = DirectCast(command.ExecuteScalar(), Byte())
    Try setting a break point at this line as see what you get.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: Buffer cannot be null

    I have changed the Sql statement ..where clause is gone,I have got the image but not according to the name,just one picture is showing no matter which row I click .

    Code:
    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

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Buffer cannot be null

    Yes, you're calling ExecuteScalar which will only return 1 result. Call ExecuteReader instead and loop through the result.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: Buffer cannot be null

    My where clause has the problem ...so I changed it to this

    Code:
    where Firstname = '" & _
                      DGV1.CurrentRow.Cells(1).Value() & "'"
    and now it works fine
    Thank you all for guiding me and helping me in this thread ,I have learned some new things in this process .I hope I will keep that in mind.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: Buffer cannot be null

    Now how do I make this thread [solved]?

  12. #12
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Buffer cannot be null

    Pull down the Thread Tools menu close to the top and select Mark as resolved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width