Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ADO : Displaying Images from an Access Database using plain ASP
 

Displaying Images from an Access Database using plain ASP

by Faisal Khan.Follow Faisal Khan on Twitter

Introduction
This is the 2nd article in a series of articles about inserting binary data ( files, images etc ) into database ( Access Database ) and then displaying that binary data from the database using ASP. I am going to assume here that you have read the 1st article, Uploading Files into an Access Database using plain ASP. Quite a lot of important background information and detail has been covered there, so better give it a reading beforing continuing with this article.

That article described how to upload a binary file via ASP into the database. In this article I am going to talk about the second part, displaying that binary data from the database.

File Uploading with ASP.NET
If you have the privilege of using ASP.NET then you should read these comprehensive tutorials regarding file uploading using built-in ASP.NET server controls:

  1. File uploading to server hard disk.
  2. File uploading to Microsoft Access database.
  3. Uploading images, determining size, width & height and resizing image files.

In the 1st article I deliberately left two files, show.asp and file.asp Those two files are going to be the ones we create in this article.

Show.asp
Open notepad and create a new file. Name it as show.asp. Copy the following code and paste it into the newly created show.asp file and hit the save button :

<%
   ' -- show.asp --
   ' Generates a list of uploaded files
   
   Response.Buffer = True
   
   ' Connection String
   Dim connStr
      connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
         Server.MapPath("FileDB.mdb")
%>
<html>
<head>
   <title>Inserts Images into Database</title>
   <style>
      body, input, td { font-family:verdana,arial; font-size:10pt; }
   </style>
</head>
<body>
   <p align="center">
      <b>Showing Binary Data from the Database</b><br>
      <a href="insert.htm">To insert data click here</a>
   </p>
   
   <table width="700" border="1" align="center">
<%
   ' Recordset Object
   Dim rs
      Set rs = Server.CreateObject("ADODB.Recordset")
      
      ' opening connection
      rs.Open "select [ID],[File Name],[File Size],[Content Type]," & _
		"[First Name],[Last Name],[Profession] from Files " & _
		"order by [ID] desc", connStr, 3, 4

      If Not rs.EOF Then
         Response.Write "<tr><td colspan=""7"" align=""center""><i>"
         Response.Write "No. of records : " & rs.RecordCount
         Response.Write ", Table : Files</i><br>"
         Response.Write "</td></tr>"
   
         While Not rs.EOF
            Response.Write "<tr><td>"
            Response.Write rs("ID") & "</td><td>"
            Response.Write "<a href=""file.asp?ID=" & rs("ID") & """>"
            Response.Write rs("File Name") & "</a></td><td>"
            Response.Write rs("File Size") & "</td><td>"
            Response.Write rs("Content Type") & "</td><td>"
            Response.Write rs("First Name") & "</td><td>"
            Response.Write rs("Last Name") & "</td><td>"
            Response.Write rs("Profession")
            Response.Write "</td></tr>"
            rs.MoveNext
         Wend
      Else
         Response.Write "No Record Found"
      End If
      
      rs.Close
      Set rs = Nothing
%>
   </table>
</body>
</html>

Explanation

<%
' -- show.asp --
' Generates a list of uploaded files
	
Response.Buffer = True

Set the buffering of the show.asp page to True.

' Connection String
Dim connStr
	connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _ 
		Server.MapPath("FileDB.mdb")
%>

Next we declare a variable connStr as our connection string and set it's value to the path of FileDB.mdb database. Note that FileDB.mdb is the database we have been using to store binary data.

show.asp

<%
' Recordset Object
Dim rs
Set rs = Server.CreateObject("ADODB.Recordset")

' SQL Statement
Dim sql_select
sql_select = "SELECT [ID],[File Name],[File Size],[Content Type] "
sql_select = sql_select & "FROM Files ORDER BY [ID] desc"

' opening connection
rs.Open sql_select, connStr, 3, 4

We create a Recordset object and run a SELECT query to retrieve all the records from the Files table.

If Not rs.EOF Then
Response.Write "<tr><td colspan=""4"" align=""center""><i>"
Response.Write "No. of records : " & rs.RecordCount
Response.Write ", Table : Files</i><br>"
Response.Write "</td></tr>"

If the retrieved Recordset is not empty, meaning that there are some records in the Files table, we write the headers of an HTML table to show these records.

While Not rs.EOF
Response.Write "<tr><td>"
Response.Write rs("ID") & "</td><td>"
Response.Write "<a href=""file.asp?ID=" & rs("ID") & """>"
Response.Write rs("File Name") & "</a></td><td>"
Response.Write rs("File Size") & "</td><td>"
Response.Write rs("Content Type")
Response.Write "</td></tr>"

