Results 1 to 7 of 7

Thread: how to check datagridviewimagecolumn is null or not ?

  1. #1
    Mazhar is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2016
    Posts
    15
    Reputation
    11

    how to check datagridviewimagecolumn is null or not ?

    I m using this code to Put image from Datagridviewimage column to Picturebox

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

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,814
    Reputation
    1707
    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.

  3. #3
    Mazhar is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2016
    Posts
    15
    Reputation
    11
    Quote Originally Posted by jmcilhinney View Post
    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.
    Code:
      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

  4. #4
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,814
    Reputation
    1707
    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.

  5. #5
    Mazhar is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2016
    Posts
    15
    Reputation
    11
    Quote Originally Posted by jmcilhinney View Post
    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

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

  6. #6
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,814
    Reputation
    1707
    Let's examine what I said:
    Quote Originally Posted by jmcilhinney View Post
    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.

  7. #7
    Mazhar is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2016
    Posts
    15
    Reputation
    11
    Quote Originally Posted by jmcilhinney View Post
    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 :
    Code:
    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.

    Code:
        
     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 by Mazhar; 09-07-2016 at 6:28 AM.

Bookmarks

Posting Permissions

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