question

Donald-0878 avatar image
0 Votes"
Donald-0878 asked Donald-0878 answered

File not showing from database

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

dotnet-csharpdotnet-aspnet-generaldotnet-aspnet-webformsdotnet-standarddotnet-aspnet-webpages
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Donald-0878 ,
According to your codes, I think your problems are because of this line:

      R esponse.Write(I mage3.I mageUrl = "d ata:image/ jpg;b ase64," + Convert.ToBase64String((byte[])dt.Rows[0]["image"]));

I suggest you check if get the image from the database.Please check if this is null.

 (byte[])dt.Rows[0]["image"])

Best regards,
Yijing Sun

0 Votes 0 ·

1 Answer

Donald-0878 avatar image
0 Votes"
Donald-0878 answered

I changed to this and it worked

 (Image3.ImageUrl = "d ata:image/ jpg;b ase64," + Convert.ToBase64String((byte[])dt.Rows[0]["image"]));
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.