Results 1 to 5 of 5

Thread: empty string to picturebox

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    42

    empty string to picturebox

    carry on from the last question the "selectpicturedialog", i now faced another problem.

    I have a picturebox which display photo which i selected in the dialog, i save the image path to a sql database along with some other data.
    it all save & load fine from the database.
    then i went on and test to save no image in the picturebox and just the text data. so , in the database image column is "null", it saved well. but i can not load it. it gave me an error.

    here my code: the red part causes the problem

    Code:
     Private Sub FoodjournalDataGrid_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FoodjournalDataGrid.Click
            Dim currentrow As Integer = FoodjournalDataGrid.CurrentRowIndex
            Dim img As String
    
            If FoodjournalDataGrid.VisibleRowCount <> 0 Then
                DateTimePicker1.Value = FoodjournalDataGrid.Item(currentrow, 0)
                TextBox1.Text = FoodjournalDataGrid.Item(currentrow, 1)
                img = FoodjournalDataGrid.Item(currentrow, 2)
                If img = String.Empty Then
                    PictureBox1.Image = Nothing
                Else
                    PictureBox1.Image = New Bitmap(img)
                End If
            Else
                MsgBox("No record found")
            End If
            FoodjournalDataGrid.Visible = False
        End Sub

  2. #2
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: empty string to picturebox

    You need to check the field for 'isdbnull' before assigning it
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: empty string to picturebox

    Calling ToString on a DBNull object will return an empty string, so if you change this:
    vb.net Code:
    1. img = FoodjournalDataGrid.Item(currentrow, 2)
    to this:
    vb.net Code:
    1. img = FoodjournalDataGrid.Item(currentrow, 2).ToString()
    it will work.

    There's another issue with your code though. You are simply discarding the current Image each time so you're creating a resource leak. You should be disposing the current Image, if there is one, before assigning a new value to the Image property of the PictureBox:
    vb.net Code:
    1. If PictureBox1.Image IsNot Nothing Then
    2.     PictureBox1.Image.Dispose()
    3. End If
    An alternative would be to create a Dictionary to store all the Images you create. That would save you creating the same image a second time if the user selects the same record again. That might be a bit memory-intensive on a mobile device though.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    42

    Re: empty string to picturebox

    ok i have added like you said, before assigning it to a new value.
    is this correct now?
    and how and where can i see the difference between not having to dispose and dispose?

    also i have a button to clear images and text on the form, so would i also need dispose for that? but when i do that , i will just give me error. plz help me to understand dispose....Thx..

    Code:
    Private Sub FoodjournalDataGrid_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FoodjournalDataGrid.Click
            Dim currentrow As Integer = FoodjournalDataGrid.CurrentRowIndex
            Dim img As String
    
            If FoodjournalDataGrid.VisibleRowCount <> 0 Then
                DateTimePicker1.Value = FoodjournalDataGrid.Item(currentrow, 0)
                TextBox1.Text = FoodjournalDataGrid.Item(currentrow, 1)
                If IsDBNull(FoodjournalDataGrid.Item(currentrow, 2)) Then
                    PictureBox1.Image = Nothing
                Else
    
                    If PictureBox1.Image IsNot Nothing Then
                        PictureBox1.Image.Dispose()
                    End If
                    img = FoodjournalDataGrid.Item(currentrow, 2)
                    PictureBox1.Image = New Bitmap(img)
                End If
            Else
                MsgBox("No record found")
            End If
                FoodjournalDataGrid.Visible = False
        End Sub

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: empty string to picturebox

    You still aren't disposing the current Image if there is no image for the new row. As you have it there, if there's already an Image in the PictureBox and the user slects a row with no image then you are simply discarding the existing Image without disposing it, creating a resource leak. You need to take the If block that disposes the Image and put it BEFORE the other If block, so an existing Image will be disposed no matter what.

    Knowing when you need to dispose is easy. If you no longer need to use the object and it has a Dispose method then you must dispose it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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