Click here to Skip to main content
14,699,081 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
here's the handler code:
public class imHandler : IHttpHandler 
    public void ProcessRequest(HttpContext context)
        string strConn = "//connection string";
        SqlConnection con = new SqlConnection(strConn);
        if (context.Request.QueryString["pid"] != null)

            string ID = context.Request.QueryString["pid"].ToString();
            string pid = ID.ToString();

            string tablename = pid;
            string st = "select top 1 image1 from @tae";
            st.Replace("@tae", tablename);
            SqlCommand cmd = new SqlCommand(st, con);

            cmd.Parameters.Add("@tae", SqlDbType.VarChar, 30).Value = pid;
            SqlDataReader dr = cmd.ExecuteReader();
                context.Response.ContentType = "jpeg";


            catch (Exception ex)
                throw ex;

    public bool IsReusable
            return false;


i am getting an error: table variable @tae must be declared

i am calling the image using this code
Image1.ImageUrl = "~/imHandler.ashx?pid=table1";

What is wrong in the code?? help

Can you debug and check where exactly it is throwing exception ?
Swayam231 27-May-13 12:00pm
here: SqlDataReader dr = cmd.ExecuteReader();
Swayam231 27-May-13 12:04pm
Error msg:
must declare the table variable @tae

1 solution

The below line in your code is not necessary. You can remove it.
cmd.Parameters.Add("@tae", SqlDbType.VarChar, 30).Value = pid;
Swayam231 27-May-13 12:02pm
still not working
What is the error now ?
Swayam231 27-May-13 13:42pm
same buddy no change
Now debug and find on which line it is throwing the exception ?
Swayam231 27-May-13 14:21pm
here: SqlDataReader dr = cmd.ExecuteReader();
Swayam231 27-May-13 14:22pm
must declare table variable @tae
Can you check in the command what is the query now ?
Hover on that and check while debugging.
Swayam231 27-May-13 14:32pm
how to check which query is executed?
Hover on cmd and check the CommandText property.
Swayam231 28-May-13 3:28am
the command text property on debugging shows

select top1 image1 from @tae;

i think @tae is not getting the value somehow
That means this value is not below code.

st.Replace("@tae", tablename);

Can you check after this line what is the value of st and what is the value of tablename ?

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