Click here to Skip to main content
16,020,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
how to bind gridview within while loop?before while datareader read Student Id then I want to disply data in gridview student Id wise.But it disply only last Id's details how I show whole data?
SqlCommand cmd = new SqlCommand("select distinct P.PartrowID from tbpanelparts P,tbParts PA where P.PartrowId=PA.ID and P.projid='"+Txt_prjid.Text+"' and P.partrowid not in (select ParentId from tbmakeup) and PA.supplierid="+Drp_supname.SelectedValue+"Union Select distinct(M.PartRowId) from tbMakeupproj MP,tbmakeup M,tbParts P where P.Id=M.PartRowId and MP.AssRowId=M.Rid and MP.ProjId='"+Txt_prjid.Text+"' and P.SupplierId="+Drp_supname.SelectedValue+"",connect.getConnection());
            cmd.Connection.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    //List p=new List();
                    //p.Add((int)dr[0]);
                    string a = dr[0].ToString();
                    SqlCommand cmd1 = new SqlCommand("Select C.CatName as ParentNm,P.CatName,P.PartNumber,n.TotQty,P.Price,P.SupplierId from tbParts P,tbCats C,(Select sum(CatQty) as TotQty from tbPanelParts PP,tbPanel PN  where PN.Id=PP.PanelId and PN.Status='Checked' and PN.Projid='" + Txt_prjid.Text + "' and PP.PartRowId=" + p + " and PP.partrowid not in (select ParentId from tbmakeup)) as n  where C.CatId=P.Parent and P.Id=" + p + "", connect.getConnection());
                    cmd1.Connection.Open();
                    SqlDataReader dr1=cmd1.ExecuteReader();
                    if (dr1.HasRows)
                    {
                        Grd_parts.DataSource = dr1;
                        Grd_parts.DataBind();
                    }
                   }
}
Posted
Updated 31-Oct-13 6:14am
v2
Comments
Richard C Bishop 31-Oct-13 11:52am    
Show the code you are using.
Member 10315373 31-Oct-13 12:02pm    
In second query Instead of parameter p I used a which is dr[0].tostring()
Member 10315373 31-Oct-13 12:00pm    
SqlCommand cmd = new SqlCommand("select distinct P.PartrowID from tbpanelparts P,tbParts PA where P.PartrowId=PA.ID and P.projid='"+Txt_prjid.Text+"' and P.partrowid not in (select ParentId from tbmakeup) and PA.supplierid="+Drp_supname.SelectedValue+"Union Select distinct(M.PartRowId) from tbMakeupproj MP,tbmakeup M,tbParts P where P.Id=M.PartRowId and MP.AssRowId=M.Rid and MP.ProjId='"+Txt_prjid.Text+"' and P.SupplierId="+Drp_supname.SelectedValue+"",connect.getConnection());
cmd.Connection.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
//List<int> p=new List<int>();
//p.Add((int)dr[0]);
string a = dr[0].ToString();
SqlCommand cmd1 = new SqlCommand("Select C.CatName as ParentNm,P.CatName,P.PartNumber,n.TotQty,P.Price,P.SupplierId from tbParts P,tbCats C,(Select sum(CatQty) as TotQty from tbPanelParts PP,tbPanel PN where PN.Id=PP.PanelId and PN.Status='Checked' and PN.Projid='" + Txt_prjid.Text + "' and PP.PartRowId=" + p + " and PP.partrowid not in (select ParentId from tbmakeup)) as n where C.CatId=P.Parent and P.Id=" + p + "", connect.getConnection());
cmd1.Connection.Open();
SqlDataReader dr1=cmd1.ExecuteReader();
if (dr1.HasRows)
{
Grd_parts.DataSource = dr1;
Grd_parts.DataBind();
}
}
}

Please reply...

Please have a look into the below article to understand more about GridView Binding. Note - You can't really set a DataReader as source for Gridview.

http://www.dotnetcurry.com/ShowArticle.aspx?ID=143[^]
 
Share this answer
 
Comments
Member 10315373 1-Nov-13 2:44am    
This article shows binding on gridview. But my question is though I use data table instead of data reader how I passed this datatable values to query which actually bind gridview.This will again comes into while loop.Is there any other way?
Ranjan.D 1-Nov-13 5:58am    
You don't need a while loop at all. Did you see the below code in that article ?

conn.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
DataTable dt = new DataTable();
dt.Load(dr);
GridView1.DataSource = dt;
GridView1.DataBind();

In first query instead of ExecuteReader you can use ExecuteScalar to return first row, first column value i.e to get P.PartrowID. Once you get this use the above code to fetch the records as you wish.
 
Share this answer
 

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