views:

114

answers:

3

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.

Thanks in advance for any help you can provide.

+2  A: 

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)

Sk93
I tried that, but when I swtich from <script> tags to <% %> tags it tells me that reader isn't declared.
Liam
See to me that makes sense as that's how I'd have done it with Classic ASP. Although it doesn't seem to let me mix <script> and <% %> tags.
Liam
you're in a bit of bind. the <% %> tags are processed whilst the page is being rendered, whilst the <script> tags are done after... which explains why you're getting the problem.Depending on how much code you've got in <script> tags that is used to populate the reader, I would suggest switching over to use <% %>.Failing that, you'll need to give your table an ID and runat="server" attributes and build the table programatically using Table.rows.add...
Sk93
A: 

Right, I've done something similar, but not exact:

           Do While reader.HasRows
                Dim tNewRow As New HtmlTableRow
                Dim cellSKU, cellDESCR, cellQORD, cellPRIC As New HtmlTableCell
                orderNoLbl.Text = reader("NUMB")

                Do While reader.Read
                    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)
                Loop
                skusTable.Rows.Add(tNewRow)
                reader.NextResult()
            Loop

But, I am unable to get it to print out more than one row (I know for a fact that for my query reader is returning 3).

I think I'm just being a bit thick and it's probably staring me in the face.

What have I missed?

Liam
+1  A: 

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
Shiraz Bhaiji
Moved it, and still the one row is printed out O.oI haven't a clue why though as that all seems fine.
Liam
Spot on, that did the trick. Can't believe it was so obvious!
Liam