Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, can you please help me.

I am trying to bind the grid view in code behind, but the data is not displayed on the page. I checked the query it is working, the problem is displaying the data. here is the code. I am new to web programming

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  Width="100%">
                <Columns>
                    <asp:BoundField DataField="PropertyType" HeaderText="Property Type" SortExpression="PropertyType" />
                    <asp:BoundField DataField="Area" HeaderText="Area" SortExpression="Area" />
                    <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
                    <asp:ImageField HeaderText="Photo" DataImageUrlField="Photo" />
                    <asp:BoundField DataField="PostedDate" HeaderText="PostedDate" SortExpression="PostedDate" />
                </Columns>
            </asp:GridView>




Property cObject = new Property();
                SqlConnection con = cObject.connection();
                SqlDataSource SqlDataSource1 = new SqlDataSource();
                SqlDataSource1.ID = "SqlDataSource1";
                this.Page.Controls.Add(SqlDataSource1);
                SqlDataSource1.ConnectionString = cObject.connection().ToString();

                string query = "SELECT PropertyType, Area, Price, Image, PostedDate FROM TB_Property WHERE PropertyType like '" + "%" + propertyType + "' AND Area like '" + "%" + area + "' AND Price between'" + minPrice + "' AND '" + maxPrice + "'";
                SqlCommand cmd = new SqlCommand(query, con);
                con.Open();
                SqlDataReader dReader = cmd.ExecuteReader();
                
                GridView1.DataSource = SqlDataSource1;
                GridView1.DataBind();
                con.Close();
Posted
Updated 22-Dec-13 0:45am
v2

try declaring the SQLDataSource in the web form code, instead of doing it in the code behind.
then set the datasource to the GridView using the DataSourceID property.

if you need to later modify the query, just change it directly from the SqlDataSource object and call Rebind() on the grid to update results, no need for you to open/close any connections.

XML
<asp:SqlDataSource
         id="SqlDataSource1"
         runat="server"
         DataSourceMode="DataReader"
         ConnectionString="<%$ ConnectionStrings:YourConnectionString%>"
         SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
     </asp:SqlDataSource>

     <asp:GridView
         id="GridView1"
         runat="server"
         DataSourceID="SqlDataSource1">
your columns here..
     </asp:GridView>
 
Share this answer
 
HI. this will work fine..
This might help you..


C#
Property cObject = new Property();
            SqlConnection con = cObject.connection();
              
            string query = "SELECT PropertyType, Area, Price, Image, PostedDate FROM TB_Property WHERE PropertyType like '" + "%" + propertyType + "' AND Area like '" + "%" + area + "' AND Price between'" + minPrice + "' AND '" + maxPrice + "'";
            SqlCommand cmd = new SqlCommand(query, con);
            con.Open();
             
            DataTable dt = new DataTable();
            new SqlDataAdapter( cmd).Fill(dt);

            GridView1.DataSource = dt;
            GridView1.DataBind();
            con.Close();
 
Share this answer
 
Try This code i have used many time it's working
ASP.NET
<asp:gridview ID="Gridview1" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField HeaderText="Property Type">
           
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%#Eval("PropertyType") %>'></asp:Label>
            </ItemTemplate>
           
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Area">
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%#Eval("Area") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Price">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%#Eval("You Column Name (Price) change if you need ") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Photo ">
            <ItemTemplate>
                <asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("You Column Name (Photo) change if you need") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Posted Date">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%#Eval("You Column Name (Posted Date) change if you need") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
        </asp:gridview>

I hope your problem has been solve, for any problem hit to reply.
 
Share this answer
 
v2

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