opening pdf with .ashx generic handler

I used the code below in a .ashx generic handler page to display a pdf file.
The code below was retrieving a file that was saved in the database as a varbinary(max) datatype.

I want to do this again and the only difference this time is the file is saved in the database as an image datatype.  

Do I have to alter the code to retrieve data stored as an image datatype???

     public void ProcessRequest(HttpContext ctx)
        {
           
            string id = ctx.Request.QueryString["id"];

            SqlConnection con = new SqlConnection("connection string here");
            SqlCommand cmd = new SqlCommand("SELECT File FROM tblFiles where FileId = @fileID", con);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@fileId", id);
       
        con.Open();
        byte[] pict = (byte[])cmd.ExecuteScalar();

        con.Close();
        try
        {
            ctx.Response.ContentType = HttpContext.Current.Session["fileExtension"].ToString();
            ctx.Response.OutputStream.Write(pict, 0, int.Parse(pict.Length.ToString()));
        }
        catch
        {
           
        }
LATurkAsked:
Who is Participating?
 
anvCommented:
Also how are you storing the PDF file in DB?
0
 
LATurkAuthor Commented:
This is the error I am getting
Unable to cast object of type 'System.Int32' to type 'System.Byte[]'.

on this line of code
byte[] pict = (byte[])cmd.ExecuteScalar();
0
 
anvCommented:
what is the data type of File Field in database?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
LATurkAuthor Commented:
the pdf file is stored in an Image datatype.
0
 
LATurkAuthor Commented:
Actually, I'm closer now.  The actual select statement I was using selected 2 fields.  An id field and then the image field.  I took out the id field so i'm only selecting the image and I no longer get that error.

Now it opens in the ashx page but it's all garbled.
0
 
LATurkAuthor Commented:
I got it to work using the below:
Does it look good to you?  Anything important I missed?

       public void ProcessRequest(HttpContext ctx)
        {
           
            string id = ctx.Request.QueryString["id"];
           
            SqlConnection con = new SqlConnection("connection string");
            SqlCommand cmd = new SqlCommand("SELECT File FROM tblFiles where FileId = @fileID", con);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@fileId", id);
       
        con.Open();
        byte[] pict = (byte[])cmd.ExecuteScalar();
           
        con.Close();
       
        try
        {
           
            ctx.Response.ContentType = HttpContext.Current.Session["application/pdf"].ToString();
            ctx.Response.BinaryWrite(pict);
        }
           
        catch
        {
           
        }
        }
       
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
0
 
LATurkAuthor Commented:
I have one more question.

The users will click a button to go to the ashx url to view a pdf.
I have the users computer set to use Adobe Professional to open the pdf.
If the user adds comments to the pdf, is it possible to save the new copy back to the database?

I've done this in a windows application but not a web application.
0
 
LATurkAuthor Commented:
I am going to award points even though I ended up finding the solution.  After reading your questions, I ended up googling it in a different way which led me to the solution.  I will post my second question in a new post.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.