I am developing a project where I am to retrieve image from database to display in GridView control. My image was stored on database in Binary Format not as a link. I am also using Handler.ashx file for to process my request and return a response to the browser. But unfortunately I am getting this error:
Parameterized Query '(@ImageId int)Select Image_Content, Image_Type from ImageGallery' expects parameter @ImageId, which was not supplied. Statement(s) could not be prepared.
My Handler.ashx Page Code
public void ProcessRequest (HttpContext context)
{
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString);
myConnection.Open();
string sql = "Select Image_Content, Image_Type from ImageGallery where Img_Id=@ImageId";
SqlCommand cmd = new SqlCommand(sql, myConnection);
cmd.Parameters.Add("@ImageId", SqlDbType.Int).Value = context.Request.QueryString["id"];
cmd.Prepare();
SqlDataReader dr = cmd.ExecuteReader(); // there is my error
dr.Read();
context.Response.ContentType = dr["Image_Type"].ToString();
context.Response.BinaryWrite((byte[])dr["Image_Content"]);
dr.Close();
myConnection.Close();
}
My Default.aspx Page Code
SelectCommand="SELECT [Img_Id], [Image_Content], [Image_Type], [Image_Size] FROM [ImageGallery]">
In Handler.ashx page I am getting error, I have underlined above in code.
Please fix it.........

PJ MartinsPosted Jun 5, 2010, 9:34 AM