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. Convert/Read SQL Image field into pdf

Convert/Read SQL Image field into pdf

Scheduled Pinned Locked Moved Visual Basic
databasehelpcsharpcom
5 Posts 3 Posters 33 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.
  • B Offline
    B Offline
    Benniit
    wrote on last edited by
    #1

    Please I am using VB.net 2019 and Sql 2016 and I have the below codes which inserts the pdf into the database successfully. But retrieving it becomes a problem for me and that is not the case for an image. I want to convert and retrieve this line of code into pdf "Me.Pdf1.Image=Image.fromStream(ms)" which gives an exception because it is not an image but pdf in the database. Please help me. Thank

    OpenFileDialog1.ShowDialog()

    txtPdfPath.Text = OpenFileDialog1.FileName'PDf file uploaded here

    'Code to insert Pdf file and after connection has been established

    Dim ms1 As New MemoryStream

    frmBPA1.Pic1.Image.Save(ms1, frmBPA1.Pic1.Image.RawFormat)

    SqlQuery = "Insert into BPA1 (Pdf1)Values(@Pdf1)"

    com.Parameters.AddWithValue("@Pdf1", ms1.ToArray)

    com.CommandText = SqlQuery

    com.CommandType = CommandType.Text

    SQLCon.Open()

    com.ExecuteNonQuery()

    SQLCon.Close()

    'Code to retrieve the pdf file

    Dim strQuery As String = "Select df1 from BPA1"

    SQLCon1.Open()

    comFile = New SqlCommand(strQuery, SQLCon1)
    comFile.Parameters.AddWithValue("@ReceiptNo", Me.txtReceiptNo.Text.Trim)
    daFile = New SqlDataAdapter(comFile)
    daFile.Fill(taFile)
    If taFile.Rows(0).Item("Pic1") IsNot DBNull.Value Then
    Dim img() As Byte
    img = taFile.Rows(0).Item("Pdf1")
    Dim ms As New MemoryStream(img)
    If img.Length <> "0" Then
    Me.Pic1.Image = Image.FromStream(ms)
    ' Me.txtPdfPath.Text = File.FromStream(ms)
    End If

    L J 2 Replies Last reply
    0
    • B Benniit

      Please I am using VB.net 2019 and Sql 2016 and I have the below codes which inserts the pdf into the database successfully. But retrieving it becomes a problem for me and that is not the case for an image. I want to convert and retrieve this line of code into pdf "Me.Pdf1.Image=Image.fromStream(ms)" which gives an exception because it is not an image but pdf in the database. Please help me. Thank

      OpenFileDialog1.ShowDialog()

      txtPdfPath.Text = OpenFileDialog1.FileName'PDf file uploaded here

      'Code to insert Pdf file and after connection has been established

      Dim ms1 As New MemoryStream

      frmBPA1.Pic1.Image.Save(ms1, frmBPA1.Pic1.Image.RawFormat)

      SqlQuery = "Insert into BPA1 (Pdf1)Values(@Pdf1)"

      com.Parameters.AddWithValue("@Pdf1", ms1.ToArray)

      com.CommandText = SqlQuery

      com.CommandType = CommandType.Text

      SQLCon.Open()

      com.ExecuteNonQuery()

      SQLCon.Close()

      'Code to retrieve the pdf file

      Dim strQuery As String = "Select df1 from BPA1"

      SQLCon1.Open()

      comFile = New SqlCommand(strQuery, SQLCon1)
      comFile.Parameters.AddWithValue("@ReceiptNo", Me.txtReceiptNo.Text.Trim)
      daFile = New SqlDataAdapter(comFile)
      daFile.Fill(taFile)
      If taFile.Rows(0).Item("Pic1") IsNot DBNull.Value Then
      Dim img() As Byte
      img = taFile.Rows(0).Item("Pdf1")
      Dim ms As New MemoryStream(img)
      If img.Length <> "0" Then
      Me.Pic1.Image = Image.FromStream(ms)
      ' Me.txtPdfPath.Text = File.FromStream(ms)
      End If

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

      txtPdfPath.Text = OpenFileDialog1.FileName'PDf file uploaded here

      That is not uploading anything, OpenFileDialog gets filenamse from the uiser, but it does not read them.

      frmBPA1.Pic1.Image.Save(ms1, frmBPA1.Pic1.Image.RawFormat)

      SqlQuery = "Insert into BPA1 (Pdf1)Values(@Pdf1)"

      You create a memory stream object ms1 from some image data. You then try to save something that you have saved in the variable named Pdf1, but you have not placed any data in Pdf1 that I can see. And why are you treating a PDF file as image data?

      B 1 Reply Last reply
      0
      • L Lost User

        txtPdfPath.Text = OpenFileDialog1.FileName'PDf file uploaded here

        That is not uploading anything, OpenFileDialog gets filenamse from the uiser, but it does not read them.

        frmBPA1.Pic1.Image.Save(ms1, frmBPA1.Pic1.Image.RawFormat)

        SqlQuery = "Insert into BPA1 (Pdf1)Values(@Pdf1)"

        You create a memory stream object ms1 from some image data. You then try to save something that you have saved in the variable named Pdf1, but you have not placed any data in Pdf1 that I can see. And why are you treating a PDF file as image data?

        B Offline
        B Offline
        Benniit
        wrote on last edited by
        #3

        Thanks for the reply. Please storing the pdf is not a problem but retrieving it is issue for now. Please how do I retrieve this line Me.Pic1.Image = Image.FromStream(ms) and convert it into pdf. This line of code was used to retrieve image file correctly and I used the same process to store a pdf file but the above code to retrieve is it is the problem. Thanks

        Dim strQuery As String = "Select ReceiptNo,Pic1 from BPA1 where ReceiptNo=@ReceiptNo"
        SQLCon1.Open()
        comFile = New SqlCommand(strQuery, SQLCon1)
        comFile.Parameters.AddWithValue("@ReceiptNo", Me.txtReceiptNo.Text.Trim)
        daFile = New SqlDataAdapter(comFile)
        daFile.Fill(taFile)
        If taFile.Rows(0).Item("Pic1") IsNot DBNull.Value Then
        Dim img() As Byte
        img = taFile.Rows(0).Item("Pic1")
        Dim ms As New MemoryStream(img)
        If img.Length <> "0" Then
        Me.Pic1.Image = Image.FromStream(ms)
        ' Me.txtPdfPath.Text = File.FromStream(ms)
        Else
        Me.Pic1.Image = Nothing
        End If
        End If

        L 1 Reply Last reply
        0
        • B Benniit

          Thanks for the reply. Please storing the pdf is not a problem but retrieving it is issue for now. Please how do I retrieve this line Me.Pic1.Image = Image.FromStream(ms) and convert it into pdf. This line of code was used to retrieve image file correctly and I used the same process to store a pdf file but the above code to retrieve is it is the problem. Thanks

          Dim strQuery As String = "Select ReceiptNo,Pic1 from BPA1 where ReceiptNo=@ReceiptNo"
          SQLCon1.Open()
          comFile = New SqlCommand(strQuery, SQLCon1)
          comFile.Parameters.AddWithValue("@ReceiptNo", Me.txtReceiptNo.Text.Trim)
          daFile = New SqlDataAdapter(comFile)
          daFile.Fill(taFile)
          If taFile.Rows(0).Item("Pic1") IsNot DBNull.Value Then
          Dim img() As Byte
          img = taFile.Rows(0).Item("Pic1")
          Dim ms As New MemoryStream(img)
          If img.Length <> "0" Then
          Me.Pic1.Image = Image.FromStream(ms)
          ' Me.txtPdfPath.Text = File.FromStream(ms)
          Else
          Me.Pic1.Image = Nothing
          End If
          End If

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

          If you cannot retrieve the PDF data then how can you be sure that you have saved it correctly? As I mentioned in my previous response, I cannot see any PDF data being saved.

          1 Reply Last reply
          0
          • B Benniit

            Please I am using VB.net 2019 and Sql 2016 and I have the below codes which inserts the pdf into the database successfully. But retrieving it becomes a problem for me and that is not the case for an image. I want to convert and retrieve this line of code into pdf "Me.Pdf1.Image=Image.fromStream(ms)" which gives an exception because it is not an image but pdf in the database. Please help me. Thank

            OpenFileDialog1.ShowDialog()

            txtPdfPath.Text = OpenFileDialog1.FileName'PDf file uploaded here

            'Code to insert Pdf file and after connection has been established

            Dim ms1 As New MemoryStream

            frmBPA1.Pic1.Image.Save(ms1, frmBPA1.Pic1.Image.RawFormat)

            SqlQuery = "Insert into BPA1 (Pdf1)Values(@Pdf1)"

            com.Parameters.AddWithValue("@Pdf1", ms1.ToArray)

            com.CommandText = SqlQuery

            com.CommandType = CommandType.Text

            SQLCon.Open()

            com.ExecuteNonQuery()

            SQLCon.Close()

            'Code to retrieve the pdf file

            Dim strQuery As String = "Select df1 from BPA1"

            SQLCon1.Open()

            comFile = New SqlCommand(strQuery, SQLCon1)
            comFile.Parameters.AddWithValue("@ReceiptNo", Me.txtReceiptNo.Text.Trim)
            daFile = New SqlDataAdapter(comFile)
            daFile.Fill(taFile)
            If taFile.Rows(0).Item("Pic1") IsNot DBNull.Value Then
            Dim img() As Byte
            img = taFile.Rows(0).Item("Pdf1")
            Dim ms As New MemoryStream(img)
            If img.Length <> "0" Then
            Me.Pic1.Image = Image.FromStream(ms)
            ' Me.txtPdfPath.Text = File.FromStream(ms)
            End If

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            That demonstrates a misunderstanding of what a PDF is. To view an image such as a png you use an image viewer that supports png images. To view a PDF you use a PDF viewer. Those are two different things. Your thought process should be - first section. 1. Find the file (it does not matter that it is a PDF.) 2. Store the bytes in the file as binary/raw in the database. At this point it is not actually a PDF (and certainly not an image), but rather just binary/raw data. Second section 1. Load the binary data 2. Handle the binary data as a PDF file. Such as figuring out how to either use a PDF viewer or deferring to something (like browser). From the above this means that the following code is always going to be wrong. Because it is not an image.

            Image.FromStream(ms)

            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