Click here to Skip to main content
12,705,109 members (26,834 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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 57 mins ago
MR.alaa546
Updated 13 mins ago

1 solution

Rate this: bad
 
good
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);
}
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 26 Jan 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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