Click here to Skip to main content
11,410,750 members (62,616 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL-Server
Hi ,

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();
 

    }
    protected void btnlogout_Click(object sender, EventArgs e)
    {
        Session["username"] = "";
        Response.Redirect("Login_page.aspx");
    }
}


my aspx file is as follows:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="User_welcome_screen.aspx.cs" Inherits="Login_screen" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
    <style type="text/css">
        #welcome
        {
            height: 168px;
        }
    </style>
</head>
<body>
    <form id="form1"  runat="server">
    <div id="welcome">
    <table align="left">
    <tr>
    <td>
    <asp:Label ID="lblWelcome" Text="Welcome " runat="server"></asp:Label><asp:Label ID="lblUsername" runat="server"></asp:Label>
    </td>
    </tr>
    </table>
    <table align="right">
    <tr>
    <td>
        <asp:Button ID="btnlogout" runat="server" Text="Logout" 
            onclick="btnlogout_Click" />
    </td>
    <td></td><td></td><td></td>
    <td>
        <asp:Button ID="btnchngpass" runat="server" Text="Change Password" />
    </td>
    </tr>
    </table>
        <br />
        <br />
        
        <div>
        <table>
        <tr>
        <td>
        <asp:Image ID="UserImage" runat="server" ImageUrl='<%"~/Handler.ashx?Emp_ID="+Eval("Emp_ID")%>' />
        </td>
        </tr>
        </table>
        </div>
    <div align="left" style="height: 117px">
    
    <table align="left" rules="cols" 
            style="font-family: Arial, Helvetica, sans-serif; font-size: medium; font-weight: bold; table-layout: auto; border-collapse: separate">
    <tr>
        <td>
        <a href="Attend_leave.aspx">Attendence\Leave System</a>
            <br />
        </td>
    </tr>
    
    <tr>
        <td>
        <a href="Edit_employee_detail.aspx">Update Personal Details</a>
            <br />
        </td>
    </tr>
    
    <tr>
        <td>
        <a href="#">Mail to Admin</a>
            <br />
        </td>
    </tr>
    
    </table>
    </div>
    </div>
   
    
   
    </form>
</body>
</html>
Please help me on this!!!
Posted 2-Feb-13 1:24am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
preetpal kapoor at 2-Feb-13 7:07am
   
I tried your way but getting same result.... Dn't knw what to do??
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

i think u r problem is here...

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);

u r not setting connectionString property of con....

u have to do..
cs.con.ConnectionString=constr;
cs.con.Open();
  Permalink  
v2
Comments
preetpal kapoor at 2-Feb-13 11:51am
   
Hi Pallavi,

Thanks for your reply but i am getting same output by making the changes referred by you.
Pallavi waikar at 2-Feb-13 12:25pm
   
go through following link...it it help you a lot..... http://www.dotnetcurry.com/ShowArticle.aspx?ID=129....compare ur code with this code...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 239
1 Sascha Lefévre 85
2 Dave Kreskowiak 80
3 Maciej Los 68
4 RyanDev 50
0 Sergey Alexandrovich Kryukov 8,885
1 OriginalGriff 6,915
2 Maciej Los 3,390
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web01 | 2.8.150414.5 | Last Updated 2 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100