Click here to Skip to main content
15,891,993 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using generic handler to retrieve a single image from sql db. Can this handler retrieve multiple images from a table???
here is my table structure:
create table book1
(
Row_id int not null IDENTITY,
name varchar(20) not null primary key,
author varchar(10),
category varchar(30),
price varchar(10),
details varchar(1000),
qty varchar (20),
imagename varchar(100),
image1 image
)


here's the code i am using in the handler:
public class imHandler : IHttpHandler 
{
    public void ProcessRequest (HttpContext context)
    {
        string strConn = " ....";
        SqlConnection con = new SqlConnection(strConn);
        con.Open();
        try
        {
            string st = "select top 1 image1 from book1 order by Row_id desc";
            SqlCommand cmd = new SqlCommand(st, con);
            SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            if (sdr.Read())
                
            {
                byte[] imgByte = (byte[])sdr["image1"];
                MemoryStream memoryStream = new MemoryStream();
                memoryStream.Write(imgByte, 0, imgByte.Length);
                System.Drawing.Image imagen = System.Drawing.Image.FromStream(memoryStream);
                context.Response.BinaryWrite(imgByte);
                
                
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
        }


and finally:

btn1_click()
{
image1.imageurl="~/imhandler.ashx"
}

Can handler return multiple images???
Posted
Comments
Swayam231 23-May-13 14:05pm    
The last code is written in another page default.aspx having few imagecontrols and buttons.
ZurdoDev 23-May-13 14:23pm    
Because you are writing bytes to the Response object, I doubt you can. I would recommend just writing a function instead of using a handler.
Swayam231 23-May-13 14:34pm    
can u please give an example for that?? using the above data
ZurdoDev 23-May-13 14:38pm    
Is button1_click just supposed to fill in values? Can you give more details? Based on your other post it looks like you are getting other values back (name, account, etc) so have a function that fills all that in for you.
Swayam231 23-May-13 14:40pm    
<pre>
protected void Button1_Click(object sender, EventArgs e)
{

string strConn = "Data Source=SWAYAM;Initial Catalog=Swayam;Integrated Security=SSPI; ";
SqlConnection con = new SqlConnection(strConn);
con.Open();
string st = "select top 1 name,author,price,details from image3 order by Row_id desc";
SqlCommand cmd = new SqlCommand(st, con);
SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
string name = "";
string author = "";
string price = "";
string details = "";
if (sdr.Read())
{
name = sdr["name"].ToString();
author = sdr["author"].ToString();
price = sdr["price"].ToString();
details = sdr["details"].ToString();
}

Label1.Text = name;
Label2.Text = price;
Label3.Text = author;
Label4.Text = details;
}

}
</pre>
Here's wht is getting me my values from db

There is no way to get multiple images from database using image handler , try store imagepath in db and image in folder.. It 100% works.
 
Share this answer
 
So it seems bit difficult to solve but still i have done it myself:
Here are the working codes:

Handler:
//retrieving multiple images based on their Row_id

public class imHandler : IHttpHandler 
{
    public void ProcessRequest(HttpContext context)
    {
        string strConn = "//connection string ";
        SqlConnection con = new SqlConnection(strConn);
        con.Open();
        if (context.Request.QueryString["pid"] != null)
        {

            string ID = context.Request.QueryString["pid"].ToString();
            int pid = Convert.ToInt32(ID);
            //the command will retrieve images based on rows

            string st = "select image1 from table1 where Row_id=@row";
            SqlCommand cmd = new SqlCommand(st, con);
            cmd.Parameters.Add("@row", SqlDbType.Int).Value = pid;
            cmd.Prepare();
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            try
            {
                context.Response.ContentType = "jpeg";//use ur image type
                context.Response.BinaryWrite((byte[])dr["image1"]);
                dr.Close();

            }

            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
        }
    }
    public bool IsReusable 
    {
        get {
                return false;
            }
    }

}


And here's how to call the images, i have written them in page_
load event u can use btn_click also!!!
Take image controls in ur page and write the codes below:

 protected void Page_Load(object sender, EventArgs e)
    {
        image1.imageurl="handlername.ashx?pid=1";
        image2.imageurl="handler.ashx?pid=2";
       // and so on......
      }
the pid here is the variable u have declared in the handler
 
Share this answer
 
Comments
NitishBharadwaj 10-Oct-13 15:30pm    
There is no way to get multiple images from database using image handler , try store imagepath in db and image in folder.. It 100% works.

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