Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9211645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:20:42+00:00 2026-06-18T01:20:42+00:00

Handler public class Handler2 : IHttpHandler { public void ProcessRequest(HttpContext context) { string id

  • 0

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";
}  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:20:43+00:00Added an answer on June 18, 2026 at 1:20 am

    You could have your generic handler return 404 status code if the image doesn’t exist:

    public void ProcessRequest(HttpContext context)
    {
        string id = context.Request.QueryString["Id"];
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["EmployeeDatabase"].ConnectionString;
        using (var con = new SqlConnection(constr))
        using (var cmd = con.CreateCommand())
        {
            con.Open();
            cmd.CommandText = "GetImage";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@EmpID", id);
            using (var dr = cmd.ExecuteReader())
            {
                if (dr.Read())
                {
                    // the image was found
                    byte[] imageBytes = (byte[])dr.GetValue(0);
                    // Don't forget to set the proper content type
                    context.Response.ContentType = "image/png";
                    context.Response.BinaryWrite(imageBytes);
                }
                else
                {
                    // no record found in the database => return 404
                    context.Response.StatusCode = 404;
                }
            }
        }
    }
    

    and then on the client you could use a WebClient to test if the image exists:

    var imageUrl = "~/Handler2.ashx?Id=" + empid;
    var baseUri = new Uri(Request.Url.GetLeftPart(UriPartial.Authority));
    var url = new Uri(baseUri, VirtualPathUtility.ToAbsolute(imageUrl));
    using (var client = new HttpClient())
    {
        var request = new HttpRequestMessage(HttpMethod.Head, url);
        var response = client.SendAsync(request).Result;
        if (response.IsSuccessStatusCode)
        {
            // the image exists:
            PageLinkButton.Text = "Update your Image";
            Image2.ImageUrl = imageUrl;
        }
        else
        {
            // the image does not exist:
            PageLinkButton.Text = "Add your Image";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom handler like this, public class Handler : IHttpHandler { public
I've wrote a simple handler: public class ImageHandler : IHttpHandler, IRequiresSessionState { public bool
I have the following file upload handler: public class FileUploader : IHttpHandler { public
I have a Handler class that looks like this: class Handler{ public $group; public
I have this code : class Event{}; class CustomEvent:public Event{}; class Handler { public:
I have this code for an event handler: rbM.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton
I have a service with an following constructor: public ShimmerService(Context context, Handler handler) {
Context I have this piece of Java Code btn.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent
I have an HttpHandler class (implements IHttphandler) where the path defined for the handler
Possible Duplicate: Java: Global Exception Handler I thought of something like this: public static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.