473,406 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

File Upload to Access Database

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
5 6551
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: weiwei | last post by:
Hi All I want to upload either image or word document into either access or SQL server database so far I am doing some small testings and what I can do is upload the file from first page and...
2
by: David Berry | last post by:
Hi All. I'm looking for any help or sample code that can show me how to make a file import wizard in ASP.NET (VB preferred) like the one that MS Access uses. I'm working on a web site where the...
1
by: igotyourdotnet | last post by:
Ok, here is the issue: I have 2 web pages one does a file upload to a database and the other page does a FTP to a server, both pages use the c:\temp directory. The page that uploads to a database...
4
by: kev | last post by:
Hi folks, I have created a database to store information on equipments. During the first level of registration, there is a form that i need the user to fill up details on the equipment testing....
3
by: Stephan | last post by:
Hi all, I am new to access and I face the following "issue": I would like to create a database, to which users can upload files (=pdf, doc, xls...). The files shall be stored locally on a...
4
by: gpl666666 | last post by:
Does anyone know any free online space which allows user to upload mdb Access database file?
1
by: amritranjan | last post by:
How to retrive image file from MS access database and display this in another JSPpage -------------------------------------------------------------------------------- This is my Jsp code for...
2
by: hotflash | last post by:
Hi All, I found the best pure ASP code to upload a file to either server and/or MS Access Database. It works fine for me however, there is one thing that I don't like and have tried to fix but...
2
by: hotflash | last post by:
Hi Master CroCrew, I found a good PURE ASP that will allow you to upload 10MB file to the server and the file contents such as Network, Author, Title, etc... will insert to MS Access at the same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.