Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have used ashx file and its code is below.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;

namespace tables
{
    /// <summary>
    /// Summary description for Handler1
    /// </summary>
    public class Handler1 : IHttpHandler
    {
        SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");


        public void ProcessRequest(HttpContext context)
        {
            string sql = "select CompanyLogo from Companies";
            SqlCommand command = new SqlCommand(sql, con);
            SqlDataReader dReader = command.ExecuteReader();
            dReader.Read();
            context.Response.BinaryWrite((byte[])dReader["~/CompanyLogo/"]);
            dReader.Close();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


I have used image control to display image. datatype of image path in database column is nvarchar(max). its code is

C#
namespace tables
{
    public partial class CompanyProfile : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Convert.ToInt16(Session["CompanyID"].ToString()) > 0)
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from Companies where CompanyID=" + Session["CompanyID"], con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
              
              
                DataSet ds = new DataSet();
                 
                da.Fill(ds, "Companies");
              
                if (ds.Tables[0].Rows.Count > 0)
                {
                   

                    Image1.ImageUrl = "Handler1.ashx";
                  

                }
            }
        }
    }
}
Posted

1 solution

Firstly why you are you using handler? you can do this directly

C#
if (ds.Tables[0].Rows.Count > 0)
     {
      Image1.ImageUrl ="~/"+ ds.Tables[0].Rows[0]["CompanyLogo "].ToString();
     }

By using handler

In page add company id in qyerystring

C#
Image1.ImageUrl = "Handler1.ashx?Companyid="Convert.ToInt16(Session["CompanyID"].ToString());


In hander change you sql query as follows

C#
public void ProcessRequest(HttpContext context)
        {
            string sql = @"select CompanyLogo from Companies where CompanyID=" + context.Request.QueryString["Companyid"].ToString();
            SqlCommand command = new SqlCommand(sql, con);
            SqlDataReader dReader = command.ExecuteReader();
            dReader.Read();
context.Response.ContentType = "image/png";
context.Response.WriteFile("~/" + dReader["CompanyLogo"].ToString());            
            dReader.Close();
        }
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900