rs.MoveNext
Wend

Using a While...Wend loop we display all the records in the Files table.

Else
Response.Write "No Record Found"
End If

rs.Close
Set rs = Nothing
%>

If the Recordset was empty, meaning there are no records in the Files table we display a "No Record Found" message. Next we close the connection to the database and Set Recordset object to Nothing.

Notice that in the all the records that we displayed we linked each record to file.asp page, file.asp is going to be the actual file to display binary records from the database.

Introduction
This is the 2nd article in a series of articles about inserting binary data ( files, images etc ) into database ( Access Database ) and then displaying that binary data from the database using ASP. I am going to assume here that you have read the 1st article, Uploading Files into an Access Database using plain ASP. Quite a lot of important background information and detail has been covered there, so better give it a reading beforing continuing with this article.

That article described how to upload a binary file via ASP into the database. In this article I am going to talk about the second part, displaying that binary data from the database.

File Uploading with ASP.NET
If you have the privilege of using ASP.NET then you should read these comprehensive tutorials regarding file uploading using built-in ASP.NET server controls:

  1. File uploading to server hard disk.
  2. File uploading to Microsoft Access database.
  3. Uploading images, determining size, width & height and resizing image files.

In the 1st article I deliberately left two files, show.asp and file.asp Those two files are going to be the ones we create in this article.

Show.asp
Open notepad and create a new file. Name it as show.asp. Copy the following code and paste it into the newly created show.asp file and hit the save button :

<%
   ' -- show.asp --
   ' Generates a list of uploaded files
   
   Response.Buffer = True
   
   ' Connection String
   Dim connStr
      connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
         Server.MapPath("FileDB.mdb")
%>
<html>
<head>
   <title>Inserts Images into Database</title>
   <style>
      body, input, td { font-family:verdana,arial; font-size:10pt; }
   </style>
</head>
<body>
   <p align="center">
      <b>Showing Binary Data from the Database</b><br>
      <a href="insert.htm">To insert data click here</a>
   </p>
   
   <table width="700" border="1" align="center">
