Click here to Skip to main content
Sign Up to vote bad
good
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()
        Try
            '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
            MessageBox.Show(ex.ToString())
            txtCompanyName.Focus()
        End Try
    End Sub
Posted 5 Nov '12 - 22:12

Comments
OriginalGriff - 6 Nov '12 - 4:15
What error does it show?

3 solutions

Comment the MessageBox :
...
        Catch ex As Exception
            'MessageBox.Show(ex.ToString()) 'commented the message box
            txtCompanyName.Focus()
        End Try
  Permalink  
Comments
OriginalGriff - 6 Nov '12 - 4:21
While this solves the symptom...:laugh:
Mehdi Gholam - 6 Nov '12 - 4:34
In the absence of any other constraints the simplest solution is the best :)
OriginalGriff - 6 Nov '12 - 4:40
Ye...esss. Like putting a Band-aid on an amputation... :)
heinhtataung - 6 Nov '12 - 6:08
Thanks , it works ! :)
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.
 
Update:
 
 
if(gdCompany.CurrentRow.Cells(15).Value != null)
{
//your code here..
}
 
  Permalink  
Comments
heinhtataung - 6 Nov '12 - 23:28
So, How can i write code for that?
jim lahey - 7 Nov '12 - 3:27
Updated just for you :)
heinhtataung - 15 Nov '12 - 22:56
It shows the Identifier Expected Error at (!= ) Sign
jim lahey - 16 Nov '12 - 3:37
Oh yeah, sorry, it's VB. Use <> or Not instead of !=
heinhtataung - 16 Nov '12 - 5:32
Thanks :)
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.
  Permalink  
Comments
heinhtataung - 15 Nov '12 - 22:56
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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 586
1 Maciej Los 255
2 Slacker007 240
3 OriginalGriff 220
4 CPallini 210
0 Sergey Alexandrovich Kryukov 9,118
1 OriginalGriff 7,134
2 CPallini 3,803
3 Rohan Leuva 3,135
4 Maciej Los 2,558


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