Click here to Skip to main content
15,878,959 members
Articles / All Topics

How to Show Binary Image Column in GridView

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
21 Oct 2014CPOL 8.5K   2   1
How to show Binary Image column in GridView
  1. Create a Handler.ashx file to perform image retrieval. This Handler.ashx page will contain only one method called ProcessRequest. This method will return binary data to the incoming request. In this method, we do normal data retrieval process and return only the Image_Content field as bytes of array.

    The sample code follows:

    C#
    public void ProcessRequest(HttpContext context)
     {
        SqlConnection myConnection = new SqlConnection("YourConnectionString");
        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();
        dr.Read();
        context.Response.ContentType = dr["Image_Type"].ToString();
        context.Response.BinaryWrite((byte[])dr["Image_Content"]);
        dr.Close();
        myConnection.Close();
     }
  2. Place a GridView control in your aspx page, with one TemplateField column, add an Image control into the TemplateField’s ItemTemplate section.

    Specify the ImageUrl property as:

    ASP.NET
    <asp:TemplateField>
    <ItemTemplate>
    <asp:Image ID="Image1? runat="server" 
    ImageUrl=<%# "Handler.ashx?id=" + Eval("Img_Id") %>‘ />
    </ItemTemplate>
    </asp:TemplateField>
  3. Now we can bind the GridView control to display all the records in the table as follows:
    JavaScript
    GridView1.DataSource = FetchAllImagesInfo();
    GridView1.DataBind();

    Before you bind the GridView, you should write the FetchAllImagesInfo method to return all the records with their image data from the table and then you have to load the images into the GridView control.

    The code for FetchAllImagesInfo is:

    C#
    public DataTable FetchAllImagesInfo())
     {
        string sql = "Select * from ImageGallery";
        SqlDataAdapter da = new SqlDataAdapter(sql, "Your Connection String");
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
     }

Filed under: ASP.NET, C#, CodeProject

Tagged: Binary Image in GridView, Image Binding, image in ASP.NET, Show Image

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Manipal Technologies
India India
Hi Everyone, this is Shipra Gupta, got 11 yrs experience in development. I am passionate about coding and love to learn Smile | :)
looking forward for your questions and feedbacks. will love to hear from you.
Read my blogs at http://shipra2003.wordpress.com
Thanks!!!

Comments and Discussions

 
QuestionNice Shipra Gupta! Pin
Volynsky Alex21-Oct-14 12:07
professionalVolynsky Alex21-Oct-14 12:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.