Click here to Skip to main content
6,631,404 members and growing! (18,325 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Beginner License: The Code Project Open License (CPOL)

Displaying Image in Gridview from Database

By Abhijit Jana

Display image in Gridview from a Database in ASP.NET
Windows, .NET 2.0, ASP.NET, ADO.NET, WebForms, SQL 2000, VS2005, DBA, Dev
Posted:7 Oct 2007
Views:39,543
Bookmarked:19 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
26 votes for this article.
Popularity: 4.01 Rating: 2.83 out of 5
8 votes, 30.8%
1

2
2 votes, 7.7%
3
1 vote, 3.8%
4
15 votes, 57.7%
5

Introduction

This is a Simple web application for users who are new in asp.net . This will show how we can retrive a image from Database and display it into Gridview

Screenshot - Output.jpg

Background

Some times we need to upload images in a web application and store it into database which store images in binary format and that can be loss of image quality. instead of that we can store the image path into database and retrive that image path from it and also the picture from there location and display to a webpage .

we need Ado.net to coonect with database. My database in SQLServer.

Screenshot - db.jpg

In This database Profile_Picture filed contain the image path. In my case , i have store all Images my applcation Directroy. If any one try to change can Do, like "~\myfolder\myimage.jpg" ,

But Our Application read image from Current directory, so if u use different folder rather than current directory, you have to set the image folderto current Directory by Directory.SetCurrentDirectory using System.IO.

We need to set some properties in Gridview also

Screenshot - gridproperty.jpg

1. Unchecked the Auto-generate filed

2. Use two BoundFiled for UserName and Country

3. Set the Header Text and Datafiled (field name in Database)

4. Use on Image Filed for image and set DataImageUrlFiled=ImagefiledName

5. Click on OK

Using the code

As per Above Discussion , Code 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=MySe;Integrated Security=True; database=test";
Load_GridData(); // call Load_GridData()

}

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 image in your web application is always interesting and afterall its from database !!!

History

Keep a running update of any changes or improvements you've made here.

License

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

About the Author

Abhijit Jana


Member
Abhijit has done Master Degree in Computer Application from Heritage Institute of Technology (HIT-K) ,Kolkata, West Bengal, India . He is an interested, committed, creative Software professional having more than 2.8 years of solid experience in web-based and windows based solutions in Microsoft Technologies using .NET 2.0, .NET 3.0 , .NET 3.5, ASP.NET 2.0, ASP.NET 3.5 C# 2.0, AJAX, Silverlight, Web Services, MS SQL Server 2005, Exchange Server, Active Directory, and Dot Net Nuke (DNN),Win Forms, WinServices, WSS (Windows Sharepoint Server 3.0 ), WPF, WWF. He is also an MCP (Microsoft Certified Professional) and MCTS (Microsoft Certified Technology Specialist) on Web Development. He has good knowledge of Object Oriented Programming, 3-Tier Architecture and Design Patterns as well as good command over IIS (IIS 5.1,IIS 6.0, IIS 7.0) and deployment of Application on Live Production Environment . His hobbies, listing to music and Developing Own small Tools Utilities and Knowledge sharing.


Awards
CodeProject MVP 2009
Prize winner "Best ASP.NET article of Sep 2009
Prize winner "Best ASP.NET article of July 2009
Prize winner "Best ASP.NET article of June 2009"
Prize winner "Best ASP.NET article of January 2009"
Prize winner "Best ASP.NET article of November 2008"

Prize winner "Best ASP.NET article of October 2008"

Abhijit's CodeProject Guru :
Sacha Barber
Occupation: Software Developer (Senior)
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
GeneralFull code Pinmembersingletony22:04 7 Jul '09  
QuestionHow to achieve this with Windows Application? PinmemberNarendra Reddy Vajrala5:39 26 May '09  
GeneralNothing is displaying inside Grdview column please help Pinmemberrameez Raja23:40 23 Feb '09  
GeneralRe: Nothing is displaying inside Grdview column please help PinmvpAbhijit Jana0:38 24 Feb '09  
GeneralRe: Nothing is displaying inside Grdview column please help Pinmemberrameez Raja2:49 24 Feb '09  
QuestionHow to create an image display in a grid view? PinmemberCejal4:44 7 Oct '08  
AnswerRe: How to create an image display in a grid view? PinmemberAbhijit Jana5:37 7 Oct '08  
GeneralRe: How to create an image display in a grid view? PinmemberCejal5:56 7 Oct '08  
GeneralRe: How to create an image display in a grid view? PinmemberAbhijit Jana6:06 7 Oct '08  
QuestionRe: How to create an image display in a grid view? PinmemberCejal6:37 7 Oct '08  
AnswerRe: How to create an image display in a grid view? PinmemberAbhijit Jana7:02 7 Oct '08  
QuestionMy code.....Please take a look and let me know on necessary changes PinmemberCejal7:09 7 Oct '08  
GeneralProblem with the code PinmemberCejal7:11 7 Oct '08  
GeneralRe: Problem with the code PinmemberPraveen Kumar A22:18 14 Jul '09  
GeneralRe: How to create an image display in a grid view? PinmemberAbhijit Jana6:07 7 Oct '08  
Questionimage database type Pinmemberheba tayyeb3:34 24 Jun '08  
AnswerRe: image database type PinmemberAbhijit Jana3:47 24 Jun '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 Oct 2007
Editor:
Copyright 2007 by Abhijit Jana
Everything else Copyright © CodeProject, 1999-2009
Web09 | Advertise on the Code Project