Click here to Skip to main content
15,860,859 members
Articles / Web Development / ASP.NET

Displaying Images from a Database in a GridView

Rate me:
Please Sign up or sign in to vote.
3.17/5 (50 votes)
9 Dec 2010CPOL1 min read 394.5K   1   53   45
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 published7 Oct 2007
Edited10 Dec 2010

License

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


Written By
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

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 1500282325-Nov-20 4:50
Member 1500282325-Nov-20 4:50 
GeneralHelpful , thanks Pin
EmanuelPirovano26-Mar-17 23:48
EmanuelPirovano26-Mar-17 23:48 
QuestionExcellent Pin
khaliqhamid20-Jul-14 23:39
khaliqhamid20-Jul-14 23:39 
GeneralMy vote of 1 Pin
R koyee2-Apr-13 15:52
R koyee2-Apr-13 15:52 
GeneralRe: My vote of 1 Pin
Member 1500282326-Nov-20 2:58
Member 1500282326-Nov-20 2:58 
GeneralMy vote of 5 Pin
Gladys Merma Molina20-Mar-13 19:28
Gladys Merma Molina20-Mar-13 19:28 
QuestionPage reload Error Pin
Dhanashree Dive20-Jan-13 19:20
Dhanashree Dive20-Jan-13 19:20 
GeneralRegarding Video Conferencing Pin
Praneshprakash21-Dec-12 18:19
Praneshprakash21-Dec-12 18:19 
GeneralMy vote of 5 Pin
sagar pathe18-May-12 2:58
sagar pathe18-May-12 2:58 
AnswerThanks Abhijit Jana Pin
mohanjune198726-Apr-12 3:28
mohanjune198726-Apr-12 3:28 
GeneralMy vote of 5 Pin
sau735-Apr-12 10:50
sau735-Apr-12 10:50 
GeneralMy vote of 5 Pin
Prasad_Kulkarni11-Mar-12 20:47
Prasad_Kulkarni11-Mar-12 20:47 
GeneralRe: My vote of 5 Pin
amit_jain_online28-Mar-12 1:54
amit_jain_online28-Mar-12 1:54 
GeneralMy vote of 3 Pin
Tarika Joshi14-Sep-11 19:35
Tarika Joshi14-Sep-11 19:35 
GeneralMy vote of 5 Pin
parveen_gkv12-Dec-10 2:00
parveen_gkv12-Dec-10 2:00 
GeneralMy vote of 2 Pin
L Viljoen9-Dec-10 22:52
professionalL Viljoen9-Dec-10 22:52 
GeneralRe: My vote of 2 Pin
Abhijit Jana10-Dec-10 0:21
professionalAbhijit Jana10-Dec-10 0:21 
GeneralRe: My vote of 2 Pin
L Viljoen10-Dec-10 1:08
professionalL Viljoen10-Dec-10 1:08 
GeneralRe: My vote of 2 Pin
Noman Aftab11-Dec-10 7:08
Noman Aftab11-Dec-10 7:08 
GeneralMy vote of 1 Pin
yunusatmaca9-Dec-10 19:37
yunusatmaca9-Dec-10 19:37 
GeneralRe: My vote of 1 Pin
Indivara9-Dec-10 19:51
professionalIndivara9-Dec-10 19:51 
GeneralMy vote of 3 Pin
Sandesh M Patil27-Nov-10 4:47
Sandesh M Patil27-Nov-10 4:47 
GeneralRe: My vote of 3 Pin
Abhijit Jana10-Dec-10 0:23
professionalAbhijit Jana10-Dec-10 0:23 
Generalhelp Pin
hitesh jariwala25-Mar-10 2:23
hitesh jariwala25-Mar-10 2:23 
AnswerRe: help Pin
Abhijit Jana27-Aug-10 8:20
professionalAbhijit Jana27-Aug-10 8:20 

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.