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;
}
}
}
}