Can't Show image in gridview using templates

Posted by n10i on Stack Overflow See other posts from Stack Overflow or by n10i
Published on 2010-06-17T21:31:24Z Indexed on 2010/06/17 21:33 UTC
Read the original article Hit count: 208

Filed under:
|
|
|
|

i am trying to load images from the northwind database (categories table, images that are stored in the database) into grid view control. But it dosenot seems to work. Plz! have a look...

Default.aspx

    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
        AutoGenerateColumns="False" DataKeyNames="CategoryID" 
        DataSourceID="NorthWindSQLExpressConnectionString" 
        EnableModelValidation="True">
        <Columns>
            <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" 
                InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" />
            <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" 
                SortExpression="CategoryName" />
            <asp:BoundField DataField="Description" HeaderText="Description" 
                SortExpression="Description" />
            <asp:TemplateField HeaderText="Picture" SortExpression="Picture">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Picture") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Image ID="Image1" runat="server" ImageUrl='<%# RetriveImage(Eval("CategoryID")) %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="NorthWindSQLExpressConnectionString" runat="server" 
        ConnectionString="<%$ ConnectionStrings:NorthwindSQLExpressConnectionString %>" 
        SelectCommand="SELECT [CategoryID], [CategoryName], [Description], [Picture] FROM [Categories]">
    </asp:SqlDataSource>

</div>
</form>

[Partial] Default.aspx.cs

    protected string RetriveImage(object eval)
    {
        return ("ImageHandler.ashx?CategoryID=" + eval.ToString());
    }

[Partial] ImageHandler.ashx

    public void ProcessRequest(HttpContext context)
    {
        if (context.Request.QueryString == null)
        {
        }
        else
        {
            try
            {
                using (var sqlCon = new SqlConnection(conString))
                {
                    const string cmdString =
                        "Select picture from Categories where CategoryID=@CategoryID";
                    using (var sqlCmd = new SqlCommand(cmdString, sqlCon))
                    {
                        sqlCmd.Parameters.AddWithValue("@CategoryID", context.Request.QueryString["CategoryID"]);
                        string trmp = sqlCmd.ToString();
                        sqlCon.Open();
                        using (var sqlDr = sqlCmd.ExecuteReader())
                        {
                            sqlDr.Read();
                            context.Response.BinaryWrite((byte[])sqlDr["Picture"]);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.Message);
            }
        }
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET