Display saved PDF file from database to PDFVIEWER - mysql

I have successfully saved a pdf file to my database (MySQL) as a longblob and used this code :
opf.Filter = "PDF FILES |*.pdf"
If opf.ShowDialog = Windows.Forms.DialogResult.OK Then
//Display the PDF you want to save in the PDFVIEWER
pdfview.src = opf.FileName
Dim Mystream As New FileStream(opf.FileName, FileMode.Open)
Dim filesize As Long = Mystream.Length
Dim buffer(filesize) As Byte
Mystream.Read(buffer, 0, filesize)
//Save the pdf file to the database
Dim query As String = "insert into sample(pdfnaho)values(#file)"
Dim cmd As New MySqlCommand(query, con)
con.Open()
cmd.Parameters.AddWithValue("#file", buffer)
cmd.ExecuteNonQuery()
MsgBox("SAVED!")
con.Close()
End If
Then I used this code for displaying but It doesn't work and I don't know what to do :
Dim query As String = "select * from sample where id = 1"
Dim cmd As New MySqlCommand(query, con)
con.Open()
Dim dr As MySqlDataReader = cmd.ExecuteReader
While dr.Read
If dr.HasRows Then
pdfview.src = dr.GetString("pdfnaho")
End If
End While
con.Close()
How should I display my saved pdf file from database? Thank You

I can not put this as a comment because I don't have enough reputation.
What PDF viewer are you using?. If it is the Adobe PDF Viewer I am almost sure (from previous research for another project) that you can not stream into it.
This is an example from my previous project, I modified it to fit yours, make sure to edit the query to fit your table and columns names.
You have to ran a query that return only the cell with the file in binary.
Dim query as String = "SELECT file_content FROM sample WHERE ID=1;"
Public Sub OpenPDF()
If File.Exists(Application.StartupPath() & "\temp.file") = True Then
pdfview.src = "blank.pdf"
My.Computer.FileSystem.DeleteFile(Application.StartupPath() & "\temp.file")
End If
Dim cmd As New MySqlCommand(query, con)
Dim Buffer As Byte()
con.open()
Buffer = cmd.ExecuteScalar
con.close()
File.WriteAllBytes(Application.StartupPath() & "\temp.file", Buffer)
pdfview.src = Application.StartupPath() & "\temp.file"
End Sub
With this example, the code will check if there is an existing "temp.file" and delete it, then load nothing on the pdfviewer to clear whatever is displaying.
The the database connection, get the data, store it on a file then display it with the pdfviewer.

Related

How to insert data from XML in MySql?

Actually i'm trying to create a program in VB.NET that get xml files from a folder and set it's data to MySQL.
Each XML file has unique ID for which is created a database and all tables.
In the folder i can have files like "DC_001" "DF_001" "DC_002" and that's mean that the data from the XML file DC_001 have to be placed in database 001 in table DC.
Actually i've made the method which create the database for each unique ID and it's tables but as i'm not very in to VB.NET i need some suggestion on how to put the data inside the XML to each database.
Here is my method where i create the database and tables.
Dim PIVA As New ArrayList
Dim conn As MySqlConnection = New MySqlConnection
conn.ConnectionString = "server=127.0.0.1;user id=root; password=block; database=mysql"
For Each fileName As String In Directory.EnumerateFileSystemEntries("C:\Users\imytyuk\Desktop\test")
Dim file As String = Trim(EstrCampo("_", fileName, 1, 2))
If Not PIVA.Contains(file) Then
PIVA.Add(file)
End If
Next
For Each iva In PIVA
Dim cmd As MySqlCommand = New MySqlCommand("CREATE DATABASE `" & iva & "`", conn)
Try
conn.Open()
Try
cmd.ExecuteNonQuery()
Catch ex As Exception
End Try
conn.Close()
CreaTB(iva)
Catch
End Try
Next
While here is like a XML file can be structured
UPDATE:
I'm trying to use the following method to insert the data from XML but i get ""Column count doesn't match value count at row 1"}" as Exception.
Here is the code:
Sub AddXML()
For Each fileName As String In Directory.EnumerateFileSystemEntries("C:\Users\imytyuk\Desktop\test")
databaseFilePut(fileName, "datacollect", Trim(EstrCampo("_", fileName, 1, 2)))
File.Delete(fileName)
Next
End Sub
Public Shared Sub databaseFilePut(ByVal varFilePath As String, ByVal table As String, ByVal db As String)
Dim file() As Byte
Dim stream = New FileStream(varFilePath, FileMode.Open, FileAccess.Read)
Dim reader = New BinaryReader(stream)
file = reader.ReadBytes(CType(stream.Length, Integer))
Dim conn As MySqlConnection = New MySqlConnection
conn.ConnectionString = "server=127.0.0.1;user id=root; password=block; database=" & db
Dim cmd As MySqlCommand = New MySqlCommand("INSERT INTO " & table & " Values(#File)", conn)
Try
conn.Open()
cmd.Parameters.Add("#File", MySqlDbType.VarBinary, file.Length).Value = file
cmd.ExecuteNonQuery()
Catch ex As Exception
End Try
End Sub

