views:

33

answers:

2

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.ContentType = "image/bmp"; //Added after "Ed B" Sugetion but still dosenot work :(
                            context.Response.BinaryWrite((byte[])sqlDr["Picture"]);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.Message);
            }
        }
    }
A: 

You need to set the content type in the generic handler

context.Response.ContentType = "image/bmp";

Hmm..try opening the handler with querystring inputs, directly from your browser...does that work?

http://yourlocalhost/yourproject/ImageHandler.ashx?CategoryID=2

You should see an error message if it fails.

Ed B
nop! it still dose not work :(
Neel
Are you sure it's a bitmap image that you stored in the DB?
IrishChieftain
http://abc/yourproject/ImageHandler.ashx?CategoryID=2 , broken image icon :(
Neel
A: 

first of all thanks for your reply Ed B

it seems the problem was not the code (ofcourse other then setting ContentType = "image/bmp") i found the solution here http://www.developerfusion.com/code/5223/using-ashx-files-to-retrieve-db-images/

Neel