Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problem: Popolating HTML Table Dynamically

Problem: Popolating HTML Table Dynamically

Scheduled Pinned Locked Moved ASP.NET
databasehelpcsharpphphtml
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    silentcore
    wrote on last edited by
    #1

    Hi There.
    I am having a problem in populating html table from database dynamically. What i mean is that i

    have a table in database that have let say 10 enteries, I want to display those 10 entries in a

    html table using loop. I am new to ASP.NET, I've previously woked in php. I can present the

    scenario in php and please help me do the same thing in ASP.NET. In php what i normally do is

    search the database, grab the table in fetch array and in html table i create one row and use

    while loop so that all of the 10 entries are populated at runtime. Here is a demonstration

    code in head:

    <?php
    $query = mysql_query("select * from table");
    ?>

    in body code:

    <table>
    <tr>
    <td>Username</td>
    <td>Email</td>
    <td>Position</td>
    <td>Status</td>
    </td>
    //start of the loop
    <?php while($fetch = mysql_fetch_array($query) { ?>
    <tr>
    <td><?php echo $fetch["username"]; ?> </td>
    <td><?php echo $fetch["email"]; ?> </td>
    <td><?php echo $fetch["position"]; ?> </td>
    <td><?php echo $fetch["status"]; ?> </td>
    </tr><?php } ?>//End of loop
    </table>

    When i use foreach loop in asp.net to do the same thing the it displays only the last row in the

    database table...I am using three tier architecture...

    projectsLoad_BLL bo = new projectsLoad_BLL();

                       DataSet ds = new DataSet();
                       DataTable dt = ds.Tables\[0\];
                       int num = 1;
                       foreach (DataRow dr in dt.Rows)
                       {
                           lblno.Text = num.ToString();
                           lblusername.Text = dr\["username"\].ToString();
                           lblemail.Text = dr\["email"\].ToString();
                           lblposition.Text = dr\["position"\].ToString();
                           lblstatus.Text = dr\["status"\].ToString();
                           dt.NewRow();
                           num++;
                       }
    

    In HTML Table:

    P J 2 Replies Last reply
    0
    • S silentcore

      Hi There.
      I am having a problem in populating html table from database dynamically. What i mean is that i

      have a table in database that have let say 10 enteries, I want to display those 10 entries in a

      html table using loop. I am new to ASP.NET, I've previously woked in php. I can present the

      scenario in php and please help me do the same thing in ASP.NET. In php what i normally do is

      search the database, grab the table in fetch array and in html table i create one row and use

      while loop so that all of the 10 entries are populated at runtime. Here is a demonstration

      code in head:

      <?php
      $query = mysql_query("select * from table");
      ?>

      in body code:

      <table>
      <tr>
      <td>Username</td>
      <td>Email</td>
      <td>Position</td>
      <td>Status</td>
      </td>
      //start of the loop
      <?php while($fetch = mysql_fetch_array($query) { ?>
      <tr>
      <td><?php echo $fetch["username"]; ?> </td>
      <td><?php echo $fetch["email"]; ?> </td>
      <td><?php echo $fetch["position"]; ?> </td>
      <td><?php echo $fetch["status"]; ?> </td>
      </tr><?php } ?>//End of loop
      </table>

      When i use foreach loop in asp.net to do the same thing the it displays only the last row in the

      database table...I am using three tier architecture...

      projectsLoad_BLL bo = new projectsLoad_BLL();

                         DataSet ds = new DataSet();
                         DataTable dt = ds.Tables\[0\];
                         int num = 1;
                         foreach (DataRow dr in dt.Rows)
                         {
                             lblno.Text = num.ToString();
                             lblusername.Text = dr\["username"\].ToString();
                             lblemail.Text = dr\["email"\].ToString();
                             lblposition.Text = dr\["position"\].ToString();
                             lblstatus.Text = dr\["status"\].ToString();
                             dt.NewRow();
                             num++;
                         }
      

      In HTML Table:

      P Offline
      P Offline
      Pankaj Nikam
      wrote on last edited by
      #2

      You can use ASP.NET Repeater control for the purpose. It will be sufficient for your need. For more details on Repeater Control check out here[^]. It also has a sample code at the end. Try it and post if you face any problem. I will be happy to help you.

      Always Keep Smiling. Yours Pankaj Nikam

      S 1 Reply Last reply
      0
      • S silentcore

        Hi There.
        I am having a problem in populating html table from database dynamically. What i mean is that i

        have a table in database that have let say 10 enteries, I want to display those 10 entries in a

        html table using loop. I am new to ASP.NET, I've previously woked in php. I can present the

        scenario in php and please help me do the same thing in ASP.NET. In php what i normally do is

        search the database, grab the table in fetch array and in html table i create one row and use

        while loop so that all of the 10 entries are populated at runtime. Here is a demonstration

        code in head:

        <?php
        $query = mysql_query("select * from table");
        ?>

        in body code:

        <table>
        <tr>
        <td>Username</td>
        <td>Email</td>
        <td>Position</td>
        <td>Status</td>
        </td>
        //start of the loop
        <?php while($fetch = mysql_fetch_array($query) { ?>
        <tr>
        <td><?php echo $fetch["username"]; ?> </td>
        <td><?php echo $fetch["email"]; ?> </td>
        <td><?php echo $fetch["position"]; ?> </td>
        <td><?php echo $fetch["status"]; ?> </td>
        </tr><?php } ?>//End of loop
        </table>

        When i use foreach loop in asp.net to do the same thing the it displays only the last row in the

        database table...I am using three tier architecture...

        projectsLoad_BLL bo = new projectsLoad_BLL();

                           DataSet ds = new DataSet();
                           DataTable dt = ds.Tables\[0\];
                           int num = 1;
                           foreach (DataRow dr in dt.Rows)
                           {
                               lblno.Text = num.ToString();
                               lblusername.Text = dr\["username"\].ToString();
                               lblemail.Text = dr\["email"\].ToString();
                               lblposition.Text = dr\["position"\].ToString();
                               lblstatus.Text = dr\["status"\].ToString();
                               dt.NewRow();
                               num++;
                           }
        

        In HTML Table:

        J Offline
        J Offline
        jkirkerx
        wrote on last edited by
        #3

        You should create a container, it can be a table called table_Container, and then loop threw the dataset, and create your tableRow and cells in code. You can use the repeater control, but I think you should learn the basics first. Do not use Response.write(""), because response write is reserved for higher level responses like a page error code, or a data set or something. PHP doesn't have html objects, so echo("tr" is quite common, but is poor practice in general.

        foreach (DataRow dr in dt.Rows) {

        TableRow tr = new TableRow;
        table\_Container.controls.add(tr);
        
        TableCell td = new TableCell;
        tr.controls.add(td);
        
        Label lbl\_UserName = new Label;
        lbl\_UserName.text = dr\["username"\];
        td.controls.add(lbl\_UserName);
        

        }

        E 1 Reply Last reply
        0
        • J jkirkerx

          You should create a container, it can be a table called table_Container, and then loop threw the dataset, and create your tableRow and cells in code. You can use the repeater control, but I think you should learn the basics first. Do not use Response.write(""), because response write is reserved for higher level responses like a page error code, or a data set or something. PHP doesn't have html objects, so echo("tr" is quite common, but is poor practice in general.

          foreach (DataRow dr in dt.Rows) {

          TableRow tr = new TableRow;
          table\_Container.controls.add(tr);
          
          TableCell td = new TableCell;
          tr.controls.add(td);
          
          Label lbl\_UserName = new Label;
          lbl\_UserName.text = dr\["username"\];
          td.controls.add(lbl\_UserName);
          

          }

          E Offline
          E Offline
          Eone James
          wrote on last edited by
          #4

          I totally agree with him that you should learn basic thing how to create response.write(". ;)

          1 Reply Last reply
          0
          • P Pankaj Nikam

            You can use ASP.NET Repeater control for the purpose. It will be sufficient for your need. For more details on Repeater Control check out here[^]. It also has a sample code at the end. Try it and post if you face any problem. I will be happy to help you.

            Always Keep Smiling. Yours Pankaj Nikam

            S Offline
            S Offline
            silentcore
            wrote on last edited by
            #5

            Thank You very much for your help. I've managed to use asp repeater with datatables efficiently. What i did is added a repeater in asp markup and bind it to dataset. And in i've placed <%# Eval("columnName") %> and viola. All operations are working fine. Thanks again!

            P 1 Reply Last reply
            0
            • S silentcore

              Thank You very much for your help. I've managed to use asp repeater with datatables efficiently. What i did is added a repeater in asp markup and bind it to dataset. And in i've placed <%# Eval("columnName") %> and viola. All operations are working fine. Thanks again!

              P Offline
              P Offline
              Pankaj Nikam
              wrote on last edited by
              #6

              Using Repeater is certainly a very good choice. I am happy that it worked for you :) I have one more suggestion - if you are having only read-only records i.e. you only view them, then consider using SqlDataReader object instead of the DataSet as it is fast - read-only - forward only. Happy to help you :)

              Always Keep Smiling. Yours Pankaj Nikam

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups