Hello,
Can anyone assist me on pulling out an image from a database to be displayed on a picturebox?
I can get values of fields in the database, but get error when trying to get the image. The image is as an OLE in the database field.
Thanks
Printable View
Hello,
Can anyone assist me on pulling out an image from a database to be displayed on a picturebox?
I can get values of fields in the database, but get error when trying to get the image. The image is as an OLE in the database field.
Thanks
Thanks for the link there, however i the following error:
It says: The execution reader requires and open and available connection. The connection's current state is closed.Code:Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data source ='" & dbPath & "';")
Dim imagecmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("select *from Service WHERE ID = '1'", con)
Dim imagereader As OleDb.OleDbDataReader
imagereader = imagecmd.ExecuteReader
imagereader.Read()
Dim b(imagereader.GetBytes(1, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
imagereader.GetBytes(1, 0, b, 0, b.Length)
imagereader.Close()
Dim ms As New System.IO.MemoryStream(b)
picLogo.Image = Image.FromStream(ms) 'PicError it the Picturebox
What does this mean?
I tried con.open() but still same thing.
Have you checked thisvb Code:
Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data source ='" & dbPath & "';")
Try this
vb Code:
Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data source ='" & dbPath & "';") Dim imagecmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("select *from Service WHERE ID = '1'", con) If con.State = ConnectionState.Open Then MessageBox.Show("Not able to connect to database") End If Dim imagereader As OleDb.OleDbDataReader imagereader = imagecmd.ExecuteReader imagereader.Read() Dim b(imagereader.GetBytes(1, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte imagereader.GetBytes(1, 0, b, 0, b.Length) imagereader.Close() Dim ms As New System.IO.MemoryStream(b) picLogo.Image = Image.FromStream(ms) 'PicError it the Picturebox
Hello,
I tried the msgbox, but it doesnt get through to it.
You should have valid Connection string the Connection path
It is valid...
I tried the code above and I get an "Invalid parameter" error....
I've tried a couple of solutions for this same problema and I always end up at the same point "Invalid parameter" error :(
Any hints???
This is what I wrote:
vb Code:
Dim dbpath As String = "D:\DB1.mdb" Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data source ='" & dbPath & "';") Dim imagecmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT photo FROM Table1 WHERE ID=1", con) If con.State = ConnectionState.Open Then MessageBox.Show("Not able to connect to database") End If Dim imagereader As OleDb.OleDbDataReader con.Open() imagereader = imagecmd.ExecuteReader imagereader.Read() Dim b(imagereader.GetBytes(0, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte imagereader.GetBytes(0, 0, b, 0, b.Length) imagereader.Close() Dim ms As New System.IO.MemoryStream(b) pbImage.Image = Image.FromStream(ms) 'This is where I get the error message
try changing connection string to:
I used:Code:Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data source =" & dbPath)
for a past project, and it seems to work.Code:DBConnect.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPath
A little off topic, but are you actually storing the image in the database or a link to the image?
According to Database Design for Mere Mortals written by Michael J. Hernandez, storing images in the database is a bad idea. He sites many disadvantages including the eventual size of the database makes queries run slower, what do you do when you want to view those images with an outside program, and how sql server allocates space for images even though each record may not have an image associated with it.
Better method is to have an 'image' folder and store the url to each image in your database. This way if you need to edit the image or view it with another app you are able to. When it comes time to deleting the record from the database you also need to delete the image from the 'image' folder unless you have some reason to not want to...but then again why would you want it in your current apps 'image' folder.
some1uk03 you should try printing or outputting (in a msgbox) the connection state so you can verify that it's open. Maybe you're missing something.
daemonk I tried the ole connection string but it doesn't seem to work. As the matter of fact that's the exact same connection string I'm using right now.
Does anyone have a clue about why does this code throw the "Invalid parameter" exception????
Hi Greedy4chips! Well one of the reasons I store the "image" in the database and not just the link, is because of security. I know that what you say is true, but the images I need to store in the db are "very sensitive", I don't want everybody looking at them and obviously don't want to give "anyone" the chance of changing or modifying the image. In that case I think storing the image in the database is quite better than just storing a link in the db and the image in a folder....
Regards,
JarZe
If you want to use a DataReader instead of ExecuteScalar, which you would only do if you were retrieving more than one row or more than one column, then rather than phaffing around like this:which means you're getting the same data from the DataReader twice, just do this:vb.net Code:
Dim b(imagereader.GetBytes(0, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte imagereader.GetBytes(0, 0, b, 0, b.Length)vb.net Code:
Dim b As Byte() = DirectCast(imagereader(0), Byte())
Thanks jmcilhinney, but I get an error if I try this
vb Code:
Dim b() As Byte ' The OLE offset is 78 offset = 78 imagereader.GetBytes(0, 0, b, offset, b.Length - offset) imagereader.Close()
I think it's because I havent set the dimension for the array...
And remember that I still have to take the 78 bytes from the OLE header. I've been able to take the 78 bytes off, but I still get the Invalid Parameter exception...
I did solve the issue though, I opened the Access Db and programatically saved the image for each record. I had to move within the form recordset and it took a while, since the query had like 1700 records. Anyhow, I do want to know why I wasn't able to do it from outside Access, which means VB.Net 05...