Sunny Chan

Sunny Chan

  • NA
  • 5
  • 5k

Retrieve file from database

Jul 27 2011 1:41 AM
%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
public class Handler : IHttpHandler
{
   const string Con_string = @"Data Source=DESKTOP10\SQLEXPRESS;Initial Catalog=Office;Integrated Security=True";
   public void ProcessRequest(HttpContext context)
   {
       context.Response.ContentType = "application/msword";
       SqlConnection con = new SqlConnection(Con_string);
       SqlCommand cmd = new SqlCommand("Select * from Resource where id=@id");
       cmd.Parameters.AddWithValue("@id", context.Request["id"]);
       using (con)
       {
           con.Open();
           byte[] file = (byte[])cmd.ExecuteScalar();
           context.Response.BinaryWrite(file);
       }
    }
   public bool IsReusable
   {
       get
       {
           return false;
       }
   }
}


I got below error. What should i do to solve this?



Answers (2)