Click here to Skip to main content
15,888,323 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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:

C#
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.NET
<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>
Posted
Updated 8-Feb-16 5:22am
v2
Comments
Richard Deeming 8-Feb-16 12:03pm    
Sounds like you didn't store the images properly. When you debug, does the photo field contain the following 13 bytes?
83, 121, 115, 116, 101, 109, 46, 66, 121, 116, 101, 91, 93
eswarasuresh 8-Feb-16 16:01pm    
@Richard, Thanks for your comments. when i am testing for one of the image from database, photo byte array size 28716. first 13 bytes contains below values.

125,247,124,125,247,180,211,77,116,225,174,58,227
Richard Deeming 9-Feb-16 7:23am    
That's very odd - a JPEG image would start with:
255, 216, 255

I can't find any file type that would start with those bytes.

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