We have Generic handler class in .Net frame work. Add generic handler class to your project like --> right click on your project -->Add --> Select New Item --> select generic handler class.
For our example name generic handler as Handler1.ashx(generic handler class extension is .ashx)
pass query string Handler1 class .
In this example i am passing query string to handler class in DataList Item template field. see below
My table tblComputers Having 'Uid' column
And this my C# code for DataList to Bind Data
SqlConnection cn = new SqlConnection("server=.;DataBase=HareeshServices;uid=sa;pwd=roopasoft");
SqlDataAdapter da;
SqlCommand cmd;
DataTable dt;
public void GetImage()
{
dt = new DataTable();
cmd = new SqlCommand("select * from tblComputers", cn);
da = new SqlDataAdapter(cmd);
da.Fill(dt);
dlComputers.DataSource = dt;
dlComputers.DataBind();
}
Now goto Handler1.ashx class Write following Code
public class Handler1 : IHttpHandler
{
SqlConnection cn = new SqlConnection("server=.;DataBase=HareeshServices;uid=sa;pwd=roopasoft");
SqlCommand cmd;
//SqlDataReader dr;
public void ProcessRequest(HttpContext context)
{
if (context.Request.QueryString["Uid"] == null) return;
string Uid= context.Request.QueryString["Uid"];
cmd = new SqlCommand("select ImageByte from tblComputers where Uid=@Uid", cn);
cmd.Parameters.Add(new SqlParameter("@Uid",Uid));
cn.Open();
//using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
//{
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
dr.Read();
context.Response.BinaryWrite((Byte[])dr[dr.GetOrdinal("ImageByte")]);
dr.Close();
//}
}
For our example name generic handler as Handler1.ashx(generic handler class extension is .ashx)
pass query string Handler1 class .
In this example i am passing query string to handler class in DataList Item template field. see below
My table tblComputers Having 'Uid' column
And this my C# code for DataList to Bind Data
SqlConnection cn = new SqlConnection("server=.;DataBase=HareeshServices;uid=sa;pwd=roopasoft");
SqlDataAdapter da;
SqlCommand cmd;
DataTable dt;
public void GetImage()
{
dt = new DataTable();
cmd = new SqlCommand("select * from tblComputers", cn);
da = new SqlDataAdapter(cmd);
da.Fill(dt);
dlComputers.DataSource = dt;
dlComputers.DataBind();
}
Now goto Handler1.ashx class Write following Code
public class Handler1 : IHttpHandler
{
SqlConnection cn = new SqlConnection("server=.;DataBase=HareeshServices;uid=sa;pwd=roopasoft");
SqlCommand cmd;
//SqlDataReader dr;
public void ProcessRequest(HttpContext context)
{
if (context.Request.QueryString["Uid"] == null) return;
string Uid= context.Request.QueryString["Uid"];
cmd = new SqlCommand("select ImageByte from tblComputers where Uid=@Uid", cn);
cmd.Parameters.Add(new SqlParameter("@Uid",Uid));
cn.Open();
//using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
//{
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
dr.Read();
context.Response.BinaryWrite((Byte[])dr[dr.GetOrdinal("ImageByte")]);
dr.Close();
//}
}
}
Here Handler1 class fires when we bind data to datasource
No comments:
Post a Comment