Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Plz help! I want get images from database with generic handler via business logic.

Business logic code
C#
 public List get_ballot()
       {
           csDAL objdal = new csDAL();
           List objlist = new List();
           IDataReader dr = null;
           dr = objdal.executespreturndr("get_ballot");
           while (dr.Read())
           {
               Ballot objc = new Ballot();
               populate_reader(dr, objc);
               objlist.Add(objc);
           }
           return objlist;
       }

       private void populate_reader(IDataReader objdr, Ballot objc)
       {
           objc.id = objdr.GetInt32(0);
           objc.imgName = objdr.GetString(1);
           objc.pic1 = (Byte[])objdr.GetValue(2);
           objc.pic2 = (Byte[])objdr.GetValue(3);
       }
    
}

generic handler code. How can i link this to the code above ?
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;

namespace SEARCH
{
    /// 

    /// Summary description for Grabimg
    /// 

    public class Grabimg : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            Int32 id;
            if (context.Request.QueryString["id"] != null) 
            {id=Convert.ToInt32 (context .Request .QueryString ["id"]);}
            else
            {throw new ArgumentException("No parameter specified");}
            context .Response .ContentType ="img/jpeg";
            System.IO.Stream strm = showImg(id);
            Byte[] buffer = new Byte[strm.Length];
            int bytesql = strm.Read(buffer, 0, buffer.Length);
            while (bytesql > 0) 
            {
                context.Response.OutputStream.Write(buffer, 0, bytesql);
                bytesql = strm.Read(buffer, 0, buffer.Length);
                strm.Close();
            }
           


            //public System .IO .Stream ShowImg(Int32 id)
            //{
            //BLL.Ballot objc=new BLL.Ballot ();
            //    objc.id=id;
            //    objc.imgName =
            //}
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
        }

        public System .IO .Stream ShowImg(int id)
            {
            BLL.Ballot objc=new BLL.Ballot ();
            List obji = new List();
                obji.id=id;
                obji  = obji.get_ballot();

            }

        private System.IO.Stream showImg(int id)
        {
           throw new NotImplementedException();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
Posted
Updated 15-Aug-11 8:45am
v3
Comments
Herman<T>.Instance 15-Aug-11 14:35pm    
what have you tried?
Ngqulunga 15-Aug-11 14:43pm    
Moved the code from the comment to the question
Herman<T>.Instance 15-Aug-11 14:51pm    
If I read your source: In the BLL you read from the database; in the Grabimg you read from the httpcontext. What are you trying to achieve? That the image is stored in the database or read from the database and shown on a webpage?

1 solution

Your data layer is a mess. Why do you need to create an instance every time ? Why does it return a reader ? Your data layer should HIDE the database, not just call it and return the object you need to iterate over. This code will always be the same, so it should be hidden. Why are you returning a List and not a generic container ?

I don't understand the question. Your presentation layer should call the business layer, which should call the data layer. Any parameters that define what data to return needs to be passed through. What are you trying to do and what is the issue ?
 
Share this answer
 

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