Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Binding misunderstanding

Tags:

c#

asp.net

I have a problem and I need your help :

I am making a web application to access for a list of employee i need to show them in a tool like ( ListView or DataList ) to bind the data from the database direct and there is one field in the database is saved as image and I need to show that image with the details as i mentioned here enter image description here

then I create one ASP Handler to stream the images its takes query string ("code") and its return stream of bytes include "data:image/png;base64,image"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TimeSheet.DstTableAdapters;

namespace TimeSheet
{
    /// <summary>
    /// Summary description for emp_img
    /// </summary>
    public class emp_img : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string emp_code = context.Request.QueryString["code"];
            string output_img = "";
            string no_image = "i remove the image code from here ";
            string male = "i remove the image code from here ";

            if (emp_code == null || emp_code == "")
            {
                output_img = no_image;
            }
            else
            {

                try
                {
                    EmployeeTableAdapter adp = new EmployeeTableAdapter();
                    Dst.EmployeeDataTable all_emps = adp.get_by_code(emp_code);
                    foreach (Dst.EmployeeRow emp in all_emps)
                    {
                        if (emp.Data == null)
                        {
                            //here to add the users without image 
                            output_img = male;
                        }
                        else
                        {
                            byte[] bytes = emp.Data;
                            string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
                            output_img = "data:image/png;base64," + base64String;
                        }


                    }
                }
                catch (Exception)
                {

                    output_img = no_image;
                }



                context.Response.BinaryWrite(GetBytes(output_img));
                context.Response.End();

            }

        }

however when I tried to use that like that way in my code ....

        <asp:ListView ID="ListView1" runat="server" DataSourceID="myteamdata" ItemPlaceholderID="placeholder" OnItemInserting="ListView1_ItemInserting" >
            <LayoutTemplate>
                <div class="row">
                <div class="col-lg-12" id="placeholder" runat="server">
                </div>
                    </div>
                <asp:DataPager ID="DataPager1" runat="server" PageSize="12"></asp:DataPager>
            </LayoutTemplate>

            <ItemTemplate>
                <div class="col-lg-3">

                    <asp:Image ID="Image1" runat="server" ImageUrl='<%# "emp_img.ashx?code="+ Eval("Code") %>'/>

                </div>
            </ItemTemplate>

        </asp:ListView>



        <asp:ObjectDataSource ID="myteamdata" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="get_all_data" TypeName="TimeSheet.DstTableAdapters.EmployeeTableAdapter"></asp:ObjectDataSource>

it's not inserting the image its just keeping a url like enter image description here

so what is the solution please to handle the output comes from the data binding like ( bind or Eval ) in c# code ,, and how can i call method makeing some calculation in some class into the ASP Page in use that data bind result as an input to that methods

and thank you so much for the help in advance

like image 463
Meroo Maher Avatar asked Mar 28 '26 17:03

Meroo Maher


1 Answers

What your image handler return is a string,

   byte[] bytes = emp.Data;
   string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
   output_img = "data:image/png;base64," + base64String;

but what img can show is a real binary file/data

Change your Content Type to

context.Response.ContentType = "image/png";
context.Response.BinaryWrite(emp.Data);

and send your data as they are.

You have more bugs, your non exist image must be an image file on disk... and I do not know what else... the foreach on return the image is also not good if you found more than one image...

like image 191
Aristos Avatar answered Mar 30 '26 08:03

Aristos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!