Question Display Picture

tqmd1

Well-known member
Joined
Dec 5, 2009
Messages
60
Programming Experience
Beginner
Dear Sir,

SQL Table employees has data as follows

sno--name----img-----img_path
1-----A----Binary---D:\C2009\BITMAPS\PICT.JPG
2-----B----Binary---D:\C2009\BITMAPS\eric.JPG


Now I want to display picture in picturebox1.
To do this I use this codes, but it does not display picture

on this line: Dim arrPicture() As Byte = CType(dt2.Rows(0)("img_path"), Byte())
it shows this message, please help

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.

VB.NET:
str2 = "select * from employees where sno =" & Val(Me.TextBox1.Text)
cmd2 = New SqlClient.SqlCommand(str2, con)
da2 = New SqlClient.SqlDataAdapter(cmd2)
dt2 = New DataTable
da2.Fill(dt2)

If (dt2.Rows.Count >= 1) Then
Me.TextBox2.Text = dt2.Rows(0)("name")
Me.TextBox3.Text = dt2.Rows(0)("city")
Me.TextBox4.Text = dt2.Rows(0)("phone")

Dim arrPicture() As Byte = CType(dt2.Rows(0)("img_path"), Byte())

Dim ms As New MemoryStream(arrPicture)

With PictureBox1
.Image = Image.FromStream(ms)
.SizeMode = PictureBoxSizeMode.StretchImage
.BorderStyle = BorderStyle.Fixed3D
End With
 
First, are you sure that a valid value is completing the SQL query? I would probably use Integer.TryParse(), Integer.Parse(), or Convert.ToInt32() rather than Val()

Second have you tried stepping through in debug mode to make sure that dt2 is being filled with a values? The error suggests that the particular field img_path is null when it gets to that line.

Third are you sure that you are casting the correct field into the byte array? It seems to me that you would be more likely to be trying to put the img field rather than the path into a byte array, but I don't know anything else about the requirements.
 
Back
Top