Shalini Shines

Shalini Shines

  • NA
  • 1
  • 623

How to Connect two Generic Handler c# asp.net

Apr 21 2015 1:58 AM
Generic_Handler.ashx
<%@ WebHandler Language="C#" Class="Generic_Handler" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
public class Generic_Handler : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
string id = context.Request.QueryString["ID"];
byte[] bytes;
string FileType;
string strConnString = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Video, FileType from TBL_YouTube where ID=@ID";
cmd.Parameters.AddWithValue("@ID", id);
cmd.Connection = con;
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
bytes = (byte[])sdr["Video"];
FileType = sdr["FileType"].ToString();
con.Close();
}
}
context.Response.Clear();
context.Response.Buffer = true;
context.Response.AppendHeader("Content-Disposition", "attachment; ID=" + id);
context.Response.ContentType = FileType;
context.Response.BinaryWrite(bytes);
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
 
Search.ashx 
 
<%@ WebHandler Language="C#" Class="Search" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Text;
public class Search : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
string searchText = context.Request.QueryString["q"];
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Videos;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select FileName from TBL_YouTube where FileName Like @Search + '%'", con);
cmd.Parameters.AddWithValue("@Search", searchText);
StringBuilder sb = new StringBuilder();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
sb.Append(string.Format("{0},{1}", dr["FileName"], Environment.NewLine));
}
}
con.Close();
context.Response.Write(sb.ToString());
}
public bool IsReusable
{
get
{
return false;
}
}
}