Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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:
SQL
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:
C#
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:
HTML
<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>
Posted
Updated 30-Sep-12 1:58am
v2

its all right
you are welcome
 
Share this answer
 
Dear,
in your select command:
that mean this

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

you did not select P_ID so <%#Eval("P_ID") %> doesnt get any value to be filled in

you need to replace you select command with the following

string sql = "Select P_ID, Property_Type,City_Id,Locality_Id,Price,Build_up_area,Bedrooms,Property_Image From tbl_post_property_sale Order By P_ID asc";
 
Share this answer
 
Comments
Raj.Rautela 30-Sep-12 9:15am    
i did what you told but image is no there only details are there
Raj.Rautela 30-Sep-12 9:21am    
thank u sir got it thanks a lot...
Raj.Rautela 30-Sep-12 11:31am    
sir i want to display city name and locality name not city id and locality id on display page it is showing city id and locality id m filling city and locality dropdown from diff table how to join..i'hv created a relation pk and fk pls help in sql joins
Dear,

you want to display what fields and where you want to display

for example if you want to display the city name and locality name from tbl_Locality and tbl_City tables along with the tbl_post_property_sale fields

do this select command:

SQL
Select P_ID, Property_Type,City_Name,Locality_Name,Price,Build_up_area,Bedrooms,Property_Image From tbl_post_property_sale,tbl_City,tbl_Locality where tbl_post_property_sale.City_Id=tbl_City.City_Id and tbl_post_property_sale.locaity_Id=tbl_Locality.locality_Id  Order By P_ID asc



For further explaination ask me ,,
 
Share this answer
 
v2
Comments
Raj.Rautela 30-Sep-12 13:10pm    
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'City_Id'. getting this error do i have to chage code in generic file also...
Inside your listView change the following:

<%#Eval("City_Id")%>
<%#Eval("Locality_Id")%>
TO
<%#Eval("City_Name")%>
<%#Eval("Locality_Name")%>
 
Share this answer
 
v2
Comments
Raj.Rautela 30-Sep-12 13:44pm    
got it sir thanks a lot......sir

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900