|
-
Apr 4th, 2007, 05:08 AM
#1
Thread Starter
Frenzied Member
[2005] Display Image from Database?
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
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Apr 4th, 2007, 05:43 AM
#2
Re: [2005] Display Image from Database?
Please mark you thread resolved using the Thread Tools as shown
-
Apr 4th, 2007, 06:13 AM
#3
Thread Starter
Frenzied Member
Re: [2005] Display Image from Database?
Thanks for the link there, however i the following error:
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
It says: The execution reader requires and open and available connection. The connection's current state is closed.
What does this mean?
I tried con.open() but still same thing.
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Apr 4th, 2007, 06:17 AM
#4
Re: [2005] Display Image from Database?
Have you checked this
vb 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
Please mark you thread resolved using the Thread Tools as shown
-
Apr 4th, 2007, 07:22 AM
#5
Thread Starter
Frenzied Member
Re: [2005] Display Image from Database?
Hello,
I tried the msgbox, but it doesnt get through to it.
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Apr 4th, 2007, 08:45 AM
#6
Re: [2005] Display Image from Database?
You should have valid Connection string the Connection path
Please mark you thread resolved using the Thread Tools as shown
-
Apr 4th, 2007, 09:18 AM
#7
Thread Starter
Frenzied Member
Re: [2005] Display Image from Database?
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Jun 1st, 2007, 04:25 PM
#8
New Member
Re: [2005] Display Image from Database?
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
-
Jun 1st, 2007, 07:11 PM
#9
Lively Member
Re: [2005] Display Image from Database?
try changing connection string to:
Code:
Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data source =" & dbPath)
I used:
Code:
DBConnect.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPath
for a past project, and it seems to work.
-
Jun 2nd, 2007, 02:39 AM
#10
New Member
Re: [2005] Display Image from Database?
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.
-
Jun 4th, 2007, 10:29 AM
#11
New Member
Re: [2005] Display Image from Database?
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
-
Jun 4th, 2007, 08:21 PM
#12
Re: [2005] Display Image from Database?
-
Jun 4th, 2007, 08:24 PM
#13
Re: [2005] Display Image from Database?
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:
vb.net Code:
Dim b(imagereader.GetBytes(0, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
imagereader.GetBytes(0, 0, b, 0, b.Length)
which means you're getting the same data from the DataReader twice, just do this:
vb.net Code:
Dim b As Byte() = DirectCast(imagereader(0), Byte())
-
Jun 6th, 2007, 01:55 PM
#14
New Member
Re: [2005] Display Image from Database?
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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|