Click here to Skip to main content
15,880,427 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 395.2K   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

 
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 
GeneralMy vote of 1 Pin
Ali Saeedi2-Feb-10 23:32
Ali Saeedi2-Feb-10 23:32 
GeneralRe: My vote of 1 Pin
Abhijit Jana27-Aug-10 8:19
professionalAbhijit Jana27-Aug-10 8:19 
QuestionPlease Help? Pin
jessy_j101-Feb-10 6:11
jessy_j101-Feb-10 6:11 
Hi,

I 'm populating the images in the listview control from the database.... but now i want to get only broken images from the server(eg images which are not showing, cross box) so my user can fix those images in the server. I want to put a checkbox in the search criteria so either they can see all the images from the table or only broken images.
Is there any way of doing that from asp.net or with javascript.

Any help will be appreciated.

Thanks.
GeneralFull code Pin
singletony7-Jul-09 21:04
singletony7-Jul-09 21:04 
QuestionHow to achieve this with Windows Application? Pin
Narendra Reddy Vajrala26-May-09 4:39
Narendra Reddy Vajrala26-May-09 4:39 
GeneralNothing is displaying inside Grdview column please help Pin
Rameez Raja23-Feb-09 22:40
Rameez Raja23-Feb-09 22:40 
GeneralRe: Nothing is displaying inside Grdview column please help Pin
Abhijit Jana23-Feb-09 23:38
professionalAbhijit Jana23-Feb-09 23:38 
GeneralRe: Nothing is displaying inside Grdview column please help Pin
Rameez Raja24-Feb-09 1:49
Rameez Raja24-Feb-09 1:49 
QuestionHow to create an image display in a grid view? Pin
Cejal7-Oct-08 3:44
Cejal7-Oct-08 3:44 
AnswerRe: How to create an image display in a grid view? Pin
Abhijit Jana7-Oct-08 4:37
professionalAbhijit Jana7-Oct-08 4:37 
GeneralRe: How to create an image display in a grid view? Pin
Cejal7-Oct-08 4:56
Cejal7-Oct-08 4:56 
GeneralRe: How to create an image display in a grid view? Pin
Abhijit Jana7-Oct-08 5:06
professionalAbhijit Jana7-Oct-08 5:06 
QuestionRe: How to create an image display in a grid view? Pin
Cejal7-Oct-08 5:37
Cejal7-Oct-08 5:37 
AnswerRe: How to create an image display in a grid view? Pin
Abhijit Jana7-Oct-08 6:02
professionalAbhijit Jana7-Oct-08 6:02 
QuestionMy code.....Please take a look and let me know on necessary changes Pin
Cejal7-Oct-08 6:09
Cejal7-Oct-08 6:09 
GeneralProblem with the code Pin
Cejal7-Oct-08 6:11
Cejal7-Oct-08 6:11 
GeneralRe: Problem with the code Pin
Praveen Kumar A14-Jul-09 21:18
Praveen Kumar A14-Jul-09 21:18 
GeneralRe: How to create an image display in a grid view? Pin
Abhijit Jana7-Oct-08 5:07
professionalAbhijit Jana7-Oct-08 5:07 
Questionimage database type Pin
heba tayyeb24-Jun-08 2:34
heba tayyeb24-Jun-08 2:34 
AnswerRe: image database type Pin
Abhijit Jana24-Jun-08 2:47
professionalAbhijit Jana24-Jun-08 2:47 

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.