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:
public class Expense - {
- public string ID { get; set; }
- public DateTime StartTimeStamp { get; set; }
- public double ExpenseAmt { get; set; }
- public byte[] Photo { get; set; }
- }
My Button click event along with method:
- protected void Button1_Click(object sender, EventArgs e)
- {
- List<expense> exps = getSQLData();
- GridViewImage.DataSource = exps;
- GridViewImage.DataBind();
- }
-
- public List<expense> getSQLData()
- {
- List<expense> Expenses = new List<expense>();
- string cs = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
-
- using (SqlConnection conn = new SqlConnection(cs))
- {
- SqlCommand cmd = new SqlCommand("Select ID, Start_Timestamp, Expense_Amt, Receipt_Photo from EXPENSE where id='testing'", conn);
- conn.Open();
- SqlDataReader rdr = cmd.ExecuteReader();
- while (rdr.Read())
- {
- Expense exp = new Expense();
-
- exp.ID = rdr[0].ToString();
- exp.StartTimeStamp = Convert.ToDateTime(rdr[1].ToString());
- exp.ExpenseAmt = Convert.ToDouble(rdr[2].ToString());
- exp.Photo = (byte[])rdr[3];
-
- Expenses.Add(exp);
-
- }
- }
-
- return Expenses;
- }
My Gridview code in my aspx file:
- <asp:GridView ID="GridViewImage" runat="server" AutoGenerateColumns="false">
- <columns>
- <asp:BoundField DataField="ID" HeaderText="EnterpriseID" />
- <asp:BoundField DataField="StartTimeStamp" HeaderText="StartTimeStamp" />
- <asp:BoundField DataField="ExpenseAmt" HeaderText="ExpenseAmt" />
- <asp:TemplateField HeaderText="Photo">
- <itemtemplate>
- <asp:Image ID="ReceiptImage" runat="server" Height="500px" Width="500px"
- ImageUrl ='<%# "data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("Photo")) %>'>
-
-
- </itemtemplate>