3

i have created a model from the standard Northwind database with the Entity Framework. Now i want to fill a Gridview with a list of categories, but in that category entity(table) is a collumn Picture aswell. The gridview gets filled with the Id, Description and CategoryName but i dont get the Picture in the gridview collumn which is binary data.

Anyone know a solution for this?

thanks.

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
Wartodust
  • 284
  • 1
  • 6
  • 16
  • It's doing what you're asking it to do, you're most likely not getting the actual image or if it is you're not telling the gridview to render the image. Are you using WPF or Winforms? – Mark W Oct 28 '11 at 16:19
  • Ever tried google? there are plenty of articles about images on gridviews. EF as a way to load a byte array is just a detail. – Davide Piras Oct 28 '11 at 16:20
  • this is a duplicate of this: http://stackoverflow.com/questions/1573287/gridview-image-gallery-from-binary-data-images if you use EF to load a class with a member of type byte[] does not really make much difference – Davide Piras Oct 28 '11 at 17:54
  • Actually yes i did google.Most examples arent with EF and since i have a Entity Category with 4 properties i dont see how i can convert that binary data to a image file.And this is a asp.net webforms btw.A gridview with autogenerate collumns and a databind in the code behind. – Wartodust Oct 28 '11 at 18:19

1 Answers1

0

you can do like this......

you have to add new generic handler -------Right click on solution Explorer and add new Generic Handler and name it “ImageHandler.ashx”

NOTE : This is only sample example on how to load images from database and display in gridview

this is the code in imagehandler.ashx

<%@ WebHandler Language="C#" %>

using System;
using System.Web;
using System.IO;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Linq;

public class ImageHandler : IHttpHandler {

    public void ProcessRequest (HttpContext context)
    {
        HttpRequest req = context.Request;          
        // string categoryID = "1";          
        string categoryID = req.QueryString["CategoryID"].ToString();          
        // Get information about the specified category          
        NorthwindDataContext db = new NorthwindDataContext();          
        var category = from c in db.Categories                         
                       where Convert.ToInt32(c.CategoryID) == Convert.ToInt32(categoryID)
                       select c.Picture;          
        int len = category.First().Length;          
        // Output the binary data          
        // But first we need to strip out the OLE header          
        int OleHeaderLength = 78;          
        int strippedImageLength = len - OleHeaderLength;          
        byte[] imagdata = new byte[strippedImageLength];          
        Array.Copy(category.First().ToArray(), OleHeaderLength, imagdata, 0, strippedImageLength);          
        if ((imagdata) != null)          
        {              
            MemoryStream m = new MemoryStream(imagdata);              
            System.Drawing.Image image = System.Drawing.Image.FromStream(m);              
            image.Save(context.Response.OutputStream, ImageFormat.Jpeg);          
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

and in Default.aspx page add new Gridview control and bind it using SQLDatasource control

<div>

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
        AutoGenerateColumns="False" DataKeyNames="CategoryID"
        DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333"
        GridLines="None">
        <RowStyle BackColor="#EFF3FB" />
        <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='<%#"ImageHandler.ashx?CategoryID="+ Eval("CategoryID")  %>'/>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
        SelectCommand="SELECT * FROM [Categories]"></asp:SqlDataSource>

</div>

I hope it will helps you......

Glory Raj
  • 17,397
  • 27
  • 100
  • 203