Results 1 to 14 of 14

Thread: [2005] Display Image from Database?

  1. #1

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    Question [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.



  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: [2005] Display Image from Database?

    Please mark you thread resolved using the Thread Tools as shown

  3. #3

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    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.



  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: [2005] Display Image from Database?

    Have you checked this
    vb Code:
    1. Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data source ='" & dbPath & "';")

    Try this
    vb Code:
    1. Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data source ='" & dbPath & "';")
    2.         Dim imagecmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("select *from Service WHERE ID = '1'", con)
    3.         If con.State = ConnectionState.Open Then
    4.             MessageBox.Show("Not able to connect to database")
    5.         End If
    6.         Dim imagereader As OleDb.OleDbDataReader
    7.  
    8.         imagereader = imagecmd.ExecuteReader
    9.         imagereader.Read()
    10.  
    11.         Dim b(imagereader.GetBytes(1, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
    12.  
    13.         imagereader.GetBytes(1, 0, b, 0, b.Length)
    14.         imagereader.Close()
    15.         Dim ms As New System.IO.MemoryStream(b)
    16.         picLogo.Image = Image.FromStream(ms) 'PicError it the Picturebox
    Please mark you thread resolved using the Thread Tools as shown

  5. #5

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    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.



  6. #6
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    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

  7. #7

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    Re: [2005] Display Image from Database?

    It is valid...
    _____________________________________________________________________

    ----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.



  8. #8
    New Member
    Join Date
    Jun 2007
    Posts
    3

    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:
    1. Dim dbpath As String = "D:\DB1.mdb"
    2.         Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data source ='" & dbPath & "';")
    3.         Dim imagecmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT photo FROM Table1 WHERE ID=1", con)
    4.  
    5.         If con.State = ConnectionState.Open Then
    6.  
    7.             MessageBox.Show("Not able to connect to database")
    8.  
    9.         End If
    10.  
    11.         Dim imagereader As OleDb.OleDbDataReader
    12.  
    13.  
    14.         con.Open()
    15.         imagereader = imagecmd.ExecuteReader
    16.  
    17.         imagereader.Read()
    18.  
    19.  
    20.  
    21.         Dim b(imagereader.GetBytes(0, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
    22.  
    23.  
    24.  
    25.         imagereader.GetBytes(0, 0, b, 0, b.Length)
    26.  
    27.         imagereader.Close()
    28.  
    29.         Dim ms As New System.IO.MemoryStream(b)
    30.  
    31.         pbImage.Image = Image.FromStream(ms) 'This is where I get the error message

  9. #9
    Lively Member
    Join Date
    Apr 2005
    Posts
    127

    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.

  10. #10
    New Member
    Join Date
    Jun 2007
    Posts
    12

    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.

  11. #11
    New Member
    Join Date
    Jun 2007
    Posts
    3

    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

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Display Image from Database?

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim b(imagereader.GetBytes(0, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
    2.  
    3. 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:
    1. Dim b As Byte() = DirectCast(imagereader(0), Byte())
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14
    New Member
    Join Date
    Jun 2007
    Posts
    3

    Re: [2005] Display Image from Database?

    Thanks jmcilhinney, but I get an error if I try this
    vb Code:
    1. Dim b() As Byte
    2.  
    3.         ' The OLE offset is 78
    4.         offset = 78
    5.  
    6.         imagereader.GetBytes(0, 0, b, offset, b.Length - offset)
    7.  
    8.         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
  •  



Click Here to Expand Forum to Full Width