Click here to Skip to main content
14,241,604 members
Rate this:
Please Sign up or sign in to vote.
See more:
i have to loop in page and get data from html table i get all the data in a list each time i loop and get them into list how can i pass them to data table where all the rows of the list has only one column and different in values ?? i try to convert them to dictionary and pass them to data table but i have get error duplicate column ?? this is my code

What I have tried:

List<string> list = new List<string>();
                       if (pagevalue != null)
                       {


                           foreach (HtmlNode data in pagevalue.DocumentNode.SelectNodes("//div"))
                           {
                                 list.Add(infodata.InnerText);
                           }

                           if (list.Count > 0)
                           {

                               var dict = list.ToDictionary(k => k, v => v);

                               foreach (KeyValuePair<string, string> item in dict)
                               {
                                   DataRow dataRow = dt.NewRow();
                                   dt.Columns.Add(item.Key);
                               }

                               DataSet ds = new DataSet();
                               ds.Tables.Add(dt);
                           }
Posted
Updated 25-Jan-17 21:16pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Why do you need dictionary? Try this and adapt:
List<string> cities = new List<string>();
cities.Add("New York");
cities.Add("Mumbai");
DataTable table = new DataTable();
table.Columns.Add("column1", typeof (string));
foreach (string str in cities)
{
	DataRow row = table.NewRow();
	row["column1"] = str;
	table.Rows.Add(row);
}
   
v2
Comments
MR.alaa 26-Jan-17 3:39am
   
the same thing it build one column (i need it in a row not column ) each time and remove the old one not append i have number of rows which to get all at the end of looping with the same column names thanks for help
Peter Leow 26-Jan-17 3:47am
   
You mean the datatable is going to have only one row with multiple columns? Anyway, you can add columns and rows to datatable. Check this out: https://www.dotnetperls.com/datatable
MR.alaa 26-Jan-17 4:14am
   
if (list.Count > 0)
{



DataTable table = new DataTable();

foreach (string ColumnN in list)
{
table.Columns.Add(ColumnN, typeof(string));
}

foreach (string str in list)
{
DataRow row = table.NewRow();
row[ColumnN] = str;
table.Rows.Add(row);
}

DataSet ds = new DataSet();
ds.Tables.Add(table);
}

i get error row[ColumnN] = str; how i can be done correctly anh how can i make the column name declared for only one time ?? not to be make in each loop ?
epdoce 13-Jul-17 1:32am
   
foreach (string ColumnN in list)
{
table.Columns.Add(ColumnN, typeof(string));
}
this is wrong...
As you see you used a loop to create columns in your table using the list items as your column name. Then in your next loop column "ColumnN" is unknown... your row will be out of index
Peter Leow 26-Jan-17 5:37am
   
What are you trying to achieve. You are adding columns using the list elements, then you try to add row using the list elements again? And it appear each row is going to have a column filled with the column name. Anyway, what is this columnN inside the row[columnN]=str, do you mean row[str]=str? Apprently, you have not visited the link I have given you https://www.dotnetperls.com/datatable

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100