Click here to Skip to main content
14,303,742 members
Rate this:
Please Sign up or sign in to vote.
I am having trouble retrieving image from db.
Here's my table structure:
create table table1
(
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 handler's code: I am retrieving multiple images

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();
            string pid = ID.ToString();
            string st = "select top 1 image1 from table1 where category='@tae'";
            SqlCommand cmd = new SqlCommand(st, con);
            cmd.Parameters.Add("@tae", SqlDbType.VarChar, 30).Value = pid;
            cmd.Prepare();
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            try
            {
                context.Response.ContentType = "jpeg";
                context.Response.BinaryWrite((byte[])dr["image1"]);
                dr.Close();

            }

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

}


and im calling the image using this code:
image1.imageurl="~/imhandler.ashx?pid=Children Books"


Can u please tell where am i going wrong??

Thanks


I am not getting any error message but the image is not shown in the page designted..
I have tried many times but i am not finding any fault
Posted
Updated 27-May-13 2:05am
v2
Comments
Mahesh Bailwal 27-May-13 7:15am
   
are you getting some error?
Swayam231 27-May-13 7:53am
   
the image is not shown in the page.
Sunasara Imdadhusen 27-May-13 7:57am
   
Hey...are you getting any error? please mentioned over here so we can try to give best solution
Swayam231 27-May-13 8:01am
   
No errors are thrown but the image is not shown in the page
Rate this:
Please Sign up or sign in to vote.

Solution 1

Change the below query
string st = "select top 1 image1 from table1 where category='@tae'";

to
string st = "select top 1 image1 from table1 where category=@tae";

and try once.

If it will not work, then debug and see what is the query which is being executed in the handler.
Comments
Swayam231 27-May-13 8:10am
   
Do i have to change the calling part also:

image1.imageurl="~/imhandler.ashx?pid=Children Books"????
   
No. Not for now.
Swayam231 27-May-13 8:13am
   
thanks tadit!!!! It works fine now
even two of these '' can ruin a well coded project!!!! lol
   
Yes... :D :D
My pleasure. Most Welcome Swayam.

Thanks for accepting and up-voting the answer.
Swayam231 27-May-13 8:21am
   
hey one more ques.. Can the same code be used for retrieving from different tables like

"Select top 1 image1 from @tablename";

do tell what to change in coding??
   
So, change the query and add parameter for @tablename as you are doing previously.
Swayam231 27-May-13 8:26am
   
what is the datatype for table name?
   
Sorry, table names can't be sent as parameter. It will also make injection attacks. Please do it carefully. If you want to do it, then you just need to replace using string replace method like below.

string tableName = "MyTable";
string st = "Select top 1 image1 from @tablename";
st.Replace("@tablename", tableName);
Swayam231 27-May-13 8:43am
   
if thats the case what should i write here:

image1.imageurl="--------"; ?
   
First you are getting the image by category. So, you are passing the Category to handler.

Now as you are directly getting it from table name, so pass the table name. And replace that in query.
Swayam231 27-May-13 8:22am
   
and how to call it in the page??
Rate this:
Please Sign up or sign in to vote.

Solution 2

First check your data reader is gettting the image value or not.
If getting the image value then try this,

context.Response.ContentType = "image/jpeg";
   
Comments
Swayam231 27-May-13 8:24am
   
it is working fine problem was in '@tae'..

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100