Display image in gridview
In this article we will see how to display image in a gridview from database (SQL Server).
In this case we need a
Template column in a
gridview and add html image (img) tag in it. you can also take asp.net
image control but it will generate view state so we have taken html img
tag.
if you are using asp.net image control than i would suggest that make the
EnableViewState property of image control to false.
Lets see the scenario
Display Picture in a GridView from Database
let's 1st create a table and call it as
Image_Table
to create sample table below is the code. Here I am using Image data
type of SQL server to save image into it. As we all know that SQL Server
store Image as binary data in a column of datatype
IMAGE.
| CREATE TABLE [dbo].[Image_Table]( |
| [id] [ int ] IDENTITY(1,1) NOT NULL , |
| [SomeText] [ varchar ](50) NULL , |
| [ImagePath] [ varchar ](200) NULL , |
| [Image_Blob] [image] NULL , |
| CONSTRAINT [PK_Image_Table] PRIMARY KEY CLUSTERED |
| ) WITH (PAD_INDEX = OFF , STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [ PRIMARY ] |
| ) ON [ PRIMARY ] TEXTIMAGE_ON [ PRIMARY ] | |
Here is the table schema in figure
Now Create one stored procedure to fetch data from database and bind that to GridView
| CREATE PROCEDURE GetImageInfoFromDB |
| SELECT id,SomeText,ImagePath,Image_Blob FROM dbo.Image_Table |
In this example we are using HTTPHandler to fetch Images from Database.
For that I have created another stored procedure which will accept one
input parameter id
| CREATE PROCEDURE GetImageFromDB(@id int ) |
|
SELECT id,SomeText,ImagePath,Image_Blob FROM dbo.Image_Table WHERE id = @id |
|
| I assume that some data is available into the table | | | | |
To add HTTPHandler right click on the project explorer --> AddNewItem --> Generic Handler from the templete eg given pic below and Name it as ImageHanlder.ashx
Now in you HTTPHandler you can add this lines of code
| <%@ WebHandler Language= "C#" Class= "ImageHandler" %> |
| using System.Data.SqlClient; |
| using System.Configuration; |
| public class ImageHandler : IHttpHandler { |
| public void ProcessRequest(HttpContext context) |
| string strId = context.Request.QueryString[ "Id" ]; |
| if (! string .IsNullOrEmpty(strId)) |
| SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[ "MyConnectionString" ].ConnectionString); |
| SqlDataReader sqlDr = null ; |
| SqlCommand sqlCmd = new SqlCommand( "GetImageFromDB" , conn); |
| sqlCmd.CommandType = CommandType.StoredProcedure; |
| sqlCmd.Parameters.AddWithValue( "@id" , strId); |
| sqlDr = sqlCmd.ExecuteReader(); |
| context.Response.BinaryWrite((Byte[])sqlDr[3]); |
| if (sqlDr != null && !sqlDr.IsClosed) |
Now in you page in which gridView is there your code should look like this after adding templete column
| < asp:gridview id = "GridView1" runat = "server" autogeneratecolumns = "False" > |
| < asp:boundfield datafield = "SomeText" headertext = "Header Text" > |
| < asp:templatefield headertext = "Image Form DataBase" > |
| < img alt = "Image" src = "%3C%#%20%22ImageHandler.ashx?Id=%22+%20Eval%28%22id%22%29%20%%3E" height = "70px" width = "70px" > |
| </ asp:boundfield ></ columns > |
Now to bind data in a gridview you can add below lines of code in any
event in which you want like Page Load event, Button Click event etc.
In C# example
| string returnValue = string .Empty; |
| string conString = ConfigurationManager.ConnectionStrings[ "MyConnectionString" ].ConnectionString; |
| SqlConnection sqlConn = new SqlConnection(conString); |
| SqlDataReader sqlDr = null ; |
| SqlDataAdapter salDa = new SqlDataAdapter( "GetImageInfoFromDB" , sqlConn); |
| DataSet ds = new DataSet(); |
| GridView1.DataSource = ds; |
| if (sqlDr != null && !sqlDr.IsClosed) |
If you run the application you can see in image in gridview like this