Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a project where I need to load data into an existing html table.

The table is preexisting where I load only the tr/td.

The problem is when the table is loaded the code is adding extra tr td elements. I can not for the life of me figure out where those extra row/cell are coming from.

Those extra row cell render my table malformed therefore I am not able to view the table's data. I can only see headings.

What I have tried:

Here's my code.

public void Load_Inventory(string sql)
        {
            _ds = Utilities.Getds(sql);
            _dt = _ds.Tables["Table"];
            List<Inventor> InventoryList = new List<Inventor>();

            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(Server.MapPath("/requisition.aspx"));

            HtmlTableRow row = new HtmlTableRow();
            HtmlTableCell cell = new HtmlTableCell();

            _htmldatastr = string.Empty;
            foreach (DataTable table in _ds.Tables)
            {
               
                foreach (DataRow dr in table.Rows)
                {
                    _htmldatastr += "<tr class='odd gradeA'>";

                    _htmldatastr += "<td contenteditable='true'>" + dr["FORM_NO"].ToString() + "</td>"
                                 + "<td contenteditable='true'>" + dr["WAREHOUSE"].ToString() + "</td>"
                                 + "<td contenteditable='true'>" + dr["ON_HAND"].ToString() + "</td>"
                                 + "<td contenteditable='true'>" + dr["LAST_INVEN"].ToString() + "</td>";

                    _htmldatastr += "</tr>";

                    cell.InnerHtml = _htmldatastr;
                        row.Controls.Add(cell);
                        dta.Rows.Add(row);
                        ++cntr;
                }
                

            }
        }
Posted
Updated 28-Jun-21 23:17pm
Comments
[no name] 29-Jun-21 2:24am    
If you're processing ONE table, why are you iterating _ds.Tables?

1 solution

That code is extremely confused.

You create an HtmlTableRow (a <tr>) and an HtmlTableCell (a td), and then set the inner HTML of the <td> to a <tr> containing other <td>s. That will, of course, generate malformed HTML:
HTML
<tr>
    <td>
        <tr class="odd gradeA">
            <td contenteditable="true">...</td>
            <td contenteditable="true">...</td>
            <td contenteditable="true">...</td>
            <td contenteditable="true">...</td>
        </tr>
    </td>
</tr>

You are also reusing the same HtmlTableCell instance for each iteration of your loops. That won't do what you expect - only the data from the last iteration will be shown.

You need to fix your code to generate valid HTML. If it still doesn't work, then you need to explain precisely what you are trying to do, and what the problem is.
 
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