Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,

I am using grid view to show my data, but its not working any data.

code of aspx page:-

ASP.NET
<asp:GridView runat="server" ID="gh1" AutoGenerateColumns="false" EmptyDataText="No">
                    <Columns>
                    <asp:TemplateField HeaderText="Product Name">

                    <ItemTemplate>
                    <asp:Label runat="server" ID="lbl1" Text='<%Eval("PName")%>'></asp:Label>
                    </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Quantity">
                    <ItemTemplate>
                    <asp:Label runat="server" ID="lbl2" Text='<%Eval("OPQuantity") %>'></asp:Label>
                    </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Price">
                    <ItemTemplate>
                    <asp:Label runat="server" ID="lbl3" Text='<%Eval("PPrice") %>'></asp:Label>
                    </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Total">
                    <ItemTemplate>
                    <asp:Label runat="server" ID="lbl4" Text='<%Eval("OPTotalPrice") %>'></asp:Label>
                    </ItemTemplate>
                    </asp:TemplateField>
                    </Columns>
                    </asp:GridView>



code for aspx.cs page:-

C#
SqlDataAdapter daShow1 = new SqlDataAdapter("SELECT a.OID, a.PID, b.PName as PName, a.OPQuantity as OPQuantity, a.PPrice as PPrice, a.OPTotalPrice as OPTotalPrice FROM OrderProducts AS a LEFT OUTER JOIN Products AS b ON a.PID = b.PID WHERE (a.OID = "+txtSearchOrderID.Text+")", con);
        DataSet dsShow1 = new DataSet();
        daShow1.Fill(dsShow1);
        gh1.DataSource = dsShow1;
        gh1.DataBind()

.
Posted
Updated 8-Apr-12 1:03am
v3
Comments
tanweer 8-Apr-12 6:56am    
put your exception text here if there occurs error otherwise put a break point on this line
gh1.DataSource = dsShow1;
and select dsShow1 and right click on it --go to Quick Watch and see the table data. It seems your query does not return any result.
abhishekagrwl25 8-Apr-12 10:08am    
its not showing any exception, query is right, i have checked it on sql query analyser, dsshow1 has data but it not bind it to gridview.

Try This...

C#
SqlCommand cmd=new SqlCommand("SELECT a.OID, a.PID, b.PName as PName, a.OPQuantity as OPQuantity, a.PPrice as PPrice, a.OPTotalPrice as OPTotalPrice FROM OrderProducts AS a LEFT OUTER JOIN Products AS b ON a.PID = b.PID WHERE (a.OID = "+txtSearchOrderID.Text+")", con);

SqlDataAdapter daShow1 = new SqlDataAdapter(cmd);

SqlCommandBuilder cb = new SqlCommandBuilder(daShow1);
        DataSet dsShow1 = new DataSet();
        daShow1.Fill(dsShow1);
        gh1.DataSource = dsShow1;
        gh1.DataBind();
 
Share this answer
 
v2
Comments
abhishekagrwl25 8-Apr-12 10:19am    
also tried it...but still having same problem..
Kuldeep B 8-Apr-12 13:09pm    
check that your query return result(some row) or it may be not content record...
Hi ,
i modified the Solution it work fine
C#
SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=TestDB;Integrated Security=True");
        con.Open();
        SqlDataAdapter daShow1 = new SqlDataAdapter("SELECT a.OID, a.PID, b.PName as PName, a.OPQuantity as OPQuantity, a.PPrice as PPrice, a.OPTotalPrice as OPTotalPrice FROM OrderProducts AS a LEFT OUTER JOIN Products AS b ON a.PID = b.PID WHERE (a.OID = '" + txtSearchOrderID.Text + "'", con);

       // SqlDataAdapter daShow1 = new SqlDataAdapter("select * from dbo.Test_Tbl  where Name  = '" + txtSearchOrderID.Text + "'", con);
        DataSet dsShow1 = new DataSet();
        daShow1.Fill(dsShow1);
        gh1.DataSource = dsShow1.Tables[0];
        gh1.DataBind();


ASP.NET
<asp:GridView runat="server" ID="gh1" AutoGenerateColumns="false" EmptyDataText="No">
                    <Columns>
                    <asp:TemplateField HeaderText="Product Name">

                    <ItemTemplate>
                        <asp:Label ID="lbl1" runat="server" Text='<%# Eval("PName") %>'></asp:Label>
                    </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Quantity">
                    <ItemTemplate>
                    <asp:Label runat="server" ID="lbl2" Text='<%#Eval("OPQuantity") %>'></asp:Label>
                    </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Price">
                    <ItemTemplate>
                    <asp:Label runat="server" ID="lbl3" Text='<%#Eval("PPrice") %>'></asp:Label>
                    </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Total">
                    <ItemTemplate>
                    <asp:Label runat="server" ID="lbl4" Text='<%#Eval("OPTotalPrice") %>'></asp:Label>
                    </ItemTemplate>
                    </asp:TemplateField>
                    </Columns>
                    </asp:GridView>


the Problem was
1 - in your sql statement .
2 - in your expression binding , you were binding like this
C#
<asp:Label runat="server" ID="lbl1" Text='<%Eval("PName")%>'></asp:Label>

and you should do it like this one
C#
<asp:Label ID="lbl1" runat="server" Text='<%# Eval("PName") %>'></asp:Label>

Hope it help you .
Best Regards
M.Mitwalli
 
Share this answer
 
v3
Comments
abhishekagrwl25 8-Apr-12 10:15am    
tried it, but still having same problem, gridview has data through datasource, but not binding it.
Mohamed Mitwalli 9-Apr-12 1:18am    
Hi
i already fix the problem you have check the solution and you will find what was your problem
abhishekagrwl25 8-Apr-12 10:17am    
is there any problem with Eval function..??
Mohamed Mitwalli 8-Apr-12 14:00pm    
check this solution it will help you.
abhishekagrwl25 9-Apr-12 1:56am    
thanx..:)

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