I was trying to do an activity in which i want to show the image into a image control.
I am having a table in SQL server 2008 :
Table name:employee_detail
Columns:
[Emp_id] [varchar](50) NOT NULL,
[Emp_init] [varchar](50) NOT NULL,
[Emp_fname] [varchar](200) NOT NULL,
[Emp_lname] [varchar](200) NOT NULL,
[Emp_add1] [varchar](200) NULL,
[Emp_add2] [varchar](200) NULL,
[Emp_telephone] [varchar](10) NULL,
[Emp_mobile] [varchar](10) NULL,
[Emp_email] [varchar](100) NULL,
[Emp_gender] [varchar](1) NULL,
[DOB] [datetime] NULL,
[Image1] [image] NULL
I inserted the values into this table. But when i am trying to retrieve the image from this table then my code is running fine but no image is fetch
My Handler file is as follows:
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public class Handler : IHttpHandler {
connectionclass cs = new connectionclass();
public void ProcessRequest (HttpContext context)
{
if (context.Request.QueryString["id"] != null)
{
string constr = ConfigurationManager.ConnectionStrings["test_empConnectionString"].ConnectionString;
cs.con = new System.Data.SqlClient.SqlConnection();
cs.con.Open();
cs.cmd = new SqlCommand("select Image1 from employee_detail where Emp_id=@Emp_id;", cs.con);
cs.cmd.Parameters.AddWithValue("@Emp_id", context.Request.QueryString["Emp_id"].ToString());
cs.dr = cs.cmd.ExecuteReader();
cs.dr.Read();
context.Response.BinaryWrite((byte[])cs.dr["Image1"]);
cs.dr.Close();
cs.con.Close();
}
else
{
context.Response.Write("No Image Found");
}
// context.Response.ContentType = "text/plain";
// context.Response.Write("Hello World");
}
public bool IsReusable {
get {
return false;
}
}
}
My aspx.cs file is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Login_screen : System.Web.UI.Page
{
connectionclass cs = new connectionclass();
protected void Page_Load(object sender, EventArgs e)
{
string usr = (string)(Session["username"]);
cs.con = new SqlConnection(ConfigurationManager.ConnectionStrings["test_empConnectionString"].ConnectionString);
cs.cmd = new SqlCommand("Select Emp_fname +' '+Emp_lname from Employee_detail where Emp_id =(Select Emp_id from login1 where Username='"+usr+"')",cs.con);
cs.con.Open();
lblUsername.Text= Convert.ToString(cs.cmd.ExecuteScalar());
cs.con.Close();
cs.con = new SqlConnection(ConfigurationManager.ConnectionStrings["test_empConnectionString"].ConnectionString);
cs.con.Open();
cs.cmd = new SqlCommand("Select * from employee_detail where Emp_id =(Select Emp_id from login1 where Username='" + usr + "')", cs.con);
cs.dr = cs.cmd.ExecuteReader();
cs.dr.Read();
if(cs.dr.HasRows)
{
UserImage.ImageUrl = "~/Handler.ashx?Emp_id=" + usr;
}
cs.con.Close();
Bookmarks