Usman Masood
Usman Masood

Reputation: 11

Not reading the form element in the while loop first iteration ASP.NET

I have a while loop that runs 5 times:

string qry = "Select * from tbl_Products order by ProductId";
SqlCommand cmd = new SqlCommand(qry, con);
con.Open();
sbProducts="<table><tr>"; 
using (SqlDataReader sdr = cmd.ExecuteReader()) 
{ 
    while (sdr.Read()) 
    {
        sbProducts = sbProducts + "<td>";
        sbProducts = sbProducts + "<Form>";
        sbProducts = sbProducts + "123";
        sbProducts = sbProducts + "</Form>";
        sbProducts = sbProducts + "</td>"; 
    }
    sbProducts = sbProducts + "</table>";
    CellTwo = sbProducts.ToString();
    con.Close(); 
}

Its output is fine but it is not wrapping the first iteration TD is not wrap with form tag. It seems to be very illogical. The output is like:

<table>
<tr>
<td>123</td>
<td><form>123</form></td>
<td><form>123</form></td>
<td><form>123</form></td>
<td><form>123</form></td>


</tr>
</table>

Upvotes: 1

Views: 90

Answers (1)

MikeSmithDev
MikeSmithDev

Reputation: 15797

You have two problems. 1) You aren't properly building a table (you were missing a </tr>) 2) That table is likely inside another form. Your code (slightly cleaned up) should look like:

while (sdr.Read()) 
{
    sbProducts += "<td>";
    sbProducts += "<Form>";
    sbProducts += "123";
    sbProducts += "</Form>";
    sbProducts += "</td>"; 
}
sbProducts += "</tr></table>"; //You were missing this </tr>

Once you verify that you aren't doing this inside another form, you'll see that it comes out as expected. A form cannot contain another form, its an HTML standard.

Upvotes: 1

Related Questions