Inserting PDF files into MYSQL with BLOB using VB.NET

I have been trying to figure out how to submit PDF files into a MYSQL database via my VB.NET project, so far I have only been able to figure out how to insert image files into the MYSQL but I'm not sure how to translate this code to support PDF files rather than images.
The code below shows how to browse an image and display it.
Try
Dim OFD As FileDialog = New OpenFileDialog()
OFD.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif"
If OFD.ShowDialog() = DialogResult.OK Then
imgpath = OFD.FileName
PictureBox1.ImageLocation = imgpath
End If
OFD = Nothing
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
The code below this shows the insertion of the image files into the MYSQL database once an image has been chosen.
Try
Dim mstream As New System.IO.MemoryStream()
PictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
arrImage = mstream.GetBuffer()
Dim FileSize As UInt32
FileSize = mstream.Length
mstream.Close()
conn.ConnectionString = Myconnection
conn.Open()
sql = "insert into studentsubmissions(content, submissionid, studentnumber, time, date, deadline, title, work, modulename) VALUES (#filestuff, #subid, #stunumber, #subtime, #subdate, #workdeadline, #stutitle, #stuwork, #workmodulename)"
cmd.Connection = conn
cmd.CommandText = sql
cmd.Parameters.AddWithValue("#filestuff", arrImage)
cmd.Parameters.AddWithValue("#subid", idbox.Text)
cmd.Parameters.AddWithValue("#stunumber", usernameconstant.Text)
cmd.Parameters.AddWithValue("#subtime", todaystime.Text)
cmd.Parameters.AddWithValue("#subdate", todaysdate.Text)
cmd.Parameters.AddWithValue("#workdeadline", deadlinesubmission.Text)
cmd.Parameters.AddWithValue("#stutitle", titlesubmission.Text)
cmd.Parameters.AddWithValue("#stuwork", workload.Text)
cmd.Parameters.AddWithValue("#workmodulename", modulename.Text)
cmd.ExecuteNonQuery()
MessageBox.Show("Created")
cmd.Parameters.Clear()
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Below this is the method of displaying a PDF file on an Adobe PDF Reader in my project which is what I eventually want replacing the first set of code in displaying the documents and making the second set of code shown to support this.
Dim opf As New OpenFileDialog
opf.Filter = "PDF File | *.pdf"
If opf.ShowDialog = DialogResult.OK Then
AxAcroPDF1.src = opf.FileName
text_file.Text = opf.SafeFileName
End If

Data adds up but does not display in single compile

