Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
When the image exists at the database , It shows correctly at pictureBox.
If the image doesn't exist, it shows error.

The Code is Here !
  Private Sub gdCompany_CellClick_1(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles gdCompany.CellClick
        btnSave.Text = "ျပင္ရန္"
        picCompanyLogo.Visible = True
        txtCompanyName.Text = gdCompany.CurrentRow.Cells(1).Value.ToString()
        txtDescription.Text = gdCompany.CurrentRow.Cells(2).Value.ToString()
        txtPhoneNo.Text = gdCompany.CurrentRow.Cells(3).Value.ToString()
        txtFaxNo.Text = gdCompany.CurrentRow.Cells(4).Value.ToString()
        txtEmail.Text = gdCompany.CurrentRow.Cells(5).Value.ToString()
        txtWebsite.Text = gdCompany.CurrentRow.Cells(6).Value.ToString()
        ddlBankName.SelectedValue = Convert.ToInt32(gdCompany.CurrentRow.Cells(7).Value.ToString())
        ddlBankBranch.SelectedValue = Convert.ToInt32(gdCompany.CurrentRow.Cells(8).Value.ToString())
        txtBankAccountNo.Text = gdCompany.CurrentRow.Cells(9).Value.ToString()
        txtAddress.Text = gdCompany.CurrentRow.Cells(10).Value.ToString()
            'Get image data from gridview column. 
            Dim imageData As Byte() = DirectCast(gdCompany.CurrentRow.Cells(15).Value, Byte())
            'Initialize image variable 
            Dim newImage As Image
            'Read image data into a memory stream 
            Using ms As New MemoryStream(imageData, 0, imageData.Length)
                ms.Write(imageData, 0, imageData.Length)
                'Set image variable value using memory stream. 
                newImage = Image.FromStream(ms, True)
            End Using
            'set picture 
            picCompanyLogo.Image = newImage
        Catch ex As Exception
        End Try
    End Sub
Posted 5-Nov-12 23:12pm
OriginalGriff at 6-Nov-12 4:15am
What error does it show?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Comment the MessageBox :
        Catch ex As Exception
            'MessageBox.Show(ex.ToString()) 'commented the message box
        End Try
OriginalGriff at 6-Nov-12 4:21am
While this solves the symptom...:laugh:
Mehdi Gholam at 6-Nov-12 4:34am
In the absence of any other constraints the simplest solution is the best :)
OriginalGriff at 6-Nov-12 4:40am
Like putting a Band-aid on an amputation... :)
heinhtataung at 6-Nov-12 6:08am
Thanks , it works !
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I suggest checking for the possibility of gdCompany.CurrentRow.Cells(15).Value being null before you try to cast it to a byte array and try to populate the pictureBox.
if(gdCompany.CurrentRow.Cells(15).Value != null)
//your code here..
heinhtataung at 6-Nov-12 23:28pm
So, How can i write code for that?
jim lahey at 7-Nov-12 3:27am
Updated just for you :)
heinhtataung at 15-Nov-12 22:56pm
It shows the Identifier Expected Error at (!= ) Sign
jim lahey at 16-Nov-12 3:37am
Oh yeah, sorry, it's VB. Use <> or Not instead of !=
heinhtataung at 16-Nov-12 5:32am
Thanks :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

here is what i do, when if the image is not present in database i want to show a sample image,named "unknownUser" in my resources.
Private Sub DataGridView1_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
        If e.ColumnIndex = 1 Then
            Dim cell As DataGridViewImageCell = DataGridView1.Rows(e.RowIndex).Cells(1)
            cell.Value = My.Resources.UnknownUser
        End If
    End Sub
so if you use this, there wont be any error "X" image in your grid, and your code wont throw any exception at all.
heinhtataung at 15-Nov-12 22:56pm
After adding your code , Following error shows !
Value of type 'Telerik.WinControls.UI.GridViewCellInfo' cannot be converted to 'System.Windows.Forms.DataGridViewImageCell'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,535
1 OriginalGriff 8,295
2 Peter Leow 4,839
3 Kornfeld Eliyahu Peter 3,210
4 Maciej Los 2,301

Advertise | Privacy | Mobile
Web03 | 2.8.150327.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100