<%
   ' Recordset Object
   Dim rs
      Set rs = Server.CreateObject("ADODB.Recordset")
      
      ' opening connection
      rs.Open "select [ID],[File Name],[File Size],[Content Type]," & _
		"[First Name],[Last Name],[Profession] from Files " & _
		"order by [ID] desc", connStr, 3, 4

      If Not rs.EOF Then
         Response.Write "<tr><td colspan=""7"" align=""center""><i>"
         Response.Write "No. of records : " & rs.RecordCount
         Response.Write ", Table : Files</i><br>"
         Response.Write "</td></tr>"
   
         While Not rs.EOF
            Response.Write "<tr><td>"
            Response.Write rs("ID") & "</td><td>"
            Response.Write "<a href=""file.asp?ID=" & rs("ID") & """>"
            Response.Write rs("File Name") & "</a></td><td>"
            Response.Write rs("File Size") & "</td><td>"
            Response.Write rs("Content Type") & "</td><td>"
            Response.Write rs("First Name") & "</td><td>"
            Response.Write rs("Last Name") & "</td><td>"
            Response.Write rs("Profession")
            Response.Write "</td></tr>"
            rs.MoveNext
         Wend
      Else
         Response.Write "No Record Found"
      End If
      
      rs.Close
      Set rs = Nothing
%>
   </table>
</body>
</html>

Explanation

<%
' -- show.asp --
' Generates a list of uploaded files
	
Response.Buffer = True

Set the buffering of the show.asp page to True.

' Connection String
Dim connStr
	connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _ 
		Server.MapPath("FileDB.mdb")
%>

Next we declare a variable connStr as our connection string and set it's value to the path of FileDB.mdb database. Note that FileDB.mdb is the database we have been using to store binary data.

show.asp

<%
' Recordset Object
Dim rs
Set rs = Server.CreateObject("ADODB.Recordset")

' SQL Statement
Dim sql_select
sql_select = "SELECT [ID],[File Name],[File Size],[Content Type] "
sql_select = sql_select & "FROM Files ORDER BY [ID] desc"

' opening connection
rs.Open sql_select, connStr, 3, 4

We create a Recordset object and run a SELECT query to retrieve all the records from the Files table.

If Not rs.EOF Then
Response.Write "<tr><td colspan=""4"" align=""center""><i>"
Response.Write "No. of records : " & rs.RecordCount
Response.Write ", Table : Files</i><br>"
Response.Write "</td></tr>"

If the retrieved Recordset is not empty, meaning that there are some records in the Files table, we write the headers of an HTML table to show these records.

While Not rs.EOF
Response.Write "<tr><td>"
Response.Write rs("ID") & "</td><td>"
Response.Write "<a href=""file.asp?ID=" & rs("ID") & """>"
Response.Write rs("File Name") & "</a></td><td>"
Response.Write rs("File Size") & "</td><td>"
Response.Write rs("Content Type")
Response.Write "</td></tr>"

rs.MoveNext
Wend

Using a While...Wend loop we display all the records in the Files table.

Else
Response.Write "No Record Found"
End If

rs.Close
Set rs = Nothing
%>

If the Recordset was empty, meaning there are no records in the Files table we display a "No Record Found" message. Next we close the connection to the database and Set Recordset object to Nothing.

Notice that in the all the records that we displayed we linked each record to file.asp page, file.asp is going to be the actual file to display binary records from the database.


 ( 1 Remaining ) Next

See all comments and questions (post-ad) posted for this tutorial.


Comments/Questions ( Threads: 40, Comments: 69 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. How to open the audio file
  2. Please help
  3. How can i delete the images
  4. having trouble inserting sound files into database ( 3 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  5. Can't view the uploaded files on show.asp
  6. show.asp want to display image rather than hyperlink is it possible?
  7. Cannot upload or display more than 4 megs
  8. multiple database path
  9. help on figuring sql command
  10. Displaying images with file.asp, keep getting a red cross instead of image.
  11. image display problem
  12. Images loose a byte in upload
  13. Response.BinaryWrite PROBLEM !!?? ( 1 Reply )
  14. Updating of Binary Data into the Database
  15. Problem in displaying large files
  16. Some images dont display
  17. Problem with Display/Downloading from the database
  18. About Showing Binary Image from Database ( 1 Reply )
  19. Upload more than one file at a time
  20. Creating HREF Link for image uploaded into DataBAse ( 2 Replies )
  21. How can I copy binary files from database to the Server hard disk?
  22. Images missing bottom part ( 1 Reply )
  23. Displaying Images Inserted Via Access ( 2 Replies )
  24. con.Execute sql_insert
  25. File Retrieval Problem ( 1 Reply )
  26. File.asp not showing image...
  27. Problem retreiving .exe files ( 1 Reply )
  28. problem editing
  29. retrieving images
  30. the display image code is not working
  31. Printing Image
  32. Resizing the images ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  33. Display images
  34. displaying binary images ( 5 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  35. This may be a stupid question ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  36. Having some problem with the BinaryWrite method!!!!!! ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  37. unable to display different kind of files ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  38. How to put html in file.asp ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  39. Displaying file contents ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  40. viewing a doc file shows gibberish ( 3 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.