Handler
public class Handler2 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string id = context.Request.QueryString["Id"];
string constr = System.Configuration.ConfigurationManager
.ConnectionStrings["EmployeeDatabase"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd = new SqlCommand("GetImage", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@EmpID", id);
//cmd.Parameters.AddWithValue("@Gender", ge);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
byte[] imageBytes = (byte[])dr.GetValue(0);
context.Response.BinaryWrite(imageBytes);
}
dr.Close();
con.Close();
}
User Control
public void BindGridviewData()
{
String empid1 = Name;
// String empid1 = Request.QueryString["MyText"];
int empid = int.Parse(empid1);
//int empid = 1500422;
string constr = System.Configuration.ConfigurationManager
.ConnectionStrings["EmployeeDatabase"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd = new SqlCommand("GetEmployeeDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@EmpID", SqlDbType.Int, 0).Value = empid;
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
Label9.Text = dr[0].ToString();
Label2.Text = dr[1].ToString();
Label3.Text = dr[2].ToString();
Label4.Text = dr[3].ToString();
Label5.Text = dr[4].ToString();
Label6.Text = dr[5].ToString();
Label7.Text = dr[6].ToString();
Label8.Text = dr[7].ToString();
Image2.ImageUrl = "~/Handler2.ashx?Id=" + empid;
}
In the above program if we are not getting image in image control we need to display the text on button as “Add your Image” and if we have image we need to display it as “Update your Image”
I have used following ways which are not working:
Option 1
if (Image2.ImageUrl != null)
{
PageLinkButton.Text = "Upadte your Image";
}
else
{
PageLinkButton.Text = "Add your Image";
}
Option 2
WebClient client = new WebClient();
byte[] Value = client.DownloadData(Image2.ImageUrl);
if (Value != null)
{
PageLinkButton.Text = "Update your Image";
}
else
{
PageLinkButton.Text = "Add your Image";
}
You could have your generic handler return 404 status code if the image doesn’t exist:
and then on the client you could use a WebClient to test if the image exists: