Abhimanyu K Vatsa

Abhimanyu K Vatsa

  • NA
  • 40.4k
  • 21.4m

Fix It Out ? (imaeg problem in ASP)

Jun 5 2010 8:00 AM
Hello .....

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

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
            EmptyDataText="There are no data records to display." Width="540px">
            <Columns>
                <asp:BoundField DataField="Img_Id" HeaderText="Img_Id" SortExpression="Img_Id" />
                <asp:BoundField DataField="Image_Type" HeaderText="Image_Type" SortExpression="Image_Type" />
                <asp:TemplateField>
                    <ItemTemplate>
                    <asp:Image ID="Image1" runat="server" ImageUrl='<%#    "Handler.ashx?id=" + Eval("Img_Id")  %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
           
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
            ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
            SelectCommand="SELECT [Img_Id], [Image_Content], [Image_Type], [Image_Size] FROM [ImageGallery]">
        </asp:SqlDataSource>





In Handler.ashx page I am getting error, I have underlined above in code.


Please fix it.........

Answers (3)