Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have writtten some image upload code in which image is stored successfully in binary format in a database.
My problem is that image is not displayed in the gridview

This is my database table Pictures
ImgId   int

ImageName  varchar(500)
Image          image

Gridview.aspx
XML
<asp:GridView ID="GridView1" runat="server" Style="z-index: 103; left: 232px; position: absolute;
        top: 225px" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
        <Columns>
           <asp:TemplateField HeaderText="Image">
                 <ItemTemplate>
                    <asp:Image ID="Image1" runat="server" ImageUrl='<%# "ImageHandler.ashx?id="+ Eval("ImgId")  %>'/>
                 </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

GridView.cs
C#
public void Load_GridData()
{
    con.Open();
    string sql = "Select ImgId,Id,ImageName,Image from Pictures";
    SqlDataAdapter da = new SqlDataAdapter(sql, con);
    DataTable dt = new DataTable();
    da.Fill(dt);   // fill the dataset
    GridView1.DataSource = dt; // give data to GridView
    GridView1.DataBind();
    con.Close();
    GridView1.Attributes.Add("bordercolor", "black");
}

Handler.ascx
C#
using System;
using System.Web;
using System.Data.SqlClient;
public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\omar\Documents\Visual Studio 2005\WebSites\AJAXEnabledWebSite7\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
        //  SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
                SqlCommand com = new SqlCommand("Select  Image From Pictures Where ImgId=@imgid", con);
        com.Parameters.Add("@imgid", System.Data.SqlDbType.Int).Value = context.Request.QueryString["id"];
        con.Open();
        SqlDataReader dReader = com.ExecuteReader();
        dReader.Read();
        com.Prepare();

        context.Response.BinaryWrite((byte[])dReader["Image"]);
        con.Close();
        //context.Response.End(); 
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

Is the problem with the Handler or Gridview?
Posted
Updated 17-May-11 7:19am
v2
Comments
Wonde Tadesse 17-May-11 21:45pm    
Couple of qns.
1. Did you able to get the id value from the QueryString ?
2. Were you able to get the image value from the database ?
3. What is the type of the image extension(.jpg, .png, .gif,...) ?

1 solution

Hello,

You can Show that image into grid view by following steps:

1) Create an image object which should be generated from Binary that you read from database.
2) Assign that image object to the image object you placed into grid view at page load event.

If have more queries then reply me. Vote find helpful.

Regards Pavanraje
 
Share this answer
 
Comments
umairshoaib 17-May-11 13:17pm    
i do not understand plz you give me code

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900