To display an image in front end first we have to create an Handler. After creating handler it will display the image in aspx page.
First create an event handler (Go to website >>right click >>click on Add new item>>in the middle you will get a generic handler click on add.
after creating that write the following code :
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
public class ImageHandler : IHttpHandler
{
string strcon = ConfigurationManager.AppSettings["EmployeeConnectionString"].ToString();
public void ProcessRequest(HttpContext context)
{
int emp_Id = Convert.ToInt32(context.Request.QueryString["emp_Id"]);
SqlConnection connection = new SqlConnection(strcon);
connection.Open();
SqlCommand command = new SqlCommand("select image from EmployeeDetails where emp_Id=" + emp_Id, connection);
SqlDataReader dr = command.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
if (dr[0] != DBNull.Value)
{
context.Response.BinaryWrite((Byte[])dr[0]);
connection.Close();
context.Response.End();
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
Note:
1)emp_Id will get from aspx page.
2)Event handler will take the remaining things like conversion .
Now write the code in aspx.cs:
SqlConnection connection = new SqlConnection(strcon);
SqlCommand command = new SqlCommand("SELECT * from EmployeeDetails ", connection);
SqlDataAdapter daimages = new SqlDataAdapter(command);
DataTable dt = new DataTable();
daimages.Fill(dt);
gvImages.DataSource = dt;
gvImages.DataBind();
First create an event handler (Go to website >>right click >>click on Add new item>>in the middle you will get a generic handler click on add.
after creating that write the following code :
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
public class ImageHandler : IHttpHandler
{
string strcon = ConfigurationManager.AppSettings["EmployeeConnectionString"].ToString();
public void ProcessRequest(HttpContext context)
{
int emp_Id = Convert.ToInt32(context.Request.QueryString["emp_Id"]);
SqlConnection connection = new SqlConnection(strcon);
connection.Open();
SqlCommand command = new SqlCommand("select image from EmployeeDetails where emp_Id=" + emp_Id, connection);
SqlDataReader dr = command.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
if (dr[0] != DBNull.Value)
{
context.Response.BinaryWrite((Byte[])dr[0]);
connection.Close();
context.Response.End();
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
Note:
1)emp_Id will get from aspx page.
2)Event handler will take the remaining things like conversion .
Now write the code in aspx.cs:
SqlConnection connection = new SqlConnection(strcon);
SqlCommand command = new SqlCommand("SELECT * from EmployeeDetails ", connection);
SqlDataAdapter daimages = new SqlDataAdapter(command);
DataTable dt = new DataTable();
daimages.Fill(dt);
gvImages.DataSource = dt;
gvImages.DataBind();
No comments:
Post a Comment