.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Imran Ghani

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Retrieve Image from Sql & Display in Gird

Posted By: shahin     Posted Date: January 06, 2010    Points:2   Category :ASP.Net
Hi Friends,

How to retrieve the image which is stored in binary format in an SQL (image data type) Table into the Datagrid in Web Application.

Can any one tell me how to do this? (C#)

Thanks & Regards

Author: Lalij Mer             
Posted Date: January 06, 2010     Points: 5   

Populating the GridView Control:

The next step is to populate the GridView control with data as well as images. Take a look at the code below which is used to populate the GridView.

private void BindData()


SqlConnection myConnection = new SqlConnection(ConnectionString);

SqlDataAdapter ad = new SqlDataAdapter("SELECT UserID, FirstName, LastName,Url FROM Users", myConnection);

DataSet ds = new DataSet();


GridView1.DataSource = ds;



As, you can see the above code is pretty straight forward and you might have done this thousand of times. Now, let's see how we can display the images into the GridView control.

Display Images into the GridView Control:

The first thing you need to do is to add a template column in the GridView control. Once, you have added the template column simply add an Image server control inside the template column. The HTML code of the GridView will look something like the following code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4">


<asp:BoundField DataField="UserID" HeaderText="UserID" />

<asp:BoundField DataField="FirstName" HeaderText="First Name" />

<asp:TemplateField HeaderText="Image">


<asp:Image ID="Image1" ImageUrl='<%# (string) FormatImageUrl( (string) Eval("Url")) %>' runat="server" />




<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />

<RowStyle BackColor="White" ForeColor="#330099" />

<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />

<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />

<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />


I have made the Image tag bold in the above code so you will easily identify it. Next thing we need to see is the purpose of the FormatImageUrl method which is used to assign the correct url to the ImageUrl property of the Image control.

protected string FormatImageUrl(string url)


if (url != null && url.Length > 0)

return ("~/" + url);

else return null;


The purpose of "~/" is to map the url relative to the root. This means that the Image control will look for the ImageUrl starting from the root of the website.

more details

Please Check my answer if you help....
Thank You...

Author: Lalij Mer             
Posted Date: January 06, 2010     Points: 5   

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;




Please Check my answer if you help....
Thank You...

Author: Ashutosh Jha             
Posted Date: December 25, 2013     Points: 20   

Hi ,

Please follow the below link to get images in data grid which is saved in database in binary format.


Ashutosh Jha


Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend