Problem with loading image from database in ASP.NET Webforms

Viewed 57

I have a problem with reading and previewing image from my database. I tried quite several things, search a lot here, but still it won't work. I debugged, value from database is taken, there are no errors, but the image won't appear. Bellow is my code, if anyone has idea what is the problem, I would be very grateful!

This is how the image is uploaded

public bool Upload_Front_Logo(string company_name)
    {
        upload_front_logo = true;
        try
        {
            if (btnUploadLogo.PostedFile.ContentLength > 0)
            {
                string fileName = Path.GetFileName(btnUploadLogo.PostedFile.FileName);
                string fileExtension = Path.GetExtension(btnUploadLogo.PostedFile.FileName);

                if (fileExtension != ".png")
                {
                    string checkFolderNameCompany = "Uploads" + "\" + "templates" + "\" + company_name + "\";
                    string fileSavePath = Server.MapPath(checkFolderNameCompany);
                    if (!Directory.Exists(fileSavePath))
                        Directory.CreateDirectory(fileSavePath);

                    string serverFilePath = fileSavePath + fileName;
                    btnUploadLogo.PostedFile.SaveAs(serverFilePath);
                }
                else
                {
                    upload_front_logo = false;
                }
            }
            else
            {
                upload_front_logo = true;
            }
        }
        catch
        {
            upload_front_logo = false;
        }

        return upload_front_logo;
    }
    <asp:Image ID="logoFrontPreview" Height="150" Width="250"  runat="server"/><br />

And this is how I try to read it from my DB

public DataTable ReadingJsonb_GetCompanyData(string id)
    {
        DataTable dt = new DataTable();
        string query = @"SELECT data ->>  'Id' as id,
                                data ->>  'FrontLogo' as frontLogo 
        FROM companies WHERE data ->> 'Id' = '" + id + "' ";

        using (NpgsqlConnection conn = new NpgsqlConnection(ConnectionString.postgreSQLConnectionString))
        {
            conn.Open();
            using (NpgsqlCommand cmd = new NpgsqlCommand(query, conn))
            {
                using (NpgsqlDataAdapter ad = new NpgsqlDataAdapter(cmd))
                {
                    ad.Fill(dt);
                }
            }

            conn.Close();
        }
        return dt;
    }

  public void Get_Company(string id)
    {
        DataTable dt = ReadingJsonb_GetCompanyData(id);
        byte[] LogoFront = (byte [])dt.Rows[0]["logoFront"];
        string strBase64_logo_front = Convert.ToBase64String(LogoFront);
        //Session["LogoFront"] = strBase64_logo_front;
        logoFrontPreview.ImageUrl = "data:Image/png;base64," + strBase64_logo_front;
    }
0 Answers
Related