Click here to Skip to main content
15,920,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,I am dealing with picturebox retrieval in vb.net.If i run a program it displays a error message saying that "Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image".Please help me sorting out this problem.

What I have tried:

Private Sub view_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim conn As New SqlConnection
Dim mycommand As SqlCommand = New SqlCommand()
Dim datareader As SqlDataReader = Nothing
conn.ConnectionString = "Data Source=HP\SQLEXPRESS;Initial Catalog=CRIME;Integrated Security=True"
conn.Open()
Dim query As String
query = "select [DATE OF ARREST],[NAME OF CRIMINAL],[ADDRESS OF CRIMINAL],[OFFENCE OF CRIMINAL],[DESCRIPTION OF CRIMINAL],[CONFESSION OF CRIMINAL],[ACTION AGAINST CRIMINAL],[PHOTO OF CRIMINAL],[FINGER PRINT] FROM CRIMINAL WHERE [CRIMINAL REPORT NUMBER]='" & TextBox1.Text & "'"
mycommand = New SqlCommand(query, conn)
datareader = mycommand.ExecuteReader()
While datareader.Read
If datareader IsNot Nothing Then
DateTimePicker1.Value = datareader.Item("DATE OF ARREST")
TextBox2.Text = datareader.Item("NAME OF CRIMINAL")
TextBox3.Text = datareader.Item("ADDRESS OF CRIMINAL")
TextBox4.Text = datareader.Item("OFFENCE OF CRIMINAL")
TextBox5.Text = datareader.Item("DESCRIPTION OF CRIMINAL")
TextBox6.Text = datareader.Item("CONFESSION OF CRIMINAL")
TextBox7.Text = datareader.Item("ACTION AGAINST CRIMINAL")
PictureBox1.Image = datareader.Item("PHOTO OF CRIMINAL")
PictureBox2.Image = datareader.Item("FINGER PRINT")
End If
End While
conn.Close()
End Sub
Posted
Updated 21-Oct-17 3:01am

1 solution

You can't just cast a byte array to an Image.
Try this:
Dim ms As New MemoryStream(arrayOfBytes)
Dim returnImage As Image = Image.FromStream(ms)


But ... don't do DB access like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And if that is typical of the code you used to enter the images to the DB, they are going to be useless. See here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - the code is in C#, but it's pretty obvious.
 
Share this answer
 
v2

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