3

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

1 Answer 1

1

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...

Sign up to request clarification or add additional context in comments.

3 Comments

thank you so much its working fine now but i did not get for that not image i have one image on the disk also ,, and for foreach is not good what i can use of not thank you again its working now
@MerooMaher If you have image file on disk, you read it and send it the same way with your handler
@MerooMaher If the answer is help you and working please vote it and accept it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.