I am using service-based database (.mdf file) to add name and the photo to my database in VS 2010. After debugging and adding the data it adds up into the .mdf but while trying to retrieve it, it doesn't show. It shows only after next debugging. I am really stuck in this for 2 days please help me.
Thank you in advance.
Connection string I am currently using :
Dim path As String = (Microsoft.VisualBasic.Left(Application.StartupPath, Len(Application.StartupPath) - 9))
Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=" & path & "Database1.mdf;Integrated Security=True;User Instance=True")
This is the code I am using to save the image and name :
Dim cmd As New SqlCommand("INSERT INTO Information VALUES(#name,#photo)", con)
cmd.Parameters.AddWithValue("#name", TextBox1.Text)
Dim ms As New MemoryStream()
PictureBox1.BackgroundImage.Save(ms, PictureBox1.BackgroundImage.RawFormat)
Dim data As Byte() = ms.GetBuffer()
Dim p As New SqlParameter("#photo", SqlDbType.Image)
p.Value = data
cmd.Parameters.Add(p)
cmd.ExecuteNonQuery()
MessageBox.Show("Name & Image has been saved", "Save", MessageBoxButtons.OK)
And this is the code I am using to display data in the datagridview
Me.InformationTableAdapter.Fill(Me.Database1DataSet.Information

Retrieving Image from Mysql using vb.net

I have stored Image as blob in mysql,And now I want ti display this image in picturebox in Vb,
I used Following code..
Dim con As New MySqlConnection
Dim str As String = "host=localhost;userid=root;password=;database=new;"
Dim qry As String = "select pic from img where id='1'"
Dim cmd As MySqlCommand
Dim reader As MySqlDataReader
Dim dt() As Byte
con.ConnectionString = str
Try
con.Open()
MsgBox(con.State)
cmd = New MySqlCommand(qry, con)
reader = cmd.ExecuteReader()
reader.Read()
dt = reader.Item("pic")
Dim mstream As New System.IO.MemoryStream
PictureBox1.Image = Image.FromStream(mstream)
reader.Close()
Catch ex As Exception
MsgBox(ErrorToString)
End Try
It shows connection state=1
but gives error Saying"Parameter is not valid,Whats wrong?"
You're creating a MemoryStream but not writing any data to it. The image data isn't going to magically come out of the stream; you have to put it there first. Have you done any reading on the MemoryStream class? If you have then you know that it has a constructor that allows you to pass in a Byte array. You have a Byte array containing the image data so that's a match made in heaven.
Also, make sure that you dispose the MemoryStream after using it.

querying a MySQL table from VB.NET

I made my software in vb.net and connected it with MySQL databse and using phpmyadmin I have create a table update and a column version.
In the version column I have inserted the link of version.txt
I want that my update library which is updateVB will get the link of version.txt from the database from that table....
Updatevb1.checkforupdate("Text file where your version is stated (URL)",
"Current Version",
"URL of executable updater (SFX Archive),
"Username for FTP", "Password for FTP",
showUI As Boolean)
I want to get every of these information like: Text file version URL, current version, URL of executable update etc.
How can this be done?
conn = New MySqlConnection(ServerString)
Try
conn.Open()
Dim sqlquery As String = "SELECT FROM updater"
Dim data As MySqlDataReader
Dim adapter As New MySqlDataAdapter
Dim command As New MySqlCommand
command.CommandText = sqlquery
command.Connection = conn
adapter.SelectCommand = command
data = command.ExecuteReader
While data.Read()
If data.HasRows() Then
Dim vlink As String = data(1).ToString
Dim dlink As String = data(2).ToString
Dim ftpu As String = data(3).ToString
Dim ftpp As String = data(4).ToString
End If
End While
UpdateVB1.checkforupdate("vlink", "0.0.9", "dlink", "ftpu", "ftpp", showUI:=True)
data.Close()
conn.Close()
The first problem is that your SQL statement isn't specifying any columns.
Supply the list of columns that you want to consume.
Dim sqlquery As String = "SELECT vlink, dlink, ftpu, dtpp FROM updater"