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
Re: empty string to picturebox
You need to check the field for 'isdbnull' before assigning it
Re: empty string to picturebox
Calling ToString on a DBNull object will return an empty string, so if you change this:
vb.net Code:
img = FoodjournalDataGrid.Item(currentrow, 2)
to this:
vb.net Code:
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:
If PictureBox1.Image IsNot Nothing Then
PictureBox1.Image.Dispose()
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.
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..:ehh:
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
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.