File not showing from database

Donald Symmons 2,856 Reputation points
2022-05-30T14:08:08.297+00:00

On redirecting to page, image file is supposed to show from database but instead it show an empty image control.
How do I fix this please?
<div class="shell" id="card" style="width: 90%; margin: 0 auto; padding: 10px;">
<asp:Panel ID="pnlContents" runat="server">
<div class="book" style="width: 100%; height: 100%;">
<div class="page" id="parent" style="width: 100%;">
<div class="subpage" style="width: 100%; height: 100%;">
<asp:Image ID="Image3" ImageUrl="~/img/background.png" runat="server" BorderStyle="none" />
</div>
</div>
</div>
</asp:Panel>
</div>

display.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Session["CardId"] != null)
{
BindCardAndQR();
}
}

    private void BindCardAndQR()
    {
        if (Request.QueryString["CardId"] != null)
        {
            int id = Convert.ToInt32(Session["CardId"]);

            string sql = "SELECT * FROM Table WHERE Id = @Id";
            SqlParameter[] parameters = new[] {
                    new SqlParameter("@Id", id)
                };

            // Bind Image3 for Card Image
            DataTable dt = SelectFromDatabase(sql, parameters);
            if (dt.Rows.Count > 0)
            {
                Response.Write(Image3.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])dt.Rows[0]["image"]));
                Image3.AlternateText = "The Image has been found";
            }
            else
            {
                Image3.ImageUrl = "#";
                Image3.AlternateText = "The Image does not exist in DataBase";
            }
        }
    }

    public static DataTable SelectFromDatabase(string sql, SqlParameter[] parameters)
    {
        using (SqlConnection con = new SqlConnection("myConStrng"))
        {

            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                if (parameters != null)
                {
                    cmd.Parameters.AddRange(parameters);
                }

                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,374 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,254 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,247 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Donald Symmons 2,856 Reputation points
    2022-06-06T05:27:41.073+00:00

    I changed to this and it worked

    (Image3.ImageUrl = "d ata:image/ jpg;b ase64," + Convert.ToBase64String((byte[])dt.Rows[0]["image"]));
    
    0 comments No comments