![]() |
Web Development »
ASP.NET »
General
Beginner
License: The Code Project Open License (CPOL)
Displaying Image in Gridview from DatabaseBy Abhijit JanaDisplay image in Gridview from a Database in ASP.NET |
Windows, .NET 2.0, ASP.NET, ADO.NET, WebForms, SQL 2000, VS2005, DBA, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
This is a Simple web application for users who are new in asp.net . This will show how we can retrive a image from Database and display it into Gridview

Some times we need to upload images in a web application and store it into database which store images in binary format and that can be loss of image quality. instead of that we can store the image path into database and retrive that image path from it and also the picture from there location and display to a webpage .
we need Ado.net to coonect with database. My database in SQLServer.

In This database Profile_Picture filed contain the image path. In my case , i have store all Images my applcation Directroy. If any one try to change can Do, like "~\myfolder\myimage.jpg" ,
But Our Application read image from Current directory, so if u use different folder rather than current directory, you have to set the image folderto current Directory by Directory.SetCurrentDirectory using System.IO.
We need to set some properties in Gridview also

1. Unchecked the Auto-generate filed
2. Use two BoundFiled for UserName and Country
3. Set the Header Text and Datafiled (field name in Database)
4. Use on Image Filed for image and set DataImageUrlFiled=ImagefiledName
5. Click on OK
As per Above Discussion , Code is very Simple
public partial class _Default : System.Web.UI.Page { SqlConnection conn = new SqlConnection(); protected void Page_Load(object sender, EventArgs e) { conn.ConnectionString = "Data Source=MySe;Integrated Security=True; database=test"; Load_GridData(); // call Load_GridData() } void Load_GridData() { conn.Open(); //open the connection SqlDataAdapter Sqa = new SqlDataAdapter("select * from picture", conn); DataSet ds=new DataSet(); Sqa.Fill(ds); // Fill the dataset GridView1.DataSource = ds; // give data to gridview GridView1.DataBind(); conn.Close(); } }
Using image in your web application is always interesting and afterall its from database !!!
Keep a running update of any changes or improvements you've made here.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 7 Oct 2007 Editor: |
Copyright 2007 by Abhijit Jana Everything else Copyright © CodeProject, 1999-2009 Web20 | Advertise on the Code Project |