Click here to Skip to main content
15,906,097 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how everyone, how can i iterate through a list and display values for each list items,

i am retrieving a column from sql database and store in a list :var listAttributeRegion = new List<long>( ); the listAttributeRegion contains dataid values in this format: 50600,50601,50602,50603,50604 etc,

i want to pass each of this values from the list as parameter values to a select statement, and bind the resulting result set to a datagrid and also insert to a table in the database. here is my code:


C#
while ( reader.Read( ) )
                   {

                       listAttributeRegion.Add(Convert.ToInt64(reader["id"].ToString( )) );


                   }

                   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;
                                   com.Parameters.Add( "@DataID", SqlDbType.Int ).Value = dataId;
                                   using ( var adapter = new SqlDataAdapter( com ) )
                                   {

                                       using (var ds = new DataSet( ) )
                                       {
                                           adapter.Fill( ds );
                                           dataGridView1.DataSource = ds.Tables[0];
                                       }
                                   }
                               }
                           }
                       }
                       catch ( SqlException ex )
                       {

                       }
                          reader.Close( );
                   }



The above code populates ds with results set, when each dataid is passed, i want to able to append/combine all result sets and display in a datagrid, also insert to a table. Please any assistance on how to do this will be appreciated.
Posted

1 solution

I am not sure about your scenario but I think you can you can change your query to some extend to get the goal.

Instead of doing
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 ) )


you can write like:

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 in(select distinct id from yourtable) order by l.AttrID asc", strCon ) )

Or you can use joining also to get data.

Here yourtable is your first query's table name.
by this way you you can save looping through each item and hit database each time and overall your problem of combining data will also be solved.
 
Share this answer
 
Comments
Uwakpeter 23-Jun-14 11:43am    
The parameter value for @DataID is being supplied from a list, so each of the dataid supplied will return a result set, i am not sure select distinct will solve the problem, Please i will like to try your second option if you would explain it better.

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