Click here to Skip to main content
14,837,675 members
Articles / Programming Languages / ASP
Posted 30 May 2004


50 bookmarked

Display Images from Database in ASP

Rate me:
Please Sign up or sign in to vote.
4.44/5 (41 votes)
30 May 20041 min read
This article describes how to display images which are stored in database in either SQL Server or MS Access.


This article describes how to display images which are stored in a database. You can use <IMG> tag to display images stored in the database by calling another ASP page in SRC attribute of <IMG> tag.

For example:

<IMG SRC="ShowPicture.asp?PhotoId=1">

where PhotoId is the ID stored in the database.

Using the code

Following are the steps which needs to be executed:

  1. Create table Users in MS-Access or SQL server.
  2. Create ShowPicture.asp page.
  3. Call this page using <IMG> tag wherever required.

Create table with the following structure:

Table name: Users

  • user_id (AutoNumber)
  • user_name (Text)
  • user_photo (Ole Object - For MS-Access, and Image data type for SQL server).


ShowPicture.asp is used to display images. You need to pass user ID in querystring, and for that user ID, image will be displayed. Following is the code:

'Declare Variables..
   Dim sql
   Dim rs
   Dim conn
   Dim userID,str

  userID = Request("PhotoId")
  If userID = "" Then userID = 0

  'Instantiate Objects
  Set conn = Server.CreateObject("ADODB.Connection")
  Set rs = Server.CreateObject("ADODB.Recordset")

  'Open connection
  Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=" & Server.MapPath("data.mdb")

  'Get the specific image based on the ID passed in a querystring
   str = "SELECT user_photo FROM users where user_id =" & userID
   rs.Open str, conn,3,3
   if rs.eof then 'No records found
   else 'Display the contents
       Response.ContentType = "image/gif"
   end if

  'destroy the variables.
  set rs = Nothing
  set conn = Nothing

Please note that "Response.contentType" will depend on the type of content you would like to display. For example, to display jpg image, following will be the code:

Response.ContentType = "image/jpg"

To use above page for displaying images, following is the example:

<IMG SRC="ShowPicture.asp?PhotoId=1">
<IMG SRC="ShowPicture.asp?PhotoId=2">

Upload Image

Following example shows how to upload images in database by using ASPSmartUpload component. More information about this component can be found here.

Following is the code to upload image:

<FORM METHOD="POST" ACTION="saveFile.asp" 
        ENCTYPE="multipart/form-data" NAME="UploadForm">
   Employee Name : <INPUT TYPE="TEXT" NAME="USERNAME" SIZE="30"><br>
   <center><INPUT TYPE="SUBMIT" VALUE="Upload" 
             id=SUBMIT1 name=SUBMIT1></center>

Code to save the image in the database:

Dim myupload
   'declare variables..
    ' Create Upload Component
    Set myupload = Server.CreateObject("aspSmartUpload.SmartUpload")

    'Create Connection
    Set Conn = Server.CreateObject("ADODB.Connection")
    Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ 
              "Data Source=" & Server.MapPath("data.mdb")
    Set Rs = Server.CreateObject("ADODB.recordset")
    Rs.Open "SELECT user_id,user_name,user_photo FROM users", Conn,3,3

    'Select each file
    For each file In myupload.Files
        If not file.IsMissing Then 'Check for missing file
            'Add the current file in database
            file.FileToField Rs.Fields("user_photo")
            Rs("user_name") = myupload.Form("USERNAME")
            intCount = intCount + 1
        End If
    Response.Write(intCount & " file(s) uploaded.")
    'Destroy the objects...


It is always better to store images in the file system rather than storing in the database. Only image path can be stored in the database. However, storing images in the database depends on requirements and needs.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Om Prakash Pant
Web Developer
India India
No Biography provided

Comments and Discussions

Questiondisplaying images in an access database using asp Pin
Member 1405817414-Dec-18 11:14
MemberMember 1405817414-Dec-18 11:14 
GeneralMy vote of 4 Pin
Manoj Kumar Choubey21-Jan-12 6:06
professionalManoj Kumar Choubey21-Jan-12 6:06 
Question"Error" Invalid class string [modified] Pin
wahed210211-Jun-11 14:53
Memberwahed210211-Jun-11 14:53 
GeneralI want to do this but with ASP reading from an MS Access 2007 Attachment field. Pin
Scottsr5-Apr-11 14:20
MemberScottsr5-Apr-11 14:20 
GeneralMy vote of 5 Pin
Member 204452912-Feb-11 0:40
MemberMember 204452912-Feb-11 0:40 
GeneralMy vote of 4 Pin
codemoreforless8-Aug-10 10:50
Membercodemoreforless8-Aug-10 10:50 
GeneralRbmBinaryImage Pin
rmostafa26-Apr-09 21:49
Memberrmostafa26-Apr-09 21:49 
GeneralI want to store the checkboxs and textboxs values in database - asp Pin
shivasusan29-Oct-08 22:28
Membershivasusan29-Oct-08 22:28 
GeneralI want Code for serch data statewise from all state Pin
influxsam4-May-08 20:30
Memberinfluxsam4-May-08 20:30 
GeneralAccess 2007/SQL Express 2005 and viewing images from ASP Pin
Jstncase16-Apr-08 9:10
MemberJstncase16-Apr-08 9:10 
GeneralRe: Access 2007/SQL Express 2005 and viewing images from ASP Pin
Scottsr5-Apr-11 14:25
MemberScottsr5-Apr-11 14:25 
GeneralThanks Pin
drweb8610-Feb-08 21:54
Memberdrweb8610-Feb-08 21:54 
GeneralHi Can you suggest how to Upload Images in ASP.NET Pin
kodalis29-May-07 5:59
Memberkodalis29-May-07 5:59 
GeneralRe: Hi Can you suggest how to Upload Images in ASP.NET Pin
Bilal Haider1-Aug-07 4:22
professionalBilal Haider1-Aug-07 4:22 
AnswerRe: Hi Can you suggest how to Upload Images in ASP.NET Pin
tfzwgd1-Dec-09 16:44
Membertfzwgd1-Dec-09 16:44 
QuestionNo duplicate files Pin
dlbdennis10-May-07 17:49
Memberdlbdennis10-May-07 17:49 
Questionhow to put flash pgms Pin
By using our site you acknowledge18-Feb-07 19:42
MemberBy using our site you acknowledge18-Feb-07 19:42 
General.jpg images from access database displayed as garble Pin
Member 38120469-Feb-07 3:18
MemberMember 38120469-Feb-07 3:18 
GeneralDisplay all images Pin
brtakaram14-Dec-06 0:24
Memberbrtakaram14-Dec-06 0:24 
GeneralRe: Display all images Pin
brtakaram19-Dec-06 6:48
Memberbrtakaram19-Dec-06 6:48 
GeneralDisplay image along with text Pin
tonyg2smith18-Oct-06 6:02
Membertonyg2smith18-Oct-06 6:02 
GeneralRe: Display image along with text Pin
rameshkumarp20-Feb-07 3:15
Memberrameshkumarp20-Feb-07 3:15 
AnswerRe: Display image along with text Pin
tonyg2smith23-Feb-07 3:42
Membertonyg2smith23-Feb-07 3:42 
QuestionUnable to Upload Image with aspSmartUpload component [modified] Pin
asprajesh16-Oct-06 0:13
Memberasprajesh16-Oct-06 0:13 
Questionsmartupload component ? Pin
coolraazi11-Oct-06 0:04
Membercoolraazi11-Oct-06 0:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.