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


Top 5 Contributors of the Month
david stephan

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

Responses
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);

da.Fill(ds);

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"));

Response.End();
}

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);

da.Fill(ds);



ds.Tables(0).Columns.Add("imgFile");



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

imgGrid.DataBind();



Hope this will help you

Cheers
Pankaj

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

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

}
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
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

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