Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a gridview and i want to bind it from the loop in which loop contain data from database and gridview displays only last record. i want to display all records which data is according to query.


my query in for loop like...

for (int k = 0; k < intArray.Length; k++)
{

string xxxx = "select title from document where id in('"+intArray[k]+"')";
ad1 = new SqlDataAdapter(xxxx, c.getcon());
ds1 = new DataSet();
ad1.Fill(ds1);

if (ds.Tables[0].Rows.Count != 0)
{
GridView1.DataSource = ds1;
GridView1.DataBind();
}
Posted
Updated 18-Apr-12 21:03pm
v2

Why do you need for loop?

Try this solution, i have just removed for loop.
C#
string xxxx = "select title from document";
ad1 = new SqlDataAdapter(xxxx, c.getcon());
ds1 = new DataSet();
ad1.Fill(ds1);

if (ds1.Tables[0].Rows.Count != 0)
{
GridView1.DataSource = ds1;
GridView1.DataBind();
}
 
Share this answer
 
This is not quite the correct approach.
You are actually overriding the gridview source in every iteration of the loop.

Try something similar to this
string xxxx = "select title from document where id in('";
for (int k = 0; k < intArray.Length; k++)
{
xxxx+=intArray[k]+"',"
}
xxxx=xxxx.SubString(0,xxxx.length-1) + ")";
 
ad1 = new SqlDataAdapter(xxxx, c.getcon());
ds1 = new DataSet();
ad1.Fill(ds1);
 
if (ds.Tables[0].Rows.Count != 0)
{
GridView1.DataSource = ds1;
GridView1.DataBind();
}
 
Share this answer
 
Comments
[no name] 19-Apr-12 3:34am    
but i want to bind data into gridview using query in which inarray contain id and each id i will check and then display according to the id. SO which way i go....
Make the for loop for the where condition only when you will get the desired solution.

C#
string xxxx;
for (int k = 0; k < intArray.Length; k++)
{
    xxxx += "'" + intArray[k] + "'," ;
}

xxxx = xxxx.Substring(0, xxxx.Length - 1);
SqlCommand cmd = new SqlCommand("select title from document where id in " + xxxx + ")", cn);
ad1 = new SqlDataAdapter(xxxx, c.getcon());
ds1 = new DataSet();
ad1.Fill(ds1);

if (ds1.Tables[0].Rows.Count != 0)
{
GridView1.DataSource = ds1;
GridView1.DataBind();
}
 
Share this answer
 
v2
Its Wrong code... Its not working
 
Share this answer
 
Comments
[no name] 20-Oct-12 6:18am    
what you want and please clarify what you want

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