Drag/Drop/Upload Functionallity

W

Webbyz

Hello, I was wondering if there is a way that I can enable my
application to accept a file such as a .pdf that when dropped onto the
document, it will upload the file to my database or a directory.

I am trying to make the app as user friendly as possable and would
just like the users to be able to drop the file into an area of the
screen and it would just accept the document and maybe prompt if there
is an existing file for that record in the database.

Any help would be apperciated.

-Matt
 
W

Webbyz

It seems like the example shows just picture handeling. But this does
not have any way to absorb the file directly into a directory or Db.
 
W

Webbyz

It seems like this function is hanging up where it wants to import the
data with the type Byte.

I keep getting the once I select the file to import and it attempts to
read into the Db. It just doesnt like the type Byte for some reason.

Any other ideas?
 
P

Patrice

Doesn't like that is ? Do you have any error message ? What is the DB you
are using ? What is the size of the file you tried ?
 
W

Webbyz

The size of the file is a .pdf which is about 750kb.

Error as follows.
"An error was thrown by the ADO.Net component in the GetData function.
Error Details: Syntax error (missing operator) in query expression
"System.Byte[]""

Database is a Access 2000 .mdb With three fields.
1.) ID - AutoNumber
2.) Description - Text
3.) File - OLEObject

Here is my code so far.

Imports System.IO
Public Class Form1

Public Shared Sub AddFile(ByVal pdfFilePath As String)

Dim newFile() As Byte = GetFile(pdfFilePath)

UpdateData("INSERT INTO Table1 (Description, File) VALUES('" &
My.Computer.FileSystem.GetFileInfo(pdfFilePath).Name & "'," &
newFile.ToString & ")")

End Sub

Public Shared Function GetFile(ByVal filePath As String) As Byte()
Dim stream As FileStream = New FileStream( _
filePath, FileMode.Open, FileAccess.Read)
Dim reader As BinaryReader = New BinaryReader(stream)

Dim newFile() As Byte = reader.ReadBytes(stream.Length)

reader.Close()
stream.Close()

Return newFile
End Function


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim OFD As New OpenFileDialog
With OFD
.Title = "Select file..."
.Filter = "All Files (*.*)|*.*|PDF Documents (*.pdf)|
*.pdf"
.ShowHelp = False
.ShowReadOnly = False
.ShowDialog()
.Multiselect = False
If File.Exists(.FileName) Then
AddFile(.FileName)
Else
MsgBox("File not found!")
End If
End With
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click
Dim myDataset As DataSet
' Grab all records in this table with ID 1 or whatever number
you want
myDataset = GetData("SELECT * FROM [table1] WHERE [ID]=1",
"myFile")
If myDataset.Tables("myFile").Rows.Count > 0 Then ' found the
File
Label1.Text =
myDataset.Tables("myFile").Rows(0).Item("Description").ToString
' Get a temp file form windows
Dim TempFileName As String =
My.Computer.FileSystem.GetTempFileName
' get the file data and put it into a file or something
Label2.Text =
myDataset.Tables("myFile").Rows(0).Item("File").ToString
Else
MsgBox("No record found!")
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

End Sub
End Class
 
P

Patrice

And what does GetData ? Are you using option strict ? It would seem you use
a byte at an inappropriate place.

The simplest sample I could come with is :
Dim Connection As New OleDbConnection(ConnectionString)
Dim Command As New OleDbCommand("SELECT TOP 1 Blob FROM Table1",
Connection)
Connection.Open()
Dim Reader As OleDb.OleDbDataReader =
Command.ExecuteReader(CommandBehavior.SequentialAccess)
Reader.Read()
Dim Blob(255) As Byte
Reader.GetBytes(0, 0, Blob, 0, 255)
Reader.Close()
Command.Dispose()
Connection.Close()
For i As Integer = 0 To UBound(Blob)
If Blob(i) <> i Then MsgBox("Doesn't match")
Debug.Print(i & "/" & Blob(i))
Next

The final check just allows to see that data are retrieved correctly (I
stored a small buffer with value n at the nth position).

Finally it's likely better to retrieve regular data using a query and to
handle blob data with another query so that you don't incurs the overhead of
processing potentially huge values when this is not really needed...

---
Patrice

Webbyz said:
The size of the file is a .pdf which is about 750kb.

Error as follows.
"An error was thrown by the ADO.Net component in the GetData function.
Error Details: Syntax error (missing operator) in query expression
"System.Byte[]""

Database is a Access 2000 .mdb With three fields.
1.) ID - AutoNumber
2.) Description - Text
3.) File - OLEObject

Here is my code so far.

Imports System.IO
Public Class Form1

Public Shared Sub AddFile(ByVal pdfFilePath As String)

Dim newFile() As Byte = GetFile(pdfFilePath)

UpdateData("INSERT INTO Table1 (Description, File) VALUES('" &
My.Computer.FileSystem.GetFileInfo(pdfFilePath).Name & "'," &
newFile.ToString & ")")

End Sub

Public Shared Function GetFile(ByVal filePath As String) As Byte()
Dim stream As FileStream = New FileStream( _
filePath, FileMode.Open, FileAccess.Read)
Dim reader As BinaryReader = New BinaryReader(stream)

