Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use the <em>FieldCount</em> property of the reader to iterate through the fields generating columns, the <em>Read</em> method of the reader to iterate through the rows. </p> <pre><code>StringBuilder tableOutput = new StringBuilder(); tableOutput.Append("&lt;table&gt;"); while (reader.Read()){ tableOutput.Append("&lt;tr&gt;"); for(int i=0;i&lt;reader.FieldCount;i++){ tableOutput.AppendFormat("&lt;td&gt;{0}&lt;/td&gt;", reader[i].ToString()); } tableOutput.Append("&lt;/tr&gt;"); } tableOutput.Append("&lt;/table&gt;"); </code></pre> <p>You can then either write them out with the response stream or use an HtmlGeneric control to set the contents. One neat trick is to use a normal div with a runat="server" attribute and an id - then you can assign the content easily using the InnerHtml property like:</p> <p>The div: </p> <pre><code>&lt;div id="dataDiv" runat="server" /&gt; </code></pre> <p>The code behind:</p> <pre><code>dataDiv.InnerHtml = tableOutput.ToString(); </code></pre> <p>Manually building the table is a bit cumbersome. It is usually much more expedient to use a control like a GridView or a template control like a DataList or Repeater to set up a template to display your results in your desired html format.</p> <p>Using that approach you wipe out all the looping and building of strings and simply assign a DataSource property like: </p> <pre><code>MyGridView.DataSource = reader; MyGridView.DataBind(); </code></pre> <p>You can see an example of templating with the Repeater <a href="http://msdn.microsoft.com/en-us/magazine/cc163780.aspx" rel="nofollow">here</a>. I myself prefer either Repeaters or DataList controls because they allow you to template your structure out in a much more flexible/html friendly approach. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload