By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,550 Members | 2,725 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,550 IT Pros & Developers. It's quick & easy.

File Upload to Access Database

P: n/a
Dear Experts,

I currently trying to use the FileUpload control from asp.net 2.0 to upload
files. The uploading of the file I would like to store it in the Access
Database. Unfortunately, I've no idea how I can do that. Can anyone provide
me some solution by writing the code in vb.net?

Many thanks in advance.

Regards,
SB
Jan 24 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Quick Example:

1. Database contains just one table with following structure
FileId Autonumber Primary Key
FileName Text 255
FileContent OLE Object

2. UploadFile.aspx page HTML content
-- BEGIN CODE --
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="UploadFile.aspx.vb"
Inherits="UploadFile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="fupFile" runat="server" /><br />
<br />
<asp:Button ID="btnUpload" runat="server" Text="Upload" /> </div>
</form>
</body>
</html>
-- END CODE --
3. UploadFile.aspx.vb code behing file
-- BEGIN CODE --
Imports System.Data
Imports System.Data.OleDb

Partial Class UploadFile
Inherits System.Web.UI.Page

Private Const ConnectionString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\TEMP\Files.mdb;"

Private Sub InsertFile(ByVal strFileName As String, ByVal abyContent As
Byte())

Dim strQuery As String = "INSERT INTO Files (FileName, FileContent)
VALUES (?, ?)"
Dim oConnection As New OleDbConnection(ConnectionString)
Dim oCommand As New OleDbCommand(strQuery, oConnection)
Dim oParameter As OleDbParameter = Nothing

oParameter = New OleDbParameter("?", OleDbType.VarChar)
oParameter.Value = strFileName
oParameter.Direction = ParameterDirection.Input
oCommand.Parameters.Add(oParameter)

oParameter = New OleDbParameter("?", OleDbType.VarBinary)
oParameter.Value = abyContent
oParameter.Direction = ParameterDirection.Input
oCommand.Parameters.Add(oParameter)

oCommand.CommandTimeout = 120
oCommand.CommandType = CommandType.Text

Try

oConnection.Open()
oCommand.ExecuteNonQuery()

Catch ex As Exception
Throw ex
Finally
If oConnection.State <> ConnectionState.Closed Then
oConnection.Close()
End If
End Try

End Sub

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnUpload.Click

If Not fupFile.FileContent Is Nothing Then
' perform validation logic before updating the database - i.e.
if file size is
' correct, etc.
With fupFile.FileContent
' allocate buffer for file data
Dim abyContent(CType(.Length, Integer)) As Byte
' copy data to buffer
.Read(abyContent, 0, .Length)

' insert file data to the database
Me.InsertFile(fupFile.FileName, abyContent)
End With
End If

End Sub

End Class
-- END CODE --

Hope this helps!
--
Milosz Skalecki
MCP, MCAD
"Seok Bee" wrote:
Dear Experts,

I currently trying to use the FileUpload control from asp.net 2.0 to upload
files. The uploading of the file I would like to store it in the Access
Database. Unfortunately, I've no idea how I can do that. Can anyone provide
me some solution by writing the code in vb.net?

Many thanks in advance.

Regards,
SB

Jan 24 '06 #2

P: n/a
Thanks alot for the code below. It really help me alot.
Another question is, can the code below support all kinds of file type to be
uploaded into the database? Which means can I open the file directly from the
database? The possible file types that I may uplooad are documents, excel,
and may be image

Thanks again once more..

"Milosz Skalecki" wrote:
Quick Example:

1. Database contains just one table with following structure
FileId Autonumber Primary Key
FileName Text 255
FileContent OLE Object

2. UploadFile.aspx page HTML content
-- BEGIN CODE --
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="UploadFile.aspx.vb"
Inherits="UploadFile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="fupFile" runat="server" /><br />
<br />
<asp:Button ID="btnUpload" runat="server" Text="Upload" /> </div>
</form>
</body>
</html>
-- END CODE --
3. UploadFile.aspx.vb code behing file
-- BEGIN CODE --
Imports System.Data
Imports System.Data.OleDb

Partial Class UploadFile
Inherits System.Web.UI.Page

Private Const ConnectionString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\TEMP\Files.mdb;"

Private Sub InsertFile(ByVal strFileName As String, ByVal abyContent As
Byte())

Dim strQuery As String = "INSERT INTO Files (FileName, FileContent)
VALUES (?, ?)"
Dim oConnection As New OleDbConnection(ConnectionString)
Dim oCommand As New OleDbCommand(strQuery, oConnection)
Dim oParameter As OleDbParameter = Nothing

oParameter = New OleDbParameter("?", OleDbType.VarChar)
oParameter.Value = strFileName
oParameter.Direction = ParameterDirection.Input
oCommand.Parameters.Add(oParameter)

oParameter = New OleDbParameter("?", OleDbType.VarBinary)
oParameter.Value = abyContent
oParameter.Direction = ParameterDirection.Input
oCommand.Parameters.Add(oParameter)

oCommand.CommandTimeout = 120
oCommand.CommandType = CommandType.Text

Try

oConnection.Open()
oCommand.ExecuteNonQuery()

Catch ex As Exception
Throw ex
Finally
If oConnection.State <> ConnectionState.Closed Then
oConnection.Close()
End If
End Try

End Sub

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnUpload.Click

If Not fupFile.FileContent Is Nothing Then
' perform validation logic before updating the database - i.e.
if file size is
' correct, etc.
With fupFile.FileContent
' allocate buffer for file data
Dim abyContent(CType(.Length, Integer)) As Byte
' copy data to buffer
.Read(abyContent, 0, .Length)

' insert file data to the database
Me.InsertFile(fupFile.FileName, abyContent)
End With
End If

End Sub

End Class
-- END CODE --

Hope this helps!
--
Milosz Skalecki
MCP, MCAD
"Seok Bee" wrote:
Dear Experts,

I currently trying to use the FileUpload control from asp.net 2.0 to upload
files. The uploading of the file I would like to store it in the Access
Database. Unfortunately, I've no idea how I can do that. Can anyone provide
me some solution by writing the code in vb.net?

Many thanks in advance.

Regards,
SB

Jan 25 '06 #3

P: n/a
Yep, all file types are supported.
--
Milosz Skalecki
MCP, MCAD
"Seok Bee" wrote:
Thanks alot for the code below. It really help me alot.
Another question is, can the code below support all kinds of file type to be
uploaded into the database? Which means can I open the file directly from the
database? The possible file types that I may uplooad are documents, excel,
and may be image

Thanks again once more..

"Milosz Skalecki" wrote:
Quick Example:

1. Database contains just one table with following structure
FileId Autonumber Primary Key
FileName Text 255
FileContent OLE Object

2. UploadFile.aspx page HTML content
-- BEGIN CODE --
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="UploadFile.aspx.vb"
Inherits="UploadFile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="fupFile" runat="server" /><br />
<br />
<asp:Button ID="btnUpload" runat="server" Text="Upload" /> </div>
</form>
</body>
</html>
-- END CODE --
3. UploadFile.aspx.vb code behing file
-- BEGIN CODE --
Imports System.Data
Imports System.Data.OleDb

Partial Class UploadFile
Inherits System.Web.UI.Page

Private Const ConnectionString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\TEMP\Files.mdb;"

Private Sub InsertFile(ByVal strFileName As String, ByVal abyContent As
Byte())

Dim strQuery As String = "INSERT INTO Files (FileName, FileContent)
VALUES (?, ?)"
Dim oConnection As New OleDbConnection(ConnectionString)
Dim oCommand As New OleDbCommand(strQuery, oConnection)
Dim oParameter As OleDbParameter = Nothing

oParameter = New OleDbParameter("?", OleDbType.VarChar)
oParameter.Value = strFileName
oParameter.Direction = ParameterDirection.Input
oCommand.Parameters.Add(oParameter)

oParameter = New OleDbParameter("?", OleDbType.VarBinary)
oParameter.Value = abyContent
oParameter.Direction = ParameterDirection.Input
oCommand.Parameters.Add(oParameter)

oCommand.CommandTimeout = 120
oCommand.CommandType = CommandType.Text

Try

oConnection.Open()
oCommand.ExecuteNonQuery()

Catch ex As Exception
Throw ex
Finally
If oConnection.State <> ConnectionState.Closed Then
oConnection.Close()
End If
End Try

End Sub

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnUpload.Click

If Not fupFile.FileContent Is Nothing Then
' perform validation logic before updating the database - i.e.
if file size is
' correct, etc.
With fupFile.FileContent
' allocate buffer for file data
Dim abyContent(CType(.Length, Integer)) As Byte
' copy data to buffer
.Read(abyContent, 0, .Length)

' insert file data to the database
Me.InsertFile(fupFile.FileName, abyContent)
End With
End If

End Sub

End Class
-- END CODE --

Hope this helps!
--
Milosz Skalecki
MCP, MCAD
"Seok Bee" wrote:
Dear Experts,

I currently trying to use the FileUpload control from asp.net 2.0 to upload
files. The uploading of the file I would like to store it in the Access
Database. Unfortunately, I've no idea how I can do that. Can anyone provide
me some solution by writing the code in vb.net?

Many thanks in advance.

Regards,
SB

Jan 25 '06 #4

P: n/a
I managed to upload the file into the database using the code you provide.
Thanks for that. Also, i tried to open the file from Access I got the
following error message:

a problem occurred while microsoft access was communication with the OLE
server or activeX control

I have disabled my antivirus, but still the same error occurs.

Secondly, how can I view the file by programmically creating the link in my
webpage so that I can view the file? Or what is the best solution for me to
be able to view and download the file from the Access Database.

Thanks.

"Milosz Skalecki" wrote:
Yep, all file types are supported.
--
Milosz Skalecki
MCP, MCAD
"Seok Bee" wrote:
Thanks alot for the code below. It really help me alot.
Another question is, can the code below support all kinds of file type to be
uploaded into the database? Which means can I open the file directly from the
database? The possible file types that I may uplooad are documents, excel,
and may be image

Thanks again once more..

"Milosz Skalecki" wrote:
Quick Example:

1. Database contains just one table with following structure
FileId Autonumber Primary Key
FileName Text 255
FileContent OLE Object

2. UploadFile.aspx page HTML content
-- BEGIN CODE --
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="UploadFile.aspx.vb"
Inherits="UploadFile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="fupFile" runat="server" /><br />
<br />
<asp:Button ID="btnUpload" runat="server" Text="Upload" /> </div>
</form>
</body>
</html>
-- END CODE --
3. UploadFile.aspx.vb code behing file
-- BEGIN CODE --
Imports System.Data
Imports System.Data.OleDb

Partial Class UploadFile
Inherits System.Web.UI.Page

Private Const ConnectionString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\TEMP\Files.mdb;"

Private Sub InsertFile(ByVal strFileName As String, ByVal abyContent As
Byte())

Dim strQuery As String = "INSERT INTO Files (FileName, FileContent)
VALUES (?, ?)"
Dim oConnection As New OleDbConnection(ConnectionString)
Dim oCommand As New OleDbCommand(strQuery, oConnection)
Dim oParameter As OleDbParameter = Nothing

oParameter = New OleDbParameter("?", OleDbType.VarChar)
oParameter.Value = strFileName
oParameter.Direction = ParameterDirection.Input
oCommand.Parameters.Add(oParameter)

oParameter = New OleDbParameter("?", OleDbType.VarBinary)
oParameter.Value = abyContent
oParameter.Direction = ParameterDirection.Input
oCommand.Parameters.Add(oParameter)

oCommand.CommandTimeout = 120
oCommand.CommandType = CommandType.Text

Try

oConnection.Open()
oCommand.ExecuteNonQuery()

Catch ex As Exception
Throw ex
Finally
If oConnection.State <> ConnectionState.Closed Then
oConnection.Close()
End If
End Try

End Sub

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnUpload.Click

If Not fupFile.FileContent Is Nothing Then
' perform validation logic before updating the database - i.e.
if file size is
' correct, etc.
With fupFile.FileContent
' allocate buffer for file data
Dim abyContent(CType(.Length, Integer)) As Byte
' copy data to buffer
.Read(abyContent, 0, .Length)

' insert file data to the database
Me.InsertFile(fupFile.FileName, abyContent)
End With
End If

End Sub

End Class
-- END CODE --

Hope this helps!
--
Milosz Skalecki
MCP, MCAD
"Seok Bee" wrote:

> Dear Experts,
>
> I currently trying to use the FileUpload control from asp.net 2.0 to upload
> files. The uploading of the file I would like to store it in the Access
> Database. Unfortunately, I've no idea how I can do that. Can anyone provide
> me some solution by writing the code in vb.net?
>
> Many thanks in advance.
>
> Regards,
> SB

Jan 25 '06 #5

P: n/a
See my rescent reply in post "images from SQL to gridview" (example i
attached to that post was for SQL server). You can amend this example to
support different file types using MIME types
Response.AddHeader("Content-Disposition", "filename=" + YourFileName)
response.ContentType = "application/msword" // it's an example of the MIME
type

Here you can find a list of known MIME types supported by IE
http://msdn.microsoft.com/library/de...appendix_a.asp

Hope this helps

--
Milosz Skalecki
MCP, MCAD
"Seok Bee" wrote:
I managed to upload the file into the database using the code you provide.
Thanks for that. Also, i tried to open the file from Access I got the
following error message:

a problem occurred while microsoft access was communication with the OLE
server or activeX control

I have disabled my antivirus, but still the same error occurs.

Secondly, how can I view the file by programmically creating the link in my
webpage so that I can view the file? Or what is the best solution for me to
be able to view and download the file from the Access Database.

Thanks.

"Milosz Skalecki" wrote:
Yep, all file types are supported.
--
Milosz Skalecki
MCP, MCAD
"Seok Bee" wrote:
Thanks alot for the code below. It really help me alot.
Another question is, can the code below support all kinds of file type to be
uploaded into the database? Which means can I open the file directly from the
database? The possible file types that I may uplooad are documents, excel,
and may be image

Thanks again once more..

"Milosz Skalecki" wrote:

> Quick Example:
>
> 1. Database contains just one table with following structure
> FileId Autonumber Primary Key
> FileName Text 255
> FileContent OLE Object
>
> 2. UploadFile.aspx page HTML content
> -- BEGIN CODE --
> <%@ Page Language="VB" AutoEventWireup="false" CodeFile="UploadFile.aspx.vb"
> Inherits="UploadFile" %>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" >
> <head runat="server">
> <title>Untitled Page</title>
> </head>
> <body>
> <form id="form1" runat="server">
> <div>
> <asp:FileUpload ID="fupFile" runat="server" /><br />
> <br />
> <asp:Button ID="btnUpload" runat="server" Text="Upload" /> </div>
> </form>
> </body>
> </html>
> -- END CODE --
> 3. UploadFile.aspx.vb code behing file
> -- BEGIN CODE --
> Imports System.Data
> Imports System.Data.OleDb
>
> Partial Class UploadFile
> Inherits System.Web.UI.Page
>
> Private Const ConnectionString As String = _
> "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\TEMP\Files.mdb;"
>
> Private Sub InsertFile(ByVal strFileName As String, ByVal abyContent As
> Byte())
>
> Dim strQuery As String = "INSERT INTO Files (FileName, FileContent)
> VALUES (?, ?)"
> Dim oConnection As New OleDbConnection(ConnectionString)
> Dim oCommand As New OleDbCommand(strQuery, oConnection)
> Dim oParameter As OleDbParameter = Nothing
>
> oParameter = New OleDbParameter("?", OleDbType.VarChar)
> oParameter.Value = strFileName
> oParameter.Direction = ParameterDirection.Input
> oCommand.Parameters.Add(oParameter)
>
> oParameter = New OleDbParameter("?", OleDbType.VarBinary)
> oParameter.Value = abyContent
> oParameter.Direction = ParameterDirection.Input
> oCommand.Parameters.Add(oParameter)
>
> oCommand.CommandTimeout = 120
> oCommand.CommandType = CommandType.Text
>
> Try
>
> oConnection.Open()
> oCommand.ExecuteNonQuery()
>
> Catch ex As Exception
> Throw ex
> Finally
> If oConnection.State <> ConnectionState.Closed Then
> oConnection.Close()
> End If
> End Try
>
> End Sub
>
> Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles btnUpload.Click
>
> If Not fupFile.FileContent Is Nothing Then
> ' perform validation logic before updating the database - i.e.
> if file size is
> ' correct, etc.
> With fupFile.FileContent
> ' allocate buffer for file data
> Dim abyContent(CType(.Length, Integer)) As Byte
> ' copy data to buffer
> .Read(abyContent, 0, .Length)
>
> ' insert file data to the database
> Me.InsertFile(fupFile.FileName, abyContent)
> End With
> End If
>
> End Sub
>
> End Class
> -- END CODE --
>
> Hope this helps!
> --
> Milosz Skalecki
> MCP, MCAD
>
>
> "Seok Bee" wrote:
>
> > Dear Experts,
> >
> > I currently trying to use the FileUpload control from asp.net 2.0 to upload
> > files. The uploading of the file I would like to store it in the Access
> > Database. Unfortunately, I've no idea how I can do that. Can anyone provide
> > me some solution by writing the code in vb.net?
> >
> > Many thanks in advance.
> >
> > Regards,
> > SB

Jan 25 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.