Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I parse table from a Html table, that has 4 columns, and then i write that into a Database, using Sqlbulkcopy.

i need to be able to add a value from a textbox to the array/list before i bulk upload. This is so i can reference based on a machine name from a database ?

C#
string Url = "http://" + TextBox1.Text + "/logs/Logfile.html";

HtmlAgilityPack.HtmlWeb WebPage = new HtmlAgilityPack.HtmlWeb();

            HtmlAgilityPack.HtmlDocument document = WebPage.Load(Url);


            var table = new DataTable("Sample Table");


            table.Columns.Add("Entry", typeof(string));
            table.Columns.Add("Date", typeof(string));
            table.Columns.Add("Time", typeof(string));
            table.Columns.Add("Description", typeof(string));
            table.Columns.Add("Server", typeof(string));

            var nodes = document.DocumentNode.SelectNodes("//table[7]/tr[position() > 1]");


            nodes

            .Skip(1) // skips for headers
            .Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim())
            .ToArray())
            .ToList()
            .ForEach(row => table.Rows.Add(row));


            using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                conn.Open();
                using (SqlBulkCopy copy = new SqlBulkCopy(conn))
                {

                    copy.DestinationTableName = "LogEntries";
                    copy.WriteToServer(table);

                    conn.Close();
                }
            }
Posted
Updated 16-Oct-13 1:41am
v3

it's not clear what exactly you want to write, so I made just an example:

C#
table.Rows.Add("entry", "date", "time", textBox1.Text, "server");
 
Share this answer
 
Comments
Member 10315034 16-Oct-13 8:59am    
Hi mark, I Parse a data Table from a Html File (below), as per the Below, there are multiple Rows in the Html File ( it's a Log file), all the log entries are retrieved from the Html File. That Works no problem ( about 40 entries ), Each of these log files sit on Different servers, so i have a Textbox, where the Server name is Typed, and will then retrieve the Log File.

I Need to add the Textbox1.text Value into the Datatable,the Datatable is Filled with the log entries using the .ForEach(row => table.Rows.Add(row));... if that is changed to include the Textbox1.text, then the Data is Blank, for the Ammount of Rows..


<table border="0" cellpadding="2" width="100%">
<tr bgcolor="#F7F7FF">
<th>Entry</th>
<th>Date</th>
<th>Time</th>
<th>Description</th>
</tr>
<tr bgcolor="#F0F0FF">
<td>BLANK ROW</td> !! This is Skipped with the "/tr[position() > 1]"
<td>ENTRY NUMBER</td>
<td>DATE</td>
<td>TIME</td>
<td>Description HERE</td>
</tr>
I think you mean like this example. I'm not used to adding all methods behind eachother as you do with the nodes object. Maybe there is also a way to add the server to the list, but I would do it this way:

C#
string Url = "http://" + TextBox1.Text + "/logs/Logfile.html";
 
HtmlAgilityPack.HtmlWeb WebPage = new HtmlAgilityPack.HtmlWeb();
 
            HtmlAgilityPack.HtmlDocument document = WebPage.Load(Url);
 

            var table = new DataTable("Sample Table");
 

            table.Columns.Add("Entry", typeof(string));
            table.Columns.Add("Date", typeof(string));
            table.Columns.Add("Time", typeof(string));
            table.Columns.Add("Description", typeof(string));
            table.Columns.Add("Server", typeof(string));
 
            var nodes = document.DocumentNode.SelectNodes("//table[7]/tr[position() > 1]");
 

            nodes
 
            .Skip(1) // skips for headers
            .Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim())
            .ToArray())
            .ToList()
            .ForEach(row => table.Rows.Add(row));
 

            for(DataRow row in table.Rows)
               row["Server"] = TextBox1.Text;

            using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                conn.Open();
                using (SqlBulkCopy copy = new SqlBulkCopy(conn))
                {
 
                    copy.DestinationTableName = "LogEntries";
                    copy.WriteToServer(table);
 
                    conn.Close();
                }
            }
 
Share this answer
 
Comments
Member 10315034 16-Oct-13 9:44am    
thanks for the input, will have a look at some options.
VB
nodes

            .Skip(1) // skips for headers
            .Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim())
            .ToArray())
            .ToList()
            .ForEach(row => table.Rows.Add(row));


DataColumn Servcol = new DataColumn("CloudServer", typeof(String));

table.Columns.Add("Servcol");

foreach (DataRow row in table.Rows)
                      {
                         row["servcol"] = txt_Server.Text;
                     }


works Like a Charm....

for the Input, Put me in the Right direction
 
Share this answer
 

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