Dim newFile() As Byte = reader.ReadBytes(stream.Length)

reader.Close()
stream.Close()

Return newFile
End Function


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim OFD As New OpenFileDialog
With OFD
.Title = "Select file..."
.Filter = "All Files (*.*)|*.*|PDF Documents (*.pdf)|
*.pdf"
.ShowHelp = False
.ShowReadOnly = False
.ShowDialog()
.Multiselect = False
If File.Exists(.FileName) Then
AddFile(.FileName)
Else
MsgBox("File not found!")
End If
End With
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click
Dim myDataset As DataSet
' Grab all records in this table with ID 1 or whatever number
you want
myDataset = GetData("SELECT * FROM [table1] WHERE [ID]=1",
"myFile")
If myDataset.Tables("myFile").Rows.Count > 0 Then ' found the
File
Label1.Text =
myDataset.Tables("myFile").Rows(0).Item("Description").ToString
' Get a temp file form windows
Dim TempFileName As String =
My.Computer.FileSystem.GetTempFileName
' get the file data and put it into a file or something
Label2.Text =
myDataset.Tables("myFile").Rows(0).Item("File").ToString
Else
MsgBox("No record found!")
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

End Sub
End Class
 
W

Webbyz

Well at this point the GetData is gathering the Data and passing it
into the Db.

Here is the Module for that function.


Module DataAccessModule
Friend Function GetData(ByVal strSQL As String, ByVal TableName As
String) As DataSet
Dim myDataset As DataSet = New DataSet
' Create our connection string
Dim myConnString As OleDbConnection = New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:
\db1.mdb")
' Create our command
Dim myCommand As OleDbCommand = New OleDbCommand(strSQL,
myConnString)
' Create our data adapter
Dim myDataAdapter As OleDbDataAdapter = New OleDbDataAdapter
' Assign the SQL command to the data adapter
myDataAdapter.SelectCommand = myCommand
Try
' Fill the dataset with the data from our command
myDataAdapter.Fill(myDataset, TableName)
' Return the dataset
GetData = myDataset
Catch ex As OleDbException
MsgBox("An error was thrown by the ADO.Net component in
the GetData function. Error Details: " & _
vbCr & ex.Message, MsgBoxStyle.Exclamation, "Error!")
Debug.WriteLine("SQL Statement: " & strSQL)
GetData = Nothing
Finally
' Close the connection and destroy our connection objects
myCommand.Connection.Close()
myDataset = Nothing
myConnString = Nothing
myCommand = Nothing
myDataAdapter = Nothing
End Try
End Function

Friend Function UpdateData(ByVal strSQL As String) As Integer
' Create our connection string
Dim myConnString As OleDbConnection = New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:
\db1.mdb")
' Create our command
Dim myCommand As OleDbCommand = New OleDbCommand(strSQL,
myConnString)
Try
' Open the connection
myCommand.Connection.Open()
' execute the SQL
UpdateData = myCommand.ExecuteNonQuery()
Catch ex As OleDbException
MsgBox("An error was thrown by the ADO.Net component in
the GetData function. Error Details: " & _
vbCr & ex.Message, MsgBoxStyle.Exclamation, "Error!")
Debug.WriteLine("SQL Statement: " & strSQL)
UpdateData = Nothing
Finally
' Close the connection and destroy our connection object
myCommand.Connection.Close()
myConnString = Nothing
myCommand = Nothing
End Try
End Function

End Module
 
P

Patrice

Ok the error message is actually misleading as the method name is not
correct. This is actually in the insertion statement where you use to string
that dumps the type name inside the SQL statement resulting in a SQL syntax
error...

Please post a message if you solve the problem. I'll try to post a sample
later today so that you can go back on better tracks...
 
P

Patrice

Ok so I made oledb samples from the SQLClient samples I discussed earlier in
the thread. It would give us for writing :

Dim BlobIn(255) As Byte
For i As Integer = 0 To UBound(BlobIn)
BlobIn(i) = CByte(i Mod 255)
Next
Dim Connection As New OleDbConnection(ConnectionString)
Dim Command As New OleDbCommand("INSERT INTO Table1(Blob) VALUES
(?)", Connection)
Connection.Open()
Command.Parameters.Add("a", OleDbType.Binary, UBound(BlobIn)).Value
= BlobIn
Command.ExecuteNonQuery()


For reading :
Dim BlobOut(1024) As Byte
Command = New OleDbCommand("SELECT TOP 1 Blob FROM Table1",
Connection) ' Would select using the PK
Dim Reader As OleDbDataReader = Command.ExecuteReader
Reader.Read()

Debug.Print(Reader.GetBytes(0, 0, BlobOut, 0,
BlobOut.Length).ToString) ' Could stream the result in chunks to a file
For i As Integer = 0 To UBound(BlobIn)
If BlobIn(i) <> BlobOut(i) Then MsgBox("Mismatch !")
Next
Connection.Close()

Assuming you have a Table1 table with a Blob field and no records. It alos
test that the fictionous writren blob is the same than the blob we read back
later. From there you should have a starting point allowing to do what you
want...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top