4 Replies - 2010 Views - Last Post: 25 January 2013 - 03:17 AM Rate Topic: -----

#1 chdboy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 26-July 12

Buffer cannot be null

Posted 22 January 2013 - 09:09 AM

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


Is This A Good Question/Topic? 0
  • +

Replies To: Buffer cannot be null

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5625
  • View blog
  • Posts: 12,064
  • Joined: 02-June 10

Re: Buffer cannot be null

Posted 22 January 2013 - 10:10 AM

put a breakpoint on line 45.
Look at the Locals pallet.
I bet you'll find that pictureData is null.
Therefore line 45 fails saying the buffer is null.

What this shows us is that you aren't familiar with breakpoints and how to debug your own code.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Jan 2013
Spoiler



Was This Post Helpful? 1
  • +
  • -

#3 chdboy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 26-July 12

Re: Buffer cannot be null

Posted 23 January 2013 - 04:31 AM

Yes you are right ,that line gives me error ,and yes I know about breakpoints ,but don't know how to debug my own project.That is why I seek help online.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5625
  • View blog
  • Posts: 12,064
  • Joined: 02-June 10

Re: Buffer cannot be null

Posted 23 January 2013 - 07:22 AM

That's why I gave you several links to tutorials to teach you debugging.

Since you already know that line 45 pictureData is null, then work backwards. Where does pictureData gets its value from? Line 38.
Therefore it must be that the query doesn't return results that become the value of that variable.

This is where you have to start realizing that never everything will alway work as you assume. Things fail. Results can be empty. You have to start checking for these things with if...else constructs as well as try...catch

Some of my common tips (some may apply more than others to your specific style):
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Don't use your GUI objects as your variable. In other words don't keep referencing TextBox4.Text everyplace. TextBox4.Text is not a variable or property. The GUI is on its own thread so as soon as you start doing multi-threading you're screwed because your worker thread can't access the GUI elements. Use properties.

  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attempts below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.


  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • I strongly suggest installing VMware or some other virtualization technology on your development PC so you can create a couple virtual computers for testing. This would allow you to debug and test inside: WinXP32, XP64, Vista, Win7x32, Win7x64... etc. without having to actually have 5 physical PC's. Visual Studio will let you send the debug directly into one of these virtual machines so you can watch it operate, check its variables, see the crashes and so on just as if it were debugging on your real machine.

  • This can't be stressed enough in today's world of cell phone messaging:
    Don't use txt/sms/leet/T9 speak like: u no, u r, dnt, wut i m do-n, coz, al gud, b4, ny1, sum1, please and so on like this guy:

    Spoiler

[/spoiler]
Was This Post Helpful? 1
  • +
  • -

#5 chdboy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 26-July 12

Re: Buffer cannot be null

Posted 25 January 2013 - 03:17 AM

my where clause has a problem

I have changed it to this
where Firstname = '" & _
                  DGV1.CurrentRow.Cells(1).Value() & "'"


and it worked fine now.Special thanks to 'tlhIn`toq'.
self debugging is a very cool and good thing.
Thank you :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1