Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Save, Retrieve Image on SQL Database using VB.net

Save, Retrieve Image on SQL Database using VB.net

Scheduled Pinned Locked Moved Visual Basic
databasegraphicshelpcsharp
4 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EngrImad
    wrote on last edited by
    #1

    Hello; I have image on PictureBox, Cinverted to Base64String string and saved in Database as following: Dim ms As MemoryStream = New MemoryStream Dim bmp As Bitmap = New Bitmap(PictureBox1.Image) bmp.Save(ms, System.Drawing.Imaging.ImageFormat.jpg) Dim My_Photo_Str As String = Convert.ToBase64String(ms.ToArray) myqry = "update MYTABLE set COLUMN1=" & "'" & My_Photo_Str & "'" & "where COLUMN2=" & 50 mycmd = New OleDbCommand(myqry, conn) mycmd.CommandTimeout = 0 mydr = mycmd.ExecuteReader Now that image need to retrieve it or shows on PictureBox, So I used the following:- myqry = "select * from MYTABLE where COLUMN2=" & 50 mycmd = New OleDbCommand(myqry, conn) mycmd.CommandTimeout = 0 mydr = mycmd.ExecuteReader mydr.Read() Dim PIC As Byte() PIC = mydr("COLUMN1") Dim My_Photo = New MemoryStream(PIC) PictureBox1.Image = Bitmap.FromStream(My_Photo) <<<<<-----HERE GOT ERROR the error on last line while try to set image to PictureBox, and error said that cannot convert Byte() to image, If any help will so appreciated thanks

    R D Richard DeemingR 3 Replies Last reply
    0
    • E EngrImad

      Hello; I have image on PictureBox, Cinverted to Base64String string and saved in Database as following: Dim ms As MemoryStream = New MemoryStream Dim bmp As Bitmap = New Bitmap(PictureBox1.Image) bmp.Save(ms, System.Drawing.Imaging.ImageFormat.jpg) Dim My_Photo_Str As String = Convert.ToBase64String(ms.ToArray) myqry = "update MYTABLE set COLUMN1=" & "'" & My_Photo_Str & "'" & "where COLUMN2=" & 50 mycmd = New OleDbCommand(myqry, conn) mycmd.CommandTimeout = 0 mydr = mycmd.ExecuteReader Now that image need to retrieve it or shows on PictureBox, So I used the following:- myqry = "select * from MYTABLE where COLUMN2=" & 50 mycmd = New OleDbCommand(myqry, conn) mycmd.CommandTimeout = 0 mydr = mycmd.ExecuteReader mydr.Read() Dim PIC As Byte() PIC = mydr("COLUMN1") Dim My_Photo = New MemoryStream(PIC) PictureBox1.Image = Bitmap.FromStream(My_Photo) <<<<<-----HERE GOT ERROR the error on last line while try to set image to PictureBox, and error said that cannot convert Byte() to image, If any help will so appreciated thanks

      R Offline
      R Offline
      RedDk
      wrote on last edited by
      #2

      Here's a link to something that might prove useful in corraling SQL/BLOB/BINARY/C#/VB ponies. It's very tutorialish but looks to me like something that I'd review if I couldn't get my TSQL flapping behind my c#/vb frontend. Saving and Extracting BLOB Data – Basic Examples | Notes on SQL[^]

      1 Reply Last reply
      0
      • E EngrImad

        Hello; I have image on PictureBox, Cinverted to Base64String string and saved in Database as following: Dim ms As MemoryStream = New MemoryStream Dim bmp As Bitmap = New Bitmap(PictureBox1.Image) bmp.Save(ms, System.Drawing.Imaging.ImageFormat.jpg) Dim My_Photo_Str As String = Convert.ToBase64String(ms.ToArray) myqry = "update MYTABLE set COLUMN1=" & "'" & My_Photo_Str & "'" & "where COLUMN2=" & 50 mycmd = New OleDbCommand(myqry, conn) mycmd.CommandTimeout = 0 mydr = mycmd.ExecuteReader Now that image need to retrieve it or shows on PictureBox, So I used the following:- myqry = "select * from MYTABLE where COLUMN2=" & 50 mycmd = New OleDbCommand(myqry, conn) mycmd.CommandTimeout = 0 mydr = mycmd.ExecuteReader mydr.Read() Dim PIC As Byte() PIC = mydr("COLUMN1") Dim My_Photo = New MemoryStream(PIC) PictureBox1.Image = Bitmap.FromStream(My_Photo) <<<<<-----HERE GOT ERROR the error on last line while try to set image to PictureBox, and error said that cannot convert Byte() to image, If any help will so appreciated thanks

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Why are you converting the image to a Base64 string? You don't have to do that to save it it in the database. Databases support blobs (Binary Large Objects) natively, so conversion to Base64 is not necessary. You can't directly use the Base64 string to create a Bitmap object from it. You have to convert the Base64 string back into an array of bytes, then create the Bitmap from that array.

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
        Dave Kreskowiak

        1 Reply Last reply
        0
        • E EngrImad

          Hello; I have image on PictureBox, Cinverted to Base64String string and saved in Database as following: Dim ms As MemoryStream = New MemoryStream Dim bmp As Bitmap = New Bitmap(PictureBox1.Image) bmp.Save(ms, System.Drawing.Imaging.ImageFormat.jpg) Dim My_Photo_Str As String = Convert.ToBase64String(ms.ToArray) myqry = "update MYTABLE set COLUMN1=" & "'" & My_Photo_Str & "'" & "where COLUMN2=" & 50 mycmd = New OleDbCommand(myqry, conn) mycmd.CommandTimeout = 0 mydr = mycmd.ExecuteReader Now that image need to retrieve it or shows on PictureBox, So I used the following:- myqry = "select * from MYTABLE where COLUMN2=" & 50 mycmd = New OleDbCommand(myqry, conn) mycmd.CommandTimeout = 0 mydr = mycmd.ExecuteReader mydr.Read() Dim PIC As Byte() PIC = mydr("COLUMN1") Dim My_Photo = New MemoryStream(PIC) PictureBox1.Image = Bitmap.FromStream(My_Photo) <<<<<-----HERE GOT ERROR the error on last line while try to set image to PictureBox, and error said that cannot convert Byte() to image, If any help will so appreciated thanks

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query. Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^] How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^] Query Parameterization Cheat Sheet | OWASP[^]


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups