C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » DataGridView »

Displaying Images in GridView


Posted Date: 03 Oct 2008    Resource Type: Code Snippets    Category: DataGridView
Author: muralidharMember Level: Gold    
Rating: 1 out of 5Points: 10



to display images in gridview.you have to do the following

in button click just paste the following code

string strSql = "Select ImageId, ImageName, Images, ImageType, ImageSize from Pictures";
SqlDataAdapter daPics = new SqlDataAdapter(strSql, con);
daPics.Fill(dsPics);
dsPics.Tables[0].Columns.Add("imgFile");
foreach (DataRow drPics in dsPics.Tables[0].Rows)
{
drPics["imgFile"] = ("grabpicture.aspx?ImageId=" + drPics[0]);
}
gvWithImages.DataSource = dsPics;
gvWithImages.DataBind();

in the above code pictures table is used and its design script is attached.you can find it in the attachment.

and i have used one more aspx page to load the image that is "grabpicture.aspx"

in grabpicture.aspx page global declaration area declare these variables

DataSet dsPics;
SqlDataAdapter daPics;
byte[] ImgContent;
DataRow drPics;
string strSql, strImgContentType;
SqlConnection con = new SqlConnection("user id=<user>;pwd=<password>;data source=<sysname/ipaddress>;database=<dbname>");

in grabpicture.aspx page load event write the following code
strSql = "Select * from Pictures where ImageId = " + Request.QueryString["ImageId"];
daPics = new SqlDataAdapter(strSql, con);
dsPics = new DataSet();
daPics.Fill(dsPics);
drPics = dsPics.Tables[0].Rows[0];
ImgContent = (byte[])drPics["Images"];
strImgContentType = drPics[3].ToString();
Response.ContentType = strImgContentType;
Response.OutputStream.Write(ImgContent, 0, (int)drPics[4]);
Response.End();

happy coding

regards,
Muralidhar.

Attachments

  • Displaying Images in GridView (21633-332-Pictures.sql)



  • Responses

    Author: Surendra    23 Sep 2009Member Level: Bronze   Points : 0
    nice code Muralidhar


    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Displaying Images in GridView  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Displaying,Adding,Updating,Editing,Deleting data of a Grid
    Previous Resource: Export to Excel from Grid View
    Return to Discussion Resource Index
    Post New Resource
    Category: DataGridView


    Post resources and earn money!
     
    Related Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use