Second parameter interfering with the common handler

This is my problem. When I only have one parameter to look for, my image control works, when I try to pass the second parameter, the image control doesn't work. I am using a generic handler like image.URL as I want to be able to pull the image from the database or select one from the disk with which to replace the copy of the database.

So, after removing all version references (which is the second parameter causing the problem) I found that this line string version = context.Request.QueryString["Version"].ToString();

in ImageHandler.ashx was causing the problem.

My code looks like this:

 public class ImageHandler : IHttpHandler
{       
    public void ProcessRequest(HttpContext context)
    {
        TemplateData imgData = null;
        string schemeCode = context.Request.QueryString["schemeCode"].ToString();
        string version = context.Request.QueryString["Version"].ToString();

        if(!String.IsNullOrEmpty(schemeCode)) imgData  = DataClass.ReturnData(schemeCode);


        if (imgData != null)
        {
            context.Response.ContentType = "image/jpeg";
            context.Response.BinaryWrite(imgData.Logo);
            context.Response.OutputStream.Write(imgData.Logo, 0, imgData.Logo.Length);

        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

      

}

Code behind ...

        protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (ddSchemeCode.SelectedIndex > 0)
        {
            // Existing Data to load from database
            TemplateData temp = DataClass.ReturnData(ddSchemeCode.SelectedItem.Text);
            if (temp != null)
            {

                txtVersion.Text = temp.Version;
                txtComment.Text = temp.Comment;
                txtSchemeCode.Text = temp.SchemeCode;
                txtTemplateId.Text = temp.TemplateId;
                imgLogo.ImageUrl = String.Format("ImageHandler.ashx?schemeCode={0}", ddSchemeCode.SelectedItem.Text);
            }
        }

      

and ReturnData

medthod ...

        public static TemplateData ReturnData(string schemeCode)
    {
        string sqlInstructionCstmID = "SELECT TOP(1) LetterTemplateCustomisationId, TemplateId, Logo, SchemeCode, Version, Comment FROM LetterTemplateCustomisation WHERE SchemeCode ='" + schemeCode + "' ";

        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["LettersDatabase"].ConnectionString;
        SqlConnection connect = new SqlConnection(connectionString);

        SqlCommand command = new SqlCommand(sqlInstructionCstmID, connect);

        command.CommandType = CommandType.Text;

        connect.Open();

        SqlDataReader dr = command.ExecuteReader();

        TemplateData tempData = null;
        if (dr.HasRows)
        {
            dr.Read();
            tempData = new TemplateData(dr);
        }

        dr.Close();
        connect.Close();

        return tempData;
    }

      

so If I pass an additional parameter to the method ReturnData

, the images are not displayed.

Famous guys. Thanks to

+3


source to share


1 answer


I created a test to simulate what you are trying to do and the error only happened once, which I could no longer reproduce. I realized that I don't always compile the code. Then I started to always compile and there was no more error.

You can check the code here .

Just call? ~ / Handler1.ashx schemeCode = whatever & Version = huh? ~ / Handler1.ashx schemeCode = whatever & Version = b Or? ~ / Handler1.ashx schemeCode = everything & Version = with

and you should see three different images.



You call .ToString () immediately after accessing the QueryString on the following lines:

        string schemeCode = context.Request.QueryString["schemeCode"].ToString();
    string version = context.Request.QueryString["Version"].ToString();

      

If in your tests you forget to compile when you change these lines, you might have some inconsistent behavior because it might throw a NullReferenceException.

+1


source







All Articles