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

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;
}
}
}
4 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

Deepak RatanPosted May 20, 2015, 7:43 AM
Nanhe SiddiquePosted May 20, 2015, 7:30 AM
Deepak RatanPosted May 20, 2015, 7:19 AM
Nanhe SiddiquePosted May 20, 2015, 7:05 AM
Cause
The above exception occurs when a specified data item in the web control does not exist in the schema of the returned DataRowView. In other words the column that you have specified with the databound control is not present in the data returned from database
Solution
1. Generally occurs due to misspelled column names, hence one should recheck that.
2. If the column names in the Control is correct the check what columns your query is returning and the column that you have specified to the control exists or not.
3. Many times due to exceptions nothing is returned in that case check the code for such issues.
Examples
1. If one specifies DataKeyNames property as ID and the actual column name is CustomerID. It will throw the above error.
2. If one specifies DataTextField or DataValueField property as ID and the actual column name is CustomerID. It will throw the above error.
Overall it is a minor issue and can be easily solved using the above steps