Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when I search for record using "empid" image does not load into picturebox. An error occurs. I am using VS2013 and datatype for image is "Varbinary(max)"
or should I use "Image Datatye" in sql. Which is Better ?
----------------------------------------------------------------
Error:
----------------------------------------------------------------
An unhandled exception of type 'System.InvalidCastException' occurred in Employee Management System.exe

Additional information: Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'.

-------------------------------------------------------------------
code:
-------------------------------------------------------------------
VB
try
con.Open()
       ss = "SELECT * from emp_master WHERE empid=" & txtempid.Text & ""
       com = New SqlCommand(ss, con)
       dr = com.ExecuteReader()
       dr.Read()
       If dr.HasRows = True Then
           txtempid.Text = dr(0)
           txtename.Text = dr(1)
           txtpfno.Text = dr(2)
           dtpdob.Value = dr(3)
           If dr(4) = "Male" Then
               rbmale.Checked = True
           ElseIf dr(4) = "Female" Then
               rbfemale.Checked = True
           End If
           txtcontact.Text = dr(5)
           txtemail.Text = dr(6)
           dtpdoj.Value = dr(7)
           txtaddress.Text = dr(8)
           txtedu.Text = dr(9)
           txtNation.Text = dr(10)
           cmb_Bloodgrp.Text = dr(11)
           txtdesig.Text = dr(12)
           txtexp.Text = dr(13)
           txtsalary.Text = dr(14)
           txtdept.Text = dr(15)
           PictureBox1.Image = dr(16)
           Cmb_utype.Text = dr(17)
       Else
           MsgBox("No Record Found !", MsgBoxStyle.OkOnly, MsgBoxStyle.Exclamation)
       End If
        Catch ex As Exception
          MsgBox(ex.Message)
        Finally
       dr.Close()
       con.Close()
        End Try
Posted
Comments
Richard MacCutchan 15-Feb-15 8:59am    
I suspect that dr(16) is not an Image.
A94 15-Feb-15 9:36am    
dr(16) id the location where image is stored in varbinary type but how to convert back to image and load into picturebox
Kuthuparakkal 15-Feb-15 10:09am    
Dim productImageByte As Byte() = TryCast(dr(16), Byte())
If productImageByte IsNot Nothing Then
Using productImageStream As Stream = New System.IO.MemoryStream(productImageByte)
PictureBox1.Image = Image.FromStream(productImageStream)
End Using
End If
A94 15-Feb-15 10:17am    
Please can you help me to understand this code. And thank you very much.

1 solution

VB
Dim productImageByte As Byte() = TryCast(dr(16), Byte())
If productImageByte IsNot Nothing Then
Using productImageStream As Stream = New System.IO.MemoryStream(productImageByte)
PictureBox1.Image = Image.FromStream(productImageStream)
End Using
End If
 
Share this answer
 

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