Reputation: 5348
Now, I could be being a bit thick and be doing this the wrong way, but in short, I have a dataset where I run a query to retrieve what a customer has ordered. Specifically, I've picked one with multiple items in for testing purposes.
I'll spare you all the code but the specific bit I'm having an issue with is:
<script runat="server">
Do While reader.HasRows
Do While reader.Read
</script>
<tr>
<td valign="top" width="100"><script runat="server">Response.Write(reader("SKUN"))</script><br /></td>
<td valign="top" width="200"><script runat="server">Response.Write(reader("DESCR"))</script></td>
<td valign="top" width="50"><script runat="server">Response.Write(reader("QORD"))</script></td>
<td valign="top" width="50"><script runat="server">Response.Write(reader("PRIC"))</script></td>
</tr>
<script runat="server">
Loop
reader.NextResult()
Loop
</script>
Ignore the inline code side of things, I'm working on another developers project and keeping in time with the way they work so it's easier for them to go back to.
However, this prints out exactly what I'm after, but it doesn't format it into rows in the table itself, it just prints one long line of text at the top of the page.
Am I missing something here or just purely doing it the wrong way? because I can't work out why it doesn't loop through the results to print out.
Upvotes: 1
Views: 165
Reputation: 65381
Try this:
Do While reader.Read
Dim tNewRow As New HtmlTableRow
Dim cellSKU, cellDESCR, cellQORD, cellPRIC As New HtmlTableCell
orderNoLbl.Text = reader("NUMB")
cellSKU.InnerText = reader("SKUN")
cellDESCR.InnerText = reader("DESCR")
cellQORD.InnerText = reader("QORD")
cellPRIC.InnerText = reader("PRIC")
tNewRow.Cells.Add(cellSKU)
tNewRow.Cells.Add(cellDESCR)
tNewRow.Cells.Add(cellQORD)
tNewRow.Cells.Add(cellPRIC)
skusTable.Rows.Add(tNewRow)
Loop
Upvotes: 1
Reputation: 3718
Response.Write() writes directly into the response stream. However, the HTML code in the ASPX page is first processed, buffered and only then written to the response stream. This results in your data being sent before any other part of the page is sent.
You should not use Response.Write(). Instead, you need to get your data into the same HTML which is processed by the ASP.NET rendering engine. Use the following syntax to achieve this:
<script runat="server">
Do While reader.HasRows
Do While reader.Read
</script>
<tr>
<td valign="top" width="100"><% =reader("SKUN") %><br /></td>
<td valign="top" width="200"><% =reader("DESCR") %></td>
<td valign="top" width="50"><% =reader("QORD") %></td>
<td valign="top" width="50"><% =reader("PRIC") %></td>
</tr>
<script runat="server">
Loop
reader.NextResult()
Loop
Edit based on comments
I think this is your best option:
<table id="someTable" runat="server">
....
<script runat="server">
Do While reader.HasRows
Do While reader.Read
Dim tr as new HtmlTableRow
tr.Cells.Add(new HtmlTableCell(){ InnerText = reader("SKUN"),Width=100,VAlign="top"})
tr.Cells.Add(new HtmlTableCell(){ InnerText = reader("DESCR"),Width=200,VAlign="top"})
tr.Cells.Add(new HtmlTableCell(){ InnerText = reader("QORD"),Width=50,VAlign="top"})
tr.Cells.Add(new HtmlTableCell(){ InnerText = reader("PRIC"),Width=50,VAlign="top"})
someTable.Rows.Add(tr)
</script>
sorry if it doesn't compile in VB.net right of the bat.. (I only have C# installed)
Upvotes: 2