1
Answer

Image not showing in webpage with image control in datagrid

Eswara Rajagiri

Eswara Rajagiri

8y
389
1
Here is my requirements. my database contain few fields along with image data as bytes. Now i have webpage i can filter the data with few filters and show the images in the grid view along with the data.

I wrote my code as below, all other data is showing fine in the webpage but for Images it is showing blank. in debug mode i verified it showing some bytes of information for image. I am not receiving any error messages as well. I am confused what went wrong with my code.

Please help
 
 My class:


  1. public
     class Expense   
  2. {       
  3. public string ID { getset; }       
  4. public DateTime StartTimeStamp { getset; }       
  5. public double ExpenseAmt { getset; }      
  6.  public byte[] Photo { getset; }   
  7. }  



My Button click event along with method:

  1. protected void Button1_Click(object sender, EventArgs e)  
  2.     {  
  3.         List<expense> exps = getSQLData();  
  4.         GridViewImage.DataSource = exps;  
  5.         GridViewImage.DataBind();  
  6.    }  
  7.    
  8. public List<expense> getSQLData()  
  9.     {  
  10.         List<expense> Expenses = new List<expense>();  
  11.         string cs = ConfigurationManager.ConnectionStrings["test"].ConnectionString;  
  12.    
  13.         using (SqlConnection conn = new SqlConnection(cs))  
  14.         {  
  15.             SqlCommand cmd = new SqlCommand("Select ID, Start_Timestamp,  Expense_Amt, Receipt_Photo from EXPENSE where id='testing'", conn);  
  16.             conn.Open();  
  17.             SqlDataReader rdr = cmd.ExecuteReader();  
  18.             while (rdr.Read())  
  19.             {  
  20.                 Expense exp = new Expense();  
  21.    
  22.                 exp.ID = rdr[0].ToString();  
  23.                 exp.StartTimeStamp = Convert.ToDateTime(rdr[1].ToString());  
  24.                 exp.ExpenseAmt = Convert.ToDouble(rdr[2].ToString());  
  25.                 exp.Photo = (byte[])rdr[3];  
  26.    
  27.                 Expenses.Add(exp);  
  28.    
  29.             }  
  30.         }  
  31.    
  32.         return Expenses;  
  33.     }  




My Gridview code in my aspx file:

  1. <asp:GridView ID="GridViewImage" runat="server" AutoGenerateColumns="false">  
  2. <columns>  
  3.     <asp:BoundField DataField="ID" HeaderText="EnterpriseID" />  
  4.     <asp:BoundField DataField="StartTimeStamp" HeaderText="StartTimeStamp" />  
  5.     <asp:BoundField DataField="ExpenseAmt" HeaderText="ExpenseAmt" />  
  6.     <asp:TemplateField HeaderText="Photo">  
  7.         <itemtemplate>  
  8.             <asp:Image ID="ReceiptImage" runat="server" Height="500px" Width="500px"  
  9.                 ImageUrl ='<%# "data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("Photo")) %>'>  
  10.    
  11.               
  12.         </itemtemplate>  


Answers (1)