sir m trying to bind data and image from database table to listview control but getting error i have used a handler class also to retirve image but not able to bind my data pls help....thanks in advance..
getting this error..
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'P_ID'.
Databse Design:
create Table tbl_post_property_sale
(
P_ID int identity(1,1)primary key,
Property_Type varchar(50)not null,
City_Id int references tbl_City(City_Id) not null,
Locality_Id int references tbl_Locality(Locality_Id)not null,
[Address] varchar(200)null,
Pincode numeric null,
Price varchar(20) not null,
Bedrooms int null,
Build_up_area varchar(20) not null,
Key_Features varchar(200)null,
Property_Image image not null,
Name varchar(50)not null,
Email varchar(50)not null,
Mobile_No numeric not null
)
code behind:
public partial class Search_Result : System.Web.UI.Page
{
string str = ConfigurationManager.ConnectionStrings["strconn"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!(IsPostBack))
{
Bindfiles();
}
}
private void Bindfiles()
{
DataTable table = new DataTable();
SqlConnection conn = new SqlConnection(str);
string sql = "Select Property_Type,City_Id,Locality_Id,Price,Build_up_area,Bedrooms,Property_Image From tbl_post_property_sale Order By P_ID asc";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(table);
listview1.DataSource = table;
listview1.DataBind();
}
}
Handler code:
<%@ WebHandler Language="C#" Class="ShowImage" %>
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public class ShowImage : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
if (context.Request.QueryString["P_ID"] == null) return;
string str = ConfigurationManager.ConnectionStrings["strconn"].ToString();
string property_ID = context.Request.QueryString["P_ID"];
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("Select Property_Type,City_Id,Locality_Id,Price,Build_up_area,Bedrooms,Property_Image From tbl_post_property_sale where P_ID = @Porperty_Id", con);
cmd.Parameters.Add(new SqlParameter("@Porperty_Id", property_ID));
con.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
dr.Read();
context.Response.BinaryWrite((Byte[])dr[dr.GetOrdinal("Property_Image")]);
dr.Close();
}
public bool IsReusable {
get {
return false;
}
}
}
Form with listview:
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="listview1" runat="server">
<ItemTemplate>
<table cellspacing="4" class="style1">
<tr>
<td class="style2">
<asp:Label ID="lblpropid" runat="server">
<%#Eval("P_ID")%>
</asp:Label>
</td>
<td rowspan="8">
<asp:Image ID="Image1" ImageUrl='<%# "~/Handlers/ImageHandler.ashx?Property_Id="+Eval("P_ID")%>'
BorderWidth="2px" Height="80px" Width="80px" runat="server" />
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="lblproptype" runat="server">
<%#Eval("Property_Type") %>
</asp:Label>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="lblcity" runat="server">
<%#Eval("City_Id")%>
</asp:Label>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="lbllocality" runat="server">
<%#Eval("Locality_Id")%>
</asp:Label>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="lblprice" runat="server">
<%#Eval("Price")%>
</asp:Label>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="lblarea" runat="server">
<%#Eval("Build_up_area")%>
</asp:Label>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="lblbedrooms" runat="server">
<%#Eval("Bedrooms")%>
</asp:Label>
</td>
</tr>
<tr>
<td class="style2">
<asp:Button class="btn" ID="btnviewdetails" runat="server" Text="ViewDetails" Width="79px" />
</td>
</tr>
<tr>
<td class="style2">
</td>
</tr>
</table>
</ItemTemplate>
<AlternatingItemTemplate>
<table cellspacing="4" bgcolor="yellow" class="style1">
<tr>
<td class="style2">
<asp:Label ID="lblpropid" runat="server">
<%#Eval("P_ID") %>
</asp:Label>
</td>
<td rowspan="8">
<asp:Image ID="Image1" ImageUrl='<%# "~/Handlers/ShowImage.ashx?P_ID="+Eval("P_ID")%>'
BorderWidth="2px" Height="80px" Width="80px" runat="server" />
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="lblproptype" runat="server">
<%#Eval("Property_Type") %>
</asp:Label>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="lblcity" runat="server">
<%#Eval("City_Id")%>
</asp:Label>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="lbllocality" runat="server">
<%#Eval("Locality_Id")%>
</asp:Label>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="lblprice" runat="server">
<%#Eval("Price")%>
</asp:Label>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="lblarea" runat="server">
<%#Eval("Build_up_area")%>
</asp:Label>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="lblbedrooms" runat="server">
<%#Eval("Bedrooms")%>
</asp:Label>
</td>
</tr>
<tr>
<td class="style2">
<asp:Button class="btn" ID="btnviewdetails" runat="server" Text="ViewDetails" Width="79px" />
</td>
</tr>
<tr>
<td class="style2">
</td>
</tr>
</table>
</AlternatingItemTemplate>
</asp:ListView>
</form>
</body>