3
Answers

Displaying Images From Database to Gridview with Parameter

Can someone help me spot my mistake? I'm trying to display an image (binary data) from my database, but the image didn't display. Below are my codes:
 
Generic Handler:
  1. public void ProcessRequest(HttpContext context)  
  2.         {  
  3.             string transactionNo = context.Request.QueryString["Data"];  
  4.             using (SqlConnection con = DBConnection.GetDbCon())  
  5.             {  
  6.                 SqlCommand cmd = new SqlCommand("spGetPicturesFromPAByPolicy", con);  
  7.                 cmd.CommandType = CommandType.StoredProcedure;  
  8.                 cmd.Parameters.AddWithValue("@TransactionNo", transactionNo);  
  9.                 con.Open();  
  10.                 SqlDataAdapter da = new SqlDataAdapter(cmd);  
  11.                 DataTable dt = new DataTable();  
  12.                 try  
  13.                 {  
  14.                     da.Fill(dt);  
  15.                 }  
  16.                 catch  
  17.                 {  
  18.                     dt = null;  
  19.                 }  
  20.                 if (dt != null)  
  21.                 {  
  22.                     Byte[] bytes = (Byte[])dt.Rows[0]["Data"];  
  23.                     context.Response.Buffer = true;  
  24.                     context.Response.Charset = "";  
  25.                     context.Response.Cache.SetCacheability(HttpCacheability.NoCache);  
  26.                     context.Response.BinaryWrite(bytes);  
  27.                     context.Response.Flush();  
  28.                     context.Response.End();  
  29.                 }  
  30.             }  
  31.         }  
My markup:
  1. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">  
  2.                     <AlternatingRowStyle BackColor="White" />  
  3.                     <Columns>  
  4.                         <asp:BoundField DataField="DateIssued" HeaderText="Date Issued" SortExpression="DateIssued" DataFormatString="{0:dd/MM/yyyy}" />  
  5.                         <asp:BoundField DataField="PAPolicyNo" HeaderText="Policy No." ReadOnly="True" SortExpression="PAPolicyNo" />  
  6.                         <asp:BoundField DataField="CustomerName" HeaderText="Insured name" SortExpression="CustomerName" />  
  7.                         <asp:BoundField DataField="PlanOption" HeaderText="Plan" SortExpression="PlanOption" />  
  8.                         <asp:BoundField DataField="PeriodFrom" HeaderText="Inception" SortExpression="PeriodFrom" DataFormatString="{0:dd/MM/yyyy}" />  
  9.                         <asp:BoundField DataField="PeriodTo" HeaderText="Expiry" SortExpression="PeriodTo" DataFormatString="{0:dd/MM/yyyy}" />  
  10.                         <asp:BoundField DataField="FileName" HeaderText="Picture name" SortExpression="FileName" />  
  11.                         <asp:TemplateField HeaderText="Picture">  
  12.                             <ItemTemplate>  
  13.                                 <asp:Image ID="Image1" runat="server" Width="200px" Height="200px"  
  14.                                     ImageUrl='<%# "/Handlers/PersonalAccidentImageHandler.ashx?transactionNo=" + Eval("Data")%>' />  
  15.                             ItemTemplate>  
  16.                         asp:TemplateField>  
  17.                     Columns>  
  18.                     <EditRowStyle BackColor="#7C6F57" />  
  19.                     <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />  
  20.                     <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />  
  21.                     <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />  
  22.                     <RowStyle BackColor="#E3EAEB" />  
  23.                     <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />  
  24.                     <SortedAscendingCellStyle BackColor="#F8FAFA" />  
  25.                     <SortedAscendingHeaderStyle BackColor="#246B61" />  
  26.                     <SortedDescendingCellStyle BackColor="#D4DFE1" />  
  27.                     <SortedDescendingHeaderStyle BackColor="#15524A" />  
  28.                 asp:GridView>  
 Code behind:
  1. private void BindGrid()  
  2.         {  
  3.             string policyNo = txtPolicyNo.Text;  
  4.             List pa = new List();  
  5.             pa = PersonalAccidentDataAccess.VerifyPolicy(policyNo);  
  6.             GridView1.DataSource = pa;  
  7.             GridView1.DataBind();  
  8.         }  
  9.         private void GetTransactionNo()  
  10.         {  
  11.             string policyNo = txtPolicyNo.Text;  
  12.             trans = PersonalAccidentDataAccess.GetTransactionNo(policyNo);  
  13.             if(txtPolicyNo.Text != null)  
  14.             {  
  15.                 lblTransactionNo.Text = trans.TransactionNo.ToString();  
  16.             }  
  17.         }  
  18.   
  19.         protected void txtPolicyNo_TextChanged(object sender, EventArgs e)  
  20.         {  
  21.             GetTransactionNo();  
  22.         }  
  23.   
  24.         protected void Button1_Click(object sender, EventArgs e)  
  25.         {  
  26.             BindGrid();  
  27.         }  
The output ;(
 
 Below is my table:
  1. CREATE TABLE [dbo].[tblPACustomerPicture](  
  2.     [Id] [int] IDENTITY(1,1) NOT NULL,  
  3.     [TransactionNo] [nvarchar](50) NULL,  
  4.     [CustomerNo] [nvarchar](50) NULL,  
  5.     [FileName] [nvarchar](250) NULL,  
  6.     [ContentType] [nvarchar](50) NULL,  
  7.     [Data] [varbinary](maxNULL,  
  8.  CONSTRAINT [PK_tblPACustomerPicture] PRIMARY KEY CLUSTERED   
  9. (  
  10.     [Id] ASC  
  11. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  12. ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]  
  13.   
  14. GO  
  15.   
  16. SET ANSI_PADDING OFF  
  17. GO  
 

Answers (3)