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:
<table>
<thead>
<tr>
<th>No.</th>
<th>Username</th>
<th>Email</th>
<th>Position</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td><asp:Label ID="lblno" runat="server" Text="Label"></asp:Label> </td>
<td><asp:Label ID="lblusername" runat="server" Text="Label"></asp:Label></td>
<td><asp:Label ID="lblemail" runat="server" Text="Label"></asp:Label></td>
<td><asp:Label ID="lblposition" runat="server" Text="Label"></asp:Label></td>
<td><asp:Label ID="lblstatus" runat="server" Text="Label"></asp:Label></td>
</tr>
</tbody>
</table>
I know that this method is wrong...if anyone can point to the right direction...thanks in advance

New Topic/Question
Reply



MultiQuote





|