locked
Hiding an empty image tag/holder when the Access cell is empty RRS feed

  • Question

  • User-2116945077 posted

    Sometimes, a book has different covers (during reprinting etc.), but others only have one cover, no matter how many times it has been reprinted.  

    My project is connected to an external Access database that has 3 columns, ("Id" Autonumber (as well as my Primary Key), ImageUrl (OLEDB object), and Image2 (OLEDB object)

    On my aspx.net page,   I've have a databound Datalist that contains a label tag and two image tags.     Now, my question is: If the cell in the second column of my database is empty after retrieving the data, how can I hide the Datalist's second image tag when debugging and have it continue to the next record without having an exception thrown because the cell is empty?<o:p></o:p>

    I already have the code to retrieve the images ( a kind person went through a lot of trouble to help me with this)

    aspx.net.cs side: (this is the coding that was given to me)

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    OleDbConnection conn = new OleDbConnection(@"Provider = Microsoft.ACE.OLEDB.12.0; Data Source =C:\Users\Anja\Documents\Database3.accdb");
                    conn.Open();
                    OleDbCommand command = new OleDbCommand("select * from boekt", conn);
                    OleDbDataReader oleDbDataReader = command.ExecuteReader();
                    this.DataList1.DataSource = oleDbDataReader;
                    this.DataList1.DataBind();
                    oleDbDataReader.Close();
                    conn.Close();
                }
            }
    
    
            protected string Handle(object ImageUrl)//converting the object to bytes
            {
               
                    byte[] data = GetOriginal(ImageUrl as byte[]);
                    MemoryStream stream = new MemoryStream();
                    stream.Write(data, 0, data.Length);
                    System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
    
                    byte[] imgdata = imageToByteArray(img);
                    string imageurl = string.Empty;
                     
                    using (MemoryStream ms = new MemoryStream(imgdata))
                    {
                        imageurl = "data:image/jpg;base64," + Convert.ToBase64String(ms.ToArray());
                    }
                    return imageurl;
                }
                  
             
            }
    
            private byte[] GetOriginal(byte[] dbbytes)//removing the header that Access adds to OLEDB objects
            {
                const string BITMAP_ID_BLOCK = "BM";
                const string JPG_ID_BLOCK = "\u00FF\u00D8\u00FF";
                const string PNG_ID_BLOCK = "\u0089PNG\r\n\u001a\n";
                const string GIF_ID_BLOCK = "GIF8";
                const string TIFF_ID_BLOCK = "II*\u0000";
                byte[] imgbytes;
                System.Text.Encoding e7 = System.Text.Encoding.UTF7;
                String strtemp = e7.GetString(dbbytes);
                String strfirsttemp = strtemp.Substring(0, 300);
                int iPos = -1;
                if (strfirsttemp.IndexOf(BITMAP_ID_BLOCK) != -1)
                    iPos = strfirsttemp.IndexOf(BITMAP_ID_BLOCK);
                else if (strfirsttemp.IndexOf(JPG_ID_BLOCK) != -1)
                    iPos = strfirsttemp.IndexOf(JPG_ID_BLOCK);
                else if (strfirsttemp.IndexOf(PNG_ID_BLOCK) != -1)
                    iPos = strfirsttemp.IndexOf(PNG_ID_BLOCK);
                else if (strfirsttemp.IndexOf(GIF_ID_BLOCK) != -1)
                    iPos = strfirsttemp.IndexOf(GIF_ID_BLOCK);
                else if (strfirsttemp.IndexOf(TIFF_ID_BLOCK) != -1)
                    iPos = strfirsttemp.IndexOf(TIFF_ID_BLOCK);
                else
                    throw new Exception("Unable to determine header size for the OLE Object");
                if (iPos == -1)
                    throw new Exception("Unable to determine header size for the OLE Object");
                MemoryStream stream = new MemoryStream();
                stream.Write(dbbytes, iPos, dbbytes.Length - iPos);
                imgbytes = stream.ToArray();
                stream.Close();
                stream.Dispose();
                return imgbytes;
            }
    
            public byte[] imageToByteArray(System.Drawing.Image imageIn)
            {
                MemoryStream ms = new MemoryStream();
                imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                return ms.ToArray();
            }

    and the aspx.net side (I've already added the second image tag

    <asp:DataList ID="DataList1" runat="server">
                    <ItemTemplate>
                        ID:<asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                        <asp:Image ID="Image1" runat="server" ImageUrl='<%# Handle(Eval("ImageUrl")) %>' Width="100px" Height="100px"/>
                        <asp:Image ID="Image2" runat="server" Image2='<%# Handle(Eval("Image2")) %>' Width="100px" Height="100px"/>
                    </ItemTemplate>

    Can someone please help me?

    Riddlesinmymind

    Monday, November 25, 2019 7:36 AM

Answers

  • User61956409 posted

    Hi riddlesinmymind,

    Thanks to your help, I was able to bypass the exception-problem.  

    Glad to hear that the solution does help resolve the issue.

    riddlesinmymind

    But how do I find that specific imagetag in the Datalist and hide or change its visibility if the  Access cell is empty.  In other words, when I debug the project, I do not want to see an outline of Image2 if its Access cell is empty.  

    You can specify Visible property of asp:Image control, like below.

    Visible='<%# Eval("ImageUrl") is System.DBNull ? false : true %>'

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 26, 2019 10:00 AM

All replies

  • User61956409 posted

    Hi riddlesinmymind,

    my question is: If the cell in the second column of my database is empty after retrieving the data, how can I hide the Datalist's second image tag when debugging and have it continue to the next record without having an exception thrown because the cell is empty?

    You can refer to the following code snippet to check if the ImageUrl field is empty.

    <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ImageUrl") is System.DBNull ? "" : Handle(Eval("ImageUrl")) %>' Width="100px" Height="100px"/>

    With Regards,

    Fei Han

    Tuesday, November 26, 2019 2:57 AM
  • User-2116945077 posted

    Hello Fei Han,

    Thanks to your help, I was able to bypass the exception-problem.  

    But how do I find that specific imagetag in the Datalist and hide or change its visibility if the  Access cell is empty.  In other words, when I debug the project, I do not want to see an outline of Image2 if its Access cell is empty.  

    Can you please help me?

    Riddlesinmymind

    Tuesday, November 26, 2019 9:46 AM
  • User61956409 posted

    Hi riddlesinmymind,

    Thanks to your help, I was able to bypass the exception-problem.  

    Glad to hear that the solution does help resolve the issue.

    riddlesinmymind

    But how do I find that specific imagetag in the Datalist and hide or change its visibility if the  Access cell is empty.  In other words, when I debug the project, I do not want to see an outline of Image2 if its Access cell is empty.  

    You can specify Visible property of asp:Image control, like below.

    Visible='<%# Eval("ImageUrl") is System.DBNull ? false : true %>'

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 26, 2019 10:00 AM
  • User-2116945077 posted

    Thank you!  It works like a charm!laughing

    Tuesday, November 26, 2019 10:48 AM