Deepak Ratan

Deepak Ratan

  • NA
  • 175
  • 50.9k

Getting an ERROR to display the photo in gridview

May 20 2015 6:39 AM
ERROR : DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'EmpID'.
 
hi i tried to display image from sql table to gridview but its not displaying this s my code to bind gridview with sql table records..
 
When the user login the Login user photo should display 
 
 DataBase:  TBL_PBLogin
 
EmpID         PK (1,1),
UName       varchar(50),
Password   varchar(50),
photo          image 
 
 
PostBookChat.aspx
 
<body>
      <form id="form1" runat="server">
            <div>
                  <div id="header">
                         <img src="Image/book.png" height="60" width="140" style ="margin-left:0px;float:left;"/>
                                 <div id="login">
                                          <b><asp:Label ID="lblsession" runat="server" ForeColor="white" CssClass="label"></asp:Label>
                                          <asp:GridView ID="gridviewphoto" runat="server" AutoGenerateColumns="false" BackColor="#CC3300"                                                  ForeColor="Black" ShowHeader="false" GridLines="None">
                                                
                                                      <Columns>
                                                              <asp:TemplateField ControlStyle-Width="100" ControlStyle-Height="100">
                                                                       <ItemTemplate>
                                                                                 <img src="Handler.ashx?EmpID=<%#Eval("EmpID").ToString()%>" width="50" height="50"/>
                                                                        </ItemTemplate>
                                                               </asp:TemplateField>
                                                      </Columns>
                                          </asp:GridView>
                              <asp:Button ID="btnlogout" runat="server" Text="Sign Out" CssClass="myButton" OnClick="btnlogout_Click"/>
                   </div>
         </div>
 
 
 
 
PostBookChat.aspx.cs
    
   protected void Page_Load(object sender, EventArgs e)
   {
         if (!IsPostBack)
         {
               lblsession.Text = "Welcome" + Convert.ToString(Session["UName"]);
               sessionimage();
         }
   }
   private void sessionimage()
   {
         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString);
         
         try
         {
               int EmpID = (int)Session["EmpID"];
               SqlDataAdapter Adp = new SqlDataAdapter("select Photo from TBL_PBLogin where EmpID='" + EmpID + "'", con);
               DataTable Dt = new DataTable();
               con.Open();
               Adp.Fill(Dt);
               gridviewphoto.DataSource = Dt;
               gridviewphoto.DataBind();
            con.Close();
         }
 
         catch (Exception ex)
         {
               throw new Exception(ex.ToString());
         }
   }
 
 
 Handler.ashx
 
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Collections.Specialized;
using System.Configuration;
   public class Handler : IHttpHandler
   {
         public void ProcessRequest(HttpContext context)
         {
               if (context.Request.QueryString["EmpID"] == null) return;
               string connStr = ConfigurationManager.AppSettings["connect"].ToString();
               string pictureId = context.Request.QueryString["EmpID"];
               using (SqlConnection conn = new SqlConnection(connStr))
               {
                     using (SqlCommand cmd = new SqlCommand("SELECT Photo FROm TBL_PBLogin WHERE EmpID = @EmpId", conn))
                     {
                           cmd.Parameters.Add(new SqlParameter("@EmpID", pictureId));
                           conn.Open();
                           using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                           {
                                 reader.Read();
                                 context.Response.BinaryWrite((Byte[])reader[reader.GetOrdinal("Photo")]);
                                 reader.Close();
                           }
                     }
               }
      }
      public bool IsReusable
      {
            get
            {
                  return false;
            }
      }
   }
 
 
 
 

Answers (4)