Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 !

VB
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
Comments
OriginalGriff 6-Nov-12 4:15am    
What error does it show?

Comment the MessageBox :
VB
...
        Catch ex As Exception
            'MessageBox.Show(ex.ToString()) 'commented the message box
            txtCompanyName.Focus()
        End Try
 
Share this answer
 
Comments
OriginalGriff 6-Nov-12 4:21am    
While this solves the symptom...:laugh:
Mehdi Gholam 6-Nov-12 4:34am    
In the absence of any other constraints the simplest solution is the best :)
OriginalGriff 6-Nov-12 4:40am    
Ye...esss.
Like putting a Band-aid on an amputation... :)
heinhtataung 6-Nov-12 6:08am    
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:

C#
if(gdCompany.CurrentRow.Cells(15).Value != null)
{
//your code here..
}
 
Share this answer
 
v2
Comments
heinhtataung 6-Nov-12 23:28pm    
So, How can i write code for that?
jim lahey 7-Nov-12 3:27am    
Updated just for you :)
heinhtataung 15-Nov-12 22:56pm    
It shows the Identifier Expected Error at (!= ) Sign
jim lahey 16-Nov-12 3:37am    
Oh yeah, sorry, it's VB. Use <> or Not instead of !=
heinhtataung 16-Nov-12 5:32am    
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.

VB
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.
 
Share this answer
 
Comments
heinhtataung 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900