Generic handler returns no value
The handler does not return an image. If I remove the conditional statement, the handler returns an image. This is my code
public void ProcessRequest(HttpContext context)
{
string sid = "JUN15MBACHN001";
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
connection.Open();
SqlCommand command = new SqlCommand("select suppassportphoto from studdetails where sregno=" + sid, connection);
SqlDataReader dr = command.ExecuteReader();
dr.Read();
Byte[] br = (Byte[])dr[0];
if (br.Length > 1)
{
context.Response.BinaryWrite((Byte[])dr[0]);
}
else
{
string path = context.Server.MapPath("~/image/emptymalepic.jpg");
byte[] byteArray = File.ReadAllBytes(path);
context.Response.BinaryWrite(byteArray);
}
connection.Close();
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
I don't know where am I going wrong? Any help would be appreciated.
+3
source to share
3 answers
First of all, I recommend distinguishing another way, because your code might throw an invalid casting exception:
Byte[] br = dr[0] as Byte[];
Then check for null
if (br != null && br.Length > 1)
Then write the file:
context.Response.ContentType = "image/jpeg"; // or whatever type you're using
context.Response.BinaryWrite(br);
And replace
context.Response.End();
from
HttpContext.Current.ApplicationInstance.CompleteRequest();
Since I noticed that somehow some browsers don't like Response.End ()
Hope this is helpful. Good luck!
+1
source to share
As I can see, in your code br.Length is always less than 1
Try this code snippet link
{
SqlConnection con = new SqlConnection("Server=Darkover;uid=<username>;pwd=<strong password>;database=northwind");
SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("MyImages");
byte[] MyData= new byte[0];
da.Fill(ds, "MyImages");
DataRow myRow;
myRow=ds.Tables["MyImages"].Rows[0];
MyData = (byte[])myRow["imgField"];
int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0);
FileStream fs = new FileStream(@"C:\winnt\Gone Fishing2.BMP", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0,ArraySize);
fs.Close();
}
0
source to share
try it
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest Dim _ConnectionString As String = ConfigurationManager.AppSettings("ConnectionString") Dim UserId As String = context.Request.QueryString("Id") Dim con As New SqlConnection(_ConnectionString) Try con.Open() Dim cmd As New SqlCommand(Convert.ToString("select UserPhoto from tblEmployee where EmpId=") & UserId, con) Dim dr As SqlDataReader = cmd.ExecuteReader() dr.Read() If Not dr.IsDBNull(0) Then context.Response.BinaryWrite(DirectCast(dr(0), Byte())) Else Dim imgpath As String = context.Server.MapPath("~/images/images.jpg") Dim byteArray As Byte() = File.ReadAllBytes(imgpath) context.Response.BinaryWrite(byteArray) End If Catch ex As Exception Throw ex End Try End Sub
0
source to share