Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have fetched the elements from the web page in a web browser control. I am using foreach and nested loops loop to match each element. Now I want to save each value into database one by one according to records on page. But every time it is saving only last record data multiple times. Please help out.

My code is:
C#
  dynamic div_classes = webBrowser1.Document.Body.GetElementsByTagName("div");
 foreach (HtmlElement d in div_classes)
            {
            dynamic d1 = webBrowser1.Document.Body.GetElementsByTagName("td");
             foreach (HtmlElement td in d1)
                {
                    if (td.GetAttribute("className") == "personName")
                    {
                        personName = td.InnerText;
                        //txtName.Text = personName;
                    }
                }
                 if (d.GetAttribute("id") == "title")
                {
                    persontitle = d.InnerText;
                 
                }
               // multiple values
            }
}


Where should I call my function "Save_to_DB()" to insert all values to database in a loop?


Thanks in advance.
Posted
Updated 25-Jun-14 0:03am
v3
Comments
Tanuj Rastogi 25-Jun-14 6:06am    
Currently, from where you are calling "Save_to_DB()" method? Do you want to save values at 'div' level or 'td' level ?
Bernhard Hiller 25-Jun-14 6:09am    
And more important: how does your "save-to-db-method" look like?
Vedat Ozan Oner 25-Jun-14 6:45am    
collect all data in a list. then send this list to your save method. if your list is too big to hold in the memory, then you can process a record as soon as you fetch it from the html page. many many options.
[no name] 25-Jun-14 7:12am    
Put your function call inside the loop where you want to call it.

This should be obvious but you'll need to save to the database inside your foreach loop so that each record gets saved to the database.
 
Share this answer
 
To be precise you should do following

dynamic div_classes = webBrowser1.Document.Body.GetElementsByTagName("div");
 foreach (HtmlElement d in div_classes)
            {
            dynamic d1 = webBrowser1.Document.Body.GetElementsByTagName("td");
             foreach (HtmlElement td in d1)
                {
                    if (td.GetAttribute("className") == "personName")
                    {
                        personName = td.InnerText;
                        //txtName.Text = personName;
                    }
                }
                 if (d.GetAttribute("id") == "title")
                {
                    persontitle = d.InnerText;
                 
                }
               Save_to_DB();
            }
}
 
Share this answer
 
Thanks for your answers.

I have already place the function to insert elements to the database at the same place but I am stuck at a very strange thing.

Every time it is inserting the data of last record of the page instead of showing each record.
Multiple data but of last record in repitition.
 
Share this answer
 
v2
Comments
_Asif_ 26-Jun-14 1:20am    
solutions are not meant for comments, please either improve your question or add comments.

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