Click here to Skip to main content
11,577,475 members (59,217 online)
Click here to Skip to main content

Displaying Images from a Database in a GridView

, 9 Dec 2010 CPOL 227.3K 1 50
Rate this:
Please Sign up or sign in to vote.
A simple guide for beginners, explaining one method of displaying images taken from a database on a GridView inside an ASP.NET web page

Introduction

This is a simple web application for users who are new to ASP.NET. This will show how we can retrieve an image from a database and display it in a GridView.

Screenshot - Output

Background

Sometimes we need to upload images to a web application and store it in a database, which store images in binary format. Since that can cause a loss of image quality, we can instead store the image path in the database, and retrieve that image path from it, and display the image from that location in the web page.

In order to do this, first we need to use ADO.NET to connect to the database. The database I use here is SQL Server.

Screenshot - Database

In the database shown, the Profile_Picture field contains the image path. In my case, I have stored all my images in the application directory. You may change this to any other directory, like ~\myfolder\myimage.jpg.

Now our application reads images from its current directory, so if you use a different folder from the current directory, you have to set the application's current directory by calling Directory.SetCurrentDirectory, of the System.IO namespace.

We also need to set some properties in the GridView .

Screenshot - GridView properties

Perform the following actions:

  1. Uncheck the Auto-generate field
  2. Use two BoundFields for UserName and Country
  3. Set the Header Text and DataField (field name in database)
  4. Use ImageField for the image and set DataImageUrlField = ImageFieldName
  5. Click OK

Using the code

The code required for the explanation above is very simple.

public partial class _Default : System.Web.UI.Page 
{
    SqlConnection conn = new SqlConnection();
    protected void Page_Load(object sender, EventArgs e)
    {
        conn.ConnectionString 
            = "Data Source=MyServer; Integrated Security=True; database=test";
        Load_GridData(); // call method below
    }

    void Load_GridData()
    {
        conn.Open();  // open the connection 
        SqlDataAdapter Sqa = new SqlDataAdapter("select * from picture", conn);
        DataSet ds = new DataSet();
        Sqa.Fill(ds);   // fill the dataset 
        GridView1.DataSource = ds; // give data to GridView
        GridView1.DataBind();
        conn.Close();
    }
}

Points of Interest

Using images in your web application is always interesting, and after all, they are from a database!

History

First published 7 Oct 2007
Edited 10 Dec 2010

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Abhijit Jana
Technical Lead
India India
.NET Consultant | Former Microsoft MVP - ASP.NET | CodeProject MVP, Mentor, Insiders| Technology Evangelist | Author | Speaker | Geek | Blogger | Husband

Blog : http://abhijitjana.net
Web Site : http://dailydotnettips.com
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide

You may also be interested in...

Comments and Discussions

 
QuestionExcellent Pin
khaliqhamid20-Jul-14 23:39
memberkhaliqhamid20-Jul-14 23:39 
GeneralMy vote of 1 Pin
R koyee2-Apr-13 15:52
memberR koyee2-Apr-13 15:52 
GeneralMy vote of 5 Pin
Gladys Merma Molina20-Mar-13 19:28
memberGladys Merma Molina20-Mar-13 19:28 
QuestionPage reload Error Pin
Dhanashree Dive20-Jan-13 19:20
memberDhanashree Dive20-Jan-13 19:20 
GeneralRegarding Video Conferencing Pin
Praneshprakash21-Dec-12 18:19
memberPraneshprakash21-Dec-12 18:19 
GeneralMy vote of 5 Pin
sagar pathe18-May-12 2:58
membersagar pathe18-May-12 2:58 
AnswerThanks Abhijit Jana Pin
mohanjune198726-Apr-12 3:28
membermohanjune198726-Apr-12 3:28 
GeneralMy vote of 5 Pin
sau735-Apr-12 10:50
membersau735-Apr-12 10:50 
GeneralMy vote of 5 Pin
pashad11-Mar-12 20:47
memberpashad11-Mar-12 20:47 
GeneralRe: My vote of 5 Pin
amit_jain_online28-Mar-12 1:54
memberamit_jain_online28-Mar-12 1:54 
GeneralMy vote of 3 Pin
Tarika Joshi14-Sep-11 19:35
memberTarika Joshi14-Sep-11 19:35 
GeneralMy vote of 5 Pin
parveen_gkv12-Dec-10 2:00
memberparveen_gkv12-Dec-10 2:00 
GeneralMy vote of 2 Pin
Chona11719-Dec-10 22:52
memberChona11719-Dec-10 22:52 
GeneralRe: My vote of 2 Pin
Abhijit Jana10-Dec-10 0:21
mvpAbhijit Jana10-Dec-10 0:21 
GeneralRe: My vote of 2 Pin
Chona117110-Dec-10 1:08
memberChona117110-Dec-10 1:08 
GeneralRe: My vote of 2 Pin
Noman Aftab11-Dec-10 7:08
memberNoman Aftab11-Dec-10 7:08 
GeneralMy vote of 1 Pin
yunusatm9-Dec-10 19:37
memberyunusatm9-Dec-10 19:37 
GeneralRe: My vote of 1 Pin
Indivara9-Dec-10 19:51
subeditorIndivara9-Dec-10 19:51 
GeneralMy vote of 3 Pin
SChristmas27-Nov-10 4:47
memberSChristmas27-Nov-10 4:47 
GeneralRe: My vote of 3 Pin
Abhijit Jana10-Dec-10 0:23
mvpAbhijit Jana10-Dec-10 0:23 
Generalhelp Pin
hitesh jariwala25-Mar-10 2:23
memberhitesh jariwala25-Mar-10 2:23 
AnswerRe: help Pin
Abhijit Jana27-Aug-10 8:20
mvpAbhijit Jana27-Aug-10 8:20 
GeneralMy vote of 1 Pin
Ali Saeedi2-Feb-10 23:32
memberAli Saeedi2-Feb-10 23:32 
GeneralRe: My vote of 1 Pin
Abhijit Jana27-Aug-10 8:19
mvpAbhijit Jana27-Aug-10 8:19 
QuestionPlease Help? Pin
jessy_j101-Feb-10 6:11
memberjessy_j101-Feb-10 6:11 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 10 Dec 2010
Article Copyright 2007 by Abhijit Jana
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid