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

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

images in grid view

Posted By: shabazsh     Posted Date: September 10, 2008    Points:2   Category :ASP.Net
how do we display images in a gridview that r retrieved from a table column which is of image type and the images r stored in binary data format. Also these images displayed sholud be of image button type so that when we click on it . it should direct us to the next referred page

Author: Pankaj Mishra             
Posted Date: September 11, 2008     Points: 5   

Hi Shahbaz,
Its tricky, First Add one aspx page in your project say "GenerateImage.aspx" not in the page load event of this "GenerateImage.aspx" write this line of code

NOTE: We have to pass query string of the image ID for the database to retrive Image

try {

DataSet ds = new DataSet();

SqlClient.SqlDataAdapter da;

byte[] arrContent;

DataRow dr;

string strSql;

strSql = "Select * from tblImage Where imgId=" + Request.QueryString("ID"); // ID is your image ID forr Database aTable

string connString = "Server=ServerName;DatBase = dbName;UID = UserId;PWD=YourDBPassword"; // Change your connection string here
da = new SqlClient.SqlDataAdapter(strSql, connString);


dr = ds.Tables(0).Rows(0);

arrContent = (byte[])dr.Item("imgData");

string conType = dr.Item("imgType").ToString();

Response.ContentType = conType;

Response.OutputStream.Write(arrContent, 0, dr.Item("imgLength"));


catch (Exception ex) {


Now in the gridview Page write this line of code whatever event you want

DataSet ds = new DataSet();

SqlClient.SqlDataAdapter da;

string strSQL;

strSQL = "Select imgId,imgTitle from tblImage";

string connString = "Server=ServerName;DatBase = dbName;UID = UserId;PWD=YourDBPassword"; // Change your connection string here

da = new SqlClient.SqlDataAdapter(strSQL, connString);



foreach (DataRow tempRow in ds.Tables(0).Rows) {

tempRow.Item("imgFile") = ("imgGrab.aspx?id=" + tempRow.Item("imgID")); //Here imgFile is Image Filed of Gridview


imgGrid.DataSource = ds; //ImgGrid is your Grid View


Hope this will help you


Author: krishana singh             
Posted Date: October 16, 2010     Points: 5   

private void showimages()
if (con.State == ConnectionState.Open)
string query = "select * from tblImg where Status='1'";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;

catch (Exception ex)

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