Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 65,906 times

Contents

Related Categories

Retrieving Images from a Database - Full Source Code

dave123aspx

Full Source Code

Image Database SQL

CREATE TABLE [dbo].[image] (
  [img_pk] [int] IDENTITY (1, 1) NOT NULL ,
  [img_name] [varchar] (50) NULL ,
  [img_data] [image] NULL ,
  [img_contenttype] [varchar] (50) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[image] WITH NOCHECK ADD
CONSTRAINT [PK_image] PRIMARY KEY NONCLUSTERED
(
  [img_pk]
) ON [PRIMARY]
GO

TestImage.aspx

<html>
<head><title>View Image from the database</title></head>
<body bgcolor=#FFFFFF>
  Here is the image, viewed from a database:<br />
  <img src="viewimage.aspx?img=1" border=1>
</body>
</html>

ViewImage.aspx

<%@ Page language="c#" Src="ViewImage.aspx.cs" Inherits="DBImages.ViewImage" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head></head>
<body>
  <form id="ViewImage" method="post" runat="server"></form>
</body>
</html>

ViewImage.aspx.cs (CodeBehind file)

using System;
using System.Collections;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace DBImages
{
    /// <summary>
    /// Summary description for ViewImage.
    /// </summary>
    public class ViewImage : System.Web.UI.Page
    {
        public ViewImage() { }
        private void Page_Load(object sender, System.EventArgs e)
        {
            //get the image id from the url
            string ImageId = Request.QueryString["img"];
            //build our query statement
            string sqlText = "SELECT img_data, img_contenttype FROM Image WHERE img_pk = " + ImageId;
            SqlConnection connection = new SqlConnection( ConfigurationSettings.AppSettings["DSN"].ToString() );
            SqlCommand command = new SqlCommand( sqlText, connection);
            //open the database and get a datareader
            connection.Open();
            SqlDataReader dr = command.ExecuteReader();
            if ( dr.Read()) //yup we found our image
            {
                Response.ContentType = dr["img_contenttype"].ToString();
                Response.BinaryWrite( (byte[]) dr["img_data"] );
            }
            connection.Close();
        }
    }
}


Comments

  • Re: can't get this to work

    Posted by manoj3939 on 18 Apr 2007

    I am Facing the sam e problem, i am able to store an image into a database as image type but fetching from database in displaying it on a web page is not working.


    Fetching from the da...

  • Posted by tomk10 on 14 Dec 2005

    Whar is Bind? I've only seen DataBinder.Eval used here and your examp,le causes a Compilation Error.

  • can't get this to work

    Posted by Jud101 on 25 Nov 2005

    I've been struggling to get an image to view on my webpage. I had to make a few changes but no way would it work for me. I ended up coping and pasting everything, ran into the error at this point : ...

  • Fix

    Posted by Louie88 on 15 Nov 2005

    Hi there! Try this „quite” simple fix. Thanks Dave, you helped a lot with this images mystery…

    ...

  • Same Problem

    Posted by tadhg88 on 22 Feb 2005

    hey guys i have the exact same problem as ye have and im just wondering if ye managed to find a solution
    thanks in advance
    tadhg88