Click here to Skip to main content
12,406,528 members (55,769 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# VB Windows .NET VB.NET
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:12pm
Comments
OriginalGriff 6-Nov-12 4:15am
   
What error does it show?
Rate this: bad
 
good
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
            txtCompanyName.Focus()
        End Try
  Permalink  
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 !
:)
Rate this: bad
 
good
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.

Update:

if(gdCompany.CurrentRow.Cells(15).Value != null)
{
//your code here..
}
  Permalink  
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 :)
Rate this: bad
 
good
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.
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160730.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2016
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