3

My problem is this. When I just have a single parameter to search by, my image control works, when I try and pass a second parameter through, the image control does not work. I am using a generic handler as the image.URL, as I want to be able to either pull the image out of a database, or select one from the drive with which to replace the database copy.

Ok, so after having removed all references to the version (which is the second parameter causing the problem) I have found that this line string version = context.Request.QueryString["Version"].ToString(); in ImageHandler.ashx, is causing the problem.

My code is as follows...

 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;
        }
    }
}

}

The coed 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 the 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 extra parameter into the ReturnData method, No images appear.

Muchly appreciated guys. Thanks

Bigtingz92
  • 129
  • 1
  • 15

1 Answers1

1

I created a test to simulate what you're trying to do and the error happened only once, than I couldn't reproduced it anymore. I realized that I wasn't always compiling the code. Then I started to always compile and the error no longer happened.

You can check the code here.

Just call ~/Handler1.ashx?schemeCode=whatever&Version=a ~/Handler1.ashx?schemeCode=whatever&Version=b Or ~/Handler1.ashx?schemeCode=whatever&Version=c

and you should see three different pictures.

You're calling .ToString() just after access the QueryString in this lines:

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

If in your tests you forgot to compile when changing this lines, you may have some inconsistent behaviors because it could throw a NullReferenceException.

japoneizo
  • 508
  • 1
  • 5
  • 15