0

I have a code

public void LoadProducts()
     {
         //StringBuilder sbProducts = new StringBuilder();
         string qry = "Select * from tbl_Products order by ProductId";
         SqlCommand cmd = new SqlCommand(qry, con);
         con.Open();
         sbProducts="<table cellpadding='0' align='center' height: '250px'; width: '1000px'><tr>";

         using (SqlDataReader sdr = cmd.ExecuteReader())
         {
             while (sdr.Read())
             {
                 sbProducts = sbProducts + "<form action='ProductDetail.aspx'><td style='border-right:1px solid
 blue;border-top:1px solid blue;border-bottom:1px solid
 blue;border-left:1px solid blue;text-align:center;width:300px'>";
                 Session["code"] = sdr[0].ToString();
                 sbProducts = sbProducts + "<img src= " + sdr[5].ToString().Substring(2) + " width=120px height=150px><br>";
                 sbProducts = sbProducts + sdr[0].ToString() + sdr[2].ToString() + "<br>";
                 sbProducts = sbProducts + sdr[3].ToString() + "<br>";
                 sbProducts = sbProducts + "Rs: " + sdr[4].ToString();
                 sbProducts = sbProducts + "<br><input type='hidden' name='pid'  value='" + Session["code"] + "'><input type='submit'
 value='View Details'>";
                 sbProducts = sbProducts + "</td></form>";
             }
             sbProducts = sbProducts + "</table>";
             CellTwo = sbProducts.ToString();
             con.Close();
         }
     }

it contains a form that need to be repeated with every iteration but on first iteration, while loop is missing this form (html) element the rest of the iteration it works OK. i dont know what the issue is any why while loop is missing form element in first iteration.

1
  • Your structure is not well formatted. Commented Oct 3, 2013 at 9:57

2 Answers 2

1

Share us your html output please
By the look of things you'll generate something like this :

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

You're not closing the tr and the form should be in a td , not between tr and td.
The result should look like this :

<table>
  <tr>
    <td>
      <form></form>
    </td>
  </tr>
</table>
Sign up to request clarification or add additional context in comments.

2 Comments

'share us your html output please' should be a comment on the OP's question, not a lead-in to the answer. But your answer is a valid place to start.
Wanted to make sure i interpreted his code correctly but i do feel that this is the solution to his problem. Browsers tend to do strange things with badly structured tables
0

You can use like this

public void LoadProducts()
     {
         //StringBuilder sbProducts = new StringBuilder();
         string qry = "Select * from tbl_Products order by ProductId";
         SqlCommand cmd = new SqlCommand(qry, con);
         con.Open();
         sbProducts="<table cellpadding='0' align='center' height: '250px'; width: '1000px'><tr>";
         int counter=0;
         using (SqlDataReader sdr = cmd.ExecuteReader())
         {
             while (sdr.Read())
             {
                 if(counter++=0)
                 {
                 sbProducts = sbProducts + "<form action='ProductDetail.aspx'><td style='border-right:1px solid
 blue;border-top:1px solid blue;border-bottom:1px solid
 blue;border-left:1px solid blue;text-align:center;width:300px'>";
                 }
                 sbProducts = sbProducts +"<tr>";
                 Session["code"] = sdr[0].ToString();
                 sbProducts = sbProducts + "<img src= " + sdr[5].ToString().Substring(2) + " width=120px height=150px><br>";
                 sbProducts = sbProducts + sdr[0].ToString() + sdr[2].ToString() + "<br>";
                 sbProducts = sbProducts + sdr[3].ToString() + "<br>";
                 sbProducts = sbProducts + "Rs: " + sdr[4].ToString();
                 sbProducts = sbProducts + "<br><input type='hidden' name='pid'  value='" + Session["code"] + "'><input type='submit'
 value='View Details'>";
                 sbProducts = sbProducts + "</td>";
                 sbProducts = sbProducts +"</tr>";
             }
             //sbProducts = sbProducts + "</table>";
            // CellTwo = sbProducts.ToString();
             con.Close();
         }
         sbProducts = sbProducts + "</table>";
         CellTwo = sbProducts.ToString();
     }

similarly you can add </form> and table outside the while loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.