Question how to check datagridviewimagecolumn is null or not ?

Mazhar

Member
Joined
Aug 27, 2016
Messages
15
Programming Experience
Beginner
I m using this code to Put image from Datagridviewimage column to Picturebox

VB.NET:
Private Sub Dgv1_CellDoubleClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Dgv1.CellDoubleClick
try
     Dim pCell As New DataGridViewImageCell
            pCell = Me.Dgv1.Item("PictureDataGridViewImageColumn", e.RowIndex)
            Me.ProductPicture.Image = byteArrayToImage(pCell.Value)
           
        Catch ex As Exception
            MessageBox.Show("Error :" & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try


Private Function byteArrayToImage(ByVal byt As Byte()) As Image
        Dim ms As New System.IO.MemoryStream()
        Dim drwimg As Image = Nothing

        Try
            ms.Write(byt, 0, byt.Length)
            drwimg = New Bitmap(ms)
        Finally
            ms.Close()
        End Try
        Return drwimg

        Conn.Close()
    End Function
end sub
I want to know how can I Put PictureBox =Nothing if DatagridviewimageCoulumn is null i.e (has not picture)

Thx.
 
If you've populated your grid by binding a DataTable that you filled from a database then, just like every other cell, your image cell will contain DBNull.Value if there's no value.
 
If you've populated your grid by binding a DataTable that you filled from a database then, just like every other cell, your image cell will contain DBNull.Value if there's no value.

Thnx for ur reply, I changed my code as under and its working as desired.
VB.NET:
  If IsDBNull(Dgv1.CurrentRow.Cells("PictureDataGridViewImageColumn").Value) Then
                Me.ProductPicture.Image = Nothing
            Else
                Dim pCell As New DataGridViewImageCell
                pCell = Dgv1.CurrentRow.Cells("PictureDataGridViewImageColumn")
                Me.ProductPicture.Image = byteArrayToImage(pCell.Value)
            End If
 
If you're creating an Image object, you should also be disposing it when you're done with it. That means calling Dispose on the current Image property value if it's not Nothing before setting the property.
 
If you're creating an Image object, you should also be disposing it when you're done with it. That means calling Dispose on the current Image property value if it's not Nothing before setting the property.

Dear Thank u for ur information, I m not so expert in vb.net and is new, however, i tried to use code like

VB.NET:
   If IsDBNull(Dgv1.CurrentRow.Cells("PictureDataGridViewImageColumn").Value) Then 'DataGridViewImageColumn

                Me.ProductPicture.Image = Nothing
             
            Else
             
                Dim pCell As New DataGridViewImageCell
                pCell = Dgv1.CurrentRow.Cells("PictureDataGridViewImageColumn") 'DataGridViewImageColumn
                Me.ProductPicture.Image = byteArrayToImage(pCell.Value)
                Me.ProductPicture.Image.Dispose()

            End If
I receive an error AurgumentExeption was unhandled : Parameter is not valid.
 
Let's examine what I said:
That means calling Dispose on the current Image property value if it's not Nothing before setting the property.
So, firstly, I said "before setting the property". Take a look at your code. Are you calling Dispose BEFORE setting the Image property?

Secondly, I said "before setting the property". Take a look at your code. You're setting the Image property in the If block as well as the Else block but you're only calling Dispose if you enter the Else block. Is that following my instructions?

Let's think this through. The Image property is being set in both the If block and the Else block. That means an existing Image object is going to need to be disposed either way. As such, there's no point putting the same code in both blocks but rather just write the code once outside the block. I clearly said BEFORE so that tells you where to put the code. Finally, there has to actually be an existing Image object to dispose so you'll need to check that first. In short, check whether there is an existing Image, dispose it if there is, then set the Image property as appropriate.
 
Let's examine what I said:So, firstly, I said "before setting the property". Take a look at your code. Are you calling Dispose BEFORE setting the Image property?

Secondly, I said "before setting the property". Take a look at your code. You're setting the Image property in the If block as well as the Else block but you're only calling Dispose if you enter the Else block. Is that following my instructions?

Let's think this through. The Image property is being set in both the If block and the Else block. That means an existing Image object is going to need to be disposed either way. As such, there's no point putting the same code in both blocks but rather just write the code once outside the block. I clearly said BEFORE so that tells you where to put the code. Finally, there has to actually be an existing Image object to dispose so you'll need to check that first. In short, check whether there is an existing Image, dispose it if there is, then set the Image property as appropriate.



Dear Thank u very much your explanations and info. It's my mistake that i could not understand correctly, perhaps because of Little knowledge of vb.net. anyhow now I change the code like :
VB.NET:
If IsDBNull(Dgv1.CurrentRow.Cells("PictureDataGridViewImageColumn").Value) Then 'DataGridViewImageColumn
               '''  Me.ProductPicture.Image.Dispose()
                Me.TxtImageText.Visible = True
                Me.TxtImageText.Text = "No Preview Available"
            Else
                Me.TxtImageText.Visible = False
                Dim pCell As New DataGridViewImageCell
                pCell = Dgv1.CurrentRow.Cells("PictureDataGridViewImageColumn") 'DataGridViewImageColumn
                Me.ProductPicture.Image = byteArrayToImage(pCell.Value)
                pCell.Dispose()
            End If
It did not give any error works fine, but there is another problem Which I tried my best to solve but did not succeed.
here is the code you told me earlier.

VB.NET:
 Dim Picvalue As Object = IIf(ProductPicture.Image Is Nothing, CObj(DBNull.Value), GetImageData(ProductPicture.Image))
cmd.Parameters.Add("@Picture", SqlDbType.VarBinary, -1).Value = Picvalue


Private Function GetImageData(image As Image) As Byte()

        Dim data As Byte()
        'Create an empty stream in memory.

        Using stream As New IO.MemoryStream
            '* * * * 'Fill the stream with the binary data from the Image.

            image.Save(stream, Imaging.ImageFormat.Jpeg)
                        '* * * * 'Get an array of Bytes from the stream.
            data = stream.ToArray()

        End Using
        Return data
    End Function

It works fine at the time of save but in update it works only when i update the picture column, If I want to update any other column it gives the Error: "A generic error occurred in GDI+"
kindly correct my code. I will be very thankful to you.
 
Last edited:

Latest posts

Back
Top