Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to Display Image in GridView Control Using Property

Author: DevASP Team
Download Source Code :

In this article I will try to explain you how you can display images using property.

To begin with this article, create new application in ASP.Net 2.0. Add a GridView control and bind the Image control using ImageUrl attribute with database. The code for the .aspx page is as follows:


<asp:GridView ID="GridView1" runat="server">


     <asp:TemplateField HeaderText="Ticket Reserved">


       <asp:Image ID="imgButton" runat="server" ImageUrl='<%# GetImage(Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "TicketReserved"))) %>' ></asp:Image>






Now on .cs page add the following property:


public string GetImage(bool allowed)


     if (allowed)

         return "~/Images/image1.gif";


         return "~/Images/image2.gif";   



Now on the page load bind the GridView control with database:


protected void Page_Load(object sender, EventArgs e)



SqlConnection objConn = new SqlConnection("Server=SERVER-981CB1DBE;Persist Security Info=False;User ID=userid;Password=password;Initial Catalog=TempDatabase");




SqlCommand objCmd = new SqlCommand("Select TicketReserverd From Ticket", objConn);

objCmd.CommandType = CommandType.Text;








Article Comments
defind in databse TicketReserverd ( image,null)

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'.

Posted on 6/18/2007 5:43:44 AM by virender singh

this is very useful to me,but i have one more doubt,you are just talking about 2 images only but i have number of images in my database how to dispaly the all the images within gridview control.

plz send me resultcode as early as possible.
tahnk you

Posted on 7/4/2007 12:45:38 AM by sudheer

how to show image from sql server-2000 to grid view in, please help me, this is urgent for my project. i m waiting 4 ur quick answer, plz co-operate me. thank u

Posted on 7/7/2007 7:56:50 AM by Tousif

im arief,i want display image to gridview
using sql server 2005 &,c#

Posted on 8/10/2007 5:56:34 AM by arief

'GetImage' is a 'type' but is used like a 'variable'

i have got an above error during compilation.

Posted on 8/22/2007 6:55:50 AM by sachin

used this "ticket reserved" field as bit not as image

TicketReserved(bit, null)

handle values at front end

Posted on 11/11/2007 1:51:50 AM by jenny

sachin i think you are using it as varible here GetImage is a method which is accessed on front end through server delimiters that is '<%# %>' try to use it like this and in code behind check that you have added the method

Posted on 11/11/2007 2:24:51 AM by Faraz

It's Wonderful. But how to display different images from sql which is in image datatype.

Posted on 2/16/2008 6:03:09 AM by Vanitha

please send me How to bind images from a table stored in sql 2005 to a gridview

Posted on 3/3/2008 7:30:11 AM by Ramu

i have made a folder named image in project and there is some jpg image in that. now i want to display image in gridview

Posted on 5/7/2008 12:15:48 AM by ravi

i stored the image path in sql table..
how can i display the image in gridview?
pleas sent me the sample code for that
thank you

Posted on 6/18/2008 3:35:35 AM by Azhahanantham

i store the image in folder how can i display my images in data gird view
please send me code

Posted on 8/11/2008 7:40:05 AM by ajay

I could bind the image by saving the path in data base .If i save the image in database then i could not bind the image to grid view. Please help me in this aspect.

Posted on 8/28/2008 1:39:22 AM by Bhargav.Evn

i stored the image path in sql table..
how can i display the image in gridview?
pleas sent me the sample code for that
thank you

Posted on 11/9/2008 3:11:32 AM by arun thiagarajan

I upload images using file upload control . The images are stored in solition explorer randomly . i want to save them in a folder named images . how can i assign path to that images and how can the path in database

Posted on 11/13/2008 1:03:47 AM by mukarramjah

I want to save images in my solution explores. how can assign folder path using C# code

Posted on 11/13/2008 1:06:16 AM by Mukharanja

I am unable to store image path in the sql table. I have created a table named Product and column name URL with image datatype. I get error msg..Invalid value for cell(row1, column5). How can i store the image path? Thank you for your help in advance.

Posted on 2/3/2009 11:35:40 AM by Naveen

i stored the image path in sql table..
how can i display the image in gridview?
pleas sent me the sample code for that
thank you

Posted on 5/19/2009 4:48:00 AM by farhan naseer

very useful, thank you!!

Posted on 11/15/2009 8:50:55 PM by Chalaco

sir please send me some coding bcoz i m a new student for

sir please
ok and thanks

Posted on 11/21/2009 12:53:23 PM by dev

please i need to retrieve an image from SQL SERVER, please help :(

Posted on 1/1/2010 11:28:10 PM by alex

Sir please send me this code in VB.Net

Posted on 2/16/2010 5:59:37 AM by Mansoor Bari

I want to display some rows with images and others have no images. It is currently displaying cannot find image icon, but I want to display nothing.

Posted on 2/22/2010 12:14:55 PM by jms

i stored the image path in sql servser(2008)table..
how can i display the image in gridview? am using visual studio 2008(c#)..plz sent me the sample code(.cs and .aspx) for that to my email id as soon as possible...
thank you .....

Posted on 2/23/2010 1:55:32 AM by Vinu

Hai.. Nice

Posted on 3/1/2010 6:28:18 AM by dhivya

I have inserted the image in database and it can be saved in a folder in aspx .how to display it in gridview ???

Posted on 3/17/2010 4:04:30 AM by Trupti

How to Display Image in GridView Control from in c#

Posted on 3/17/2010 6:37:55 AM by ravi kumar

i have saved three images in image folder( . and i have fetched data (number) in grid view from database. all the data of grid view is between 1 to 100 . now i need to replace all the data of grid view by those three images(red circle, yellow circle, green circle) .
condition is like ...(C#)
if data (number)>90
green circle
if data between 80 to 90
yellow circle
red circle
please help me

Posted on 3/24/2010 7:28:57 AM by Madhu

How to Display Image in GridView Control from in c#

Posted on 3/24/2010 8:50:46 AM by Madhu

How To Display Image in GridView By Image Folder.
or By any Other Method
Please Help

Posted on 3/27/2010 7:57:10 AM by Sony

Some body please help me for the the following:
actually i am using visual foxpro 9,i need to display the image in the gridview related to the particular item.initially system is picking the first image related to first item as i move on to the next, system display the same image to the rest

Major thanks in advance

Posted on 4/27/2010 7:19:53 AM by irfhan

database feild type is not provided

Posted on 6/11/2010 4:54:18 AM by devyani

I loaded image in gridview from data base. when i delete image from gridview it detele form data base. but the image still its display in a gridview. i am using update panel.

how to refersh the image please help me

Posted on 8/26/2010 6:46:49 AM by hameed

hay i am aashish tyagi please provide the code in chatting
help me

Posted on 11/13/2010 2:15:44 AM by arya aashish tyagi

Add Article Comment:
Name :
Email Address :
Comments :
<< How to Check all Checkboxes without PostBack

Disclaimer - Privacy
© 2002-2017