Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello house,

Please does anyone know how to bind dataset to datagrid in a foreach loop? i have tried something like this:

C#
foreach ( long k in listAttributeRegion )
                    {
                        var dataId = k;

                        try
                        {

                            using ( var strCon = new SqlConnection( ConfigurationManager.ConnectionStrings["edms"].ConnectionString ) )
                            {
                                using ( var com = new SqlCommand( "Select l.AttrID , l.ValStr from csowner.LLAttrData l join csowner.dtree d on  ID = d.DataID where d.DataID = @DataID order by l.AttrID asc", strCon ) )
                                {
                                    com.CommandType = CommandType.Text;
                                    // command.Parameters.Add( "@SelectOption", SqlDbType.VarChar, 25 ).Value = "staffLastname";
                                    com.Parameters.Add( "@DataID", SqlDbType.Int ).Value = 56059;
                                    using ( var adapter = new SqlDataAdapter( com ) )
                                    {
                                        //DataSet ds;
                                        using ( var ds = new DataSet( ) )
                                        {
                                            try
                                            {
                                                adapter.Fill( ds );

                                                dataGridView1.DataSource = ds.Tables[0];
                                            }
                                            catch ( Exception ex )
                                            {

                                                MessageBox.Show( ex.Message );
                                            }

                                        }
                                    }
                                }
                            }
                        }
                        catch ( SqlException ex )
                        {
                            // log.Error( ex.Message + "\r\n\r\n " + ex.StackTrace );
                            MessageBox.Show( ex.Message );
                        }
                    }


                    reader.Close( );
                    //Console.ReadKey( );
                }
            }
            catch ( Exception ex )
            {
                MessageBox.Show( ex.Message );
            }

But it is not working! any assistance will be appreciated. Thanks in advance.
Posted
Comments
Bitla Phanindra 23-Jun-14 11:38am    
What error are you getting?
Uwakpeter 23-Jun-14 11:55am    
Not error message, it went into an endless loop, but if i step through it, i could see the result set for each dataid in the ds and datasource.
Bitla Phanindra 23-Jun-14 12:03pm    
The foreach you have at the top might be causing the issue.
Uwakpeter 23-Jun-14 12:06pm    
i felt i needed the foreach loop there so i could iterate through each item in the list and pass to the parameter in the select statement. Or is there any other way i could handle this? Thanks

1 solution

create one sql statement which return all the data you want.
C#
string sql = string.Format("Select l.AttrID , l.ValStr from csowner.LLAttrData l join csowner.dtree d on  ID = d.DataID where d.DataID in ({0}) order by l.AttrID asc", string.Join(",", listAttributeRegion));
using (var strCon = new SqlConnection( ConfigurationManager.ConnectionStrings["edms"].ConnectionString ) )
using (var com = new SqlCommand(sql, strCon))
using (var adapter = new SqlDataAdapter( com ))
{
   var ds = new DataSet();
   adapter.Fill(ds);
   dataGridView1.DataSource = ds.Tables[0];

}
 
Share this answer
 
Comments
Uwakpeter 23-Jun-14 15:38pm    
Thanks DamithSL, it works but takes too long to populate, please could it he optimized? it takes ip to five minutes for it to populate the datagrid.
Uwakpeter 24-Jun-14 2:56am    
Thanks Damith, I really appreciate.

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