title: ASP.NET - Display Images and pdf in a GridView
body: <p>I want to display in an asp:GridView an "Images" column. The idea is to provide a thumbnails of the image with link to real size image. For some rows, this may alternatively be a PDF document. I'd like the link to be to the PDF. The PDF or image are stored in a SQL database. </p>
  
  <p>Now I have error in Handler (.ashx) file:</p>
  
  <blockquote>
    <p>"Invalid attempt to read when no data is present."</p>
  </blockquote>
  
  <p>This is my code :</p>
  
  <p><strong>ASP:</strong></p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" 
              AutoGenerateColumns="False" DataKeyNames="ID"
              DataSourceID="SqlDataSource1"&gt;
     &lt;Columns&gt;
        &lt;asp:BoundField DataField="assessment_id" HeaderText="assessment_id" 
                      InsertVisible="False" ReadOnly="True"
                      SortExpression="assessment_id" /&gt;
        &lt;asp:BoundField DataField="a_mime" HeaderText="a_mime" SortExpression="a_mime" /&gt;
        &lt;asp:TemplateField HeaderText="a_data"&gt;       
          &lt;ItemTemplate&gt;
          &lt;asp:Image ID="Image1" runat="server" ImageUrl='&lt;%# "Handler.ashx?ID=" + Eval("ID")%&gt;'/&gt;
          &lt;/ItemTemplate&gt;
        &lt;/asp:TemplateField&gt;
    &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  
  &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="&lt;%$ ConnectionStrings:testConnectionString %&gt;"
    SelectCommand="SELECT [assessment_id], [a_data], [a_mime] FROM [Assessments]"&gt;    
  &lt;/asp:SqlDataSource&gt;
  </code></pre>
  
  <p><strong>The Handler ASHX:</strong></p>
  
  <pre><code>&lt;%@ WebHandler Language="C#" Class="Handler" %&gt;
  
  public class Handler : IHttpHandler {
  
  public void ProcessRequest (HttpContext context) 
  {
      SqlConnection con = new SqlConnection();
      con.ConnectionString = ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
  
      // Create SQL Command 
      SqlCommand cmd = new SqlCommand();
      cmd.CommandText = "Select a_data from Assessments where assessment_id =@ID";
      cmd.CommandType = System.Data.CommandType.Text;
      cmd.Connection = con;
  
      SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.Int);
      ImageID.Value = Convert.ToInt32(context.Request.QueryString["assessment_id"]);
      cmd.Parameters.Add(ImageID);
      con.Open();
  
      SqlDataReader dReader = cmd.ExecuteReader();
      dReader.Read();
  
      context.Response.BinaryWrite((byte[])dReader["a_data"]);
  
      dReader.Close();
  
      con.Close();
  }
  </code></pre>
  
  <p>If it is possible, please help me. If it's time-consuming, please provide a link to an example or blog post.</p>