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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. HELP FOR saving image to sql database error

HELP FOR saving image to sql database error

Scheduled Pinned Locked Moved Visual Basic
helpdatabaseannouncement
4 Posts 2 Posters 0 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.
  • L Offline
    L Offline
    Lisana
    wrote on last edited by
    #1

    I'm trying to save the image to sql database, there is error "object must implement Iconvertible" when I run it. Here is my code: (image is the location + the name of the image) Try Dim strFn As String = image Dim fiImage As FileInfo = New FileInfo(strFn) Me.m_lImageFileLength = fiImage.Length Dim fs As FileStream = New FileStream(strFn, FileMode.Open, FileAccess.Read, FileShare.Read) m_barrImg = New Byte(Convert.ToInt32(Me.m_lImageFileLength)) {} Dim iBytesRead As Integer = fs.Read(m_barrImg, 0, Convert.ToInt32(Me.m_lImageFileLength)) fs.Close() Catch ex As Exception MessageBox.Show(ex.Message) End Try Try Me.SqlConnection1.Open() If SqlCommand1.Parameters.Count = 0 Then Me.SqlCommand1.CommandText = "UPDATE HRRecordIndexData SET pdf_location = @Picture WHERE entity_id = @ID" Me.SqlCommand1.Parameters.Add("@Picture", System.Data.SqlDbType.Image) Me.SqlCommand1.Parameters.Add("@ID", System.Data.SqlDbType.Int, 4) End If Me.SqlCommand1.Parameters("@Picture").Value = Me.m_barrImg Me.SqlCommand1.Parameters("@ID").Value = Entity Me.SqlCommand1.ExecuteNonQuery() Catch ex As Exception MessageBox.Show(ex.Message) Finally Me.SqlConnection1.Close() End Try If there is everyone knows what it needs to fix at my code, I would very appreciate it .. thanks!

    Lisa

    L 1 Reply Last reply
    0
    • L Lisana

      I'm trying to save the image to sql database, there is error "object must implement Iconvertible" when I run it. Here is my code: (image is the location + the name of the image) Try Dim strFn As String = image Dim fiImage As FileInfo = New FileInfo(strFn) Me.m_lImageFileLength = fiImage.Length Dim fs As FileStream = New FileStream(strFn, FileMode.Open, FileAccess.Read, FileShare.Read) m_barrImg = New Byte(Convert.ToInt32(Me.m_lImageFileLength)) {} Dim iBytesRead As Integer = fs.Read(m_barrImg, 0, Convert.ToInt32(Me.m_lImageFileLength)) fs.Close() Catch ex As Exception MessageBox.Show(ex.Message) End Try Try Me.SqlConnection1.Open() If SqlCommand1.Parameters.Count = 0 Then Me.SqlCommand1.CommandText = "UPDATE HRRecordIndexData SET pdf_location = @Picture WHERE entity_id = @ID" Me.SqlCommand1.Parameters.Add("@Picture", System.Data.SqlDbType.Image) Me.SqlCommand1.Parameters.Add("@ID", System.Data.SqlDbType.Int, 4) End If Me.SqlCommand1.Parameters("@Picture").Value = Me.m_barrImg Me.SqlCommand1.Parameters("@ID").Value = Entity Me.SqlCommand1.ExecuteNonQuery() Catch ex As Exception MessageBox.Show(ex.Message) Finally Me.SqlConnection1.Close() End Try If there is everyone knows what it needs to fix at my code, I would very appreciate it .. thanks!

      Lisa

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Lisa, are you sure the field "pdf_location" has type "image"? Regards Ray

      L 1 Reply Last reply
      0
      • L Lost User

        Lisa, are you sure the field "pdf_location" has type "image"? Regards Ray

        L Offline
        L Offline
        Lisana
        wrote on last edited by
        #3

        Hello, Ray, I created another field called thumbnail and the datatype is image, and I run it again, the result is the same, it still gave me the same error. thanks!

        Lisa

        L 1 Reply Last reply
        0
        • L Lisana

          Hello, Ray, I created another field called thumbnail and the datatype is image, and I run it again, the result is the same, it still gave me the same error. thanks!

          Lisa

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Hi, I have similar code and it works. The difference is only in parameter type definition : System.Data.SqlDbType.VarBinary , not image. Here is my code: fv =me.openFileDialog1.FileName Dim fs As New FileStream(fv, FileMode.OpenOrCreate, FileAccess.Read) Dim MyData(fs.Length) As Byte fs.Read(MyData, 0, CInt(fs.Length)) fs.Close() kom.Connection=conn kom.CommandText="INSERT INTO PDF (DATA,F_NAME) VALUES (@duom,@failas)" dim param1 as New SqlParameter("@duom", System.Data.SqlDbType.VarBinary) param1.Value=MyData kom.Parameters.Add(param1) dim param2 as New SqlParameter("@failas", System.Data.SqlDbType.VarChar ) param2.Value=fv kom.Parameters.Add(param2) conn.Open dim res as Integer=kom.ExecuteNonQuery()

          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