Click here to Skip to main content
15,886,689 members

Low Performance when useing Handler.ashx to show Photo.

Said Ali Jalali asked:

Open original thread
Hello. for showing photo in my site. i am using Handler(.NET 3.5) to read from DB a and show it in DataList. But when i uploaded the site photos are shown slow.
Please look at the site:

http://omed-ltd.com/[^]
omed-ltd.com

and the code that i am using:
<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Data.SqlClient;
public class Handler : IHttpHandler {

    SqlConnection myConnection;
    SqlCommand cmd;
    
    public Handler()
    {
        myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myDB"].ConnectionString);
        cmd = new SqlCommand("", myConnection);
        myConnection.Open();
    }
    public void ProcessRequest (HttpContext context) {
        try
        {
            string sql = "Select TPImage,id,Image_Type from TProduct where Id=@Id";
            cmd.CommandText = sql;
            cmd.Parameters.Add("@Id", System.Data.SqlDbType.Int).Value =    context.Request.QueryString["Id"];
            cmd.Prepare();
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            context.Response.ContentType = dr["Image_Type"].ToString();
            
            context.Response.BinaryWrite((byte[])dr["TPImage"]);
            //باید برای عکس یکی از این فایل ساخته شود تا که بتونی هر عکس رو جدا لود کرد.
            dr.Close();
            myConnection.Close();
        }
        catch (Exception ex)
        {
            //System.Windows.Forms.MessageBox.Show(ex.Message);
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

Thank you.
Tags: ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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