Keith Devens .com |
Saturday, September 6, 2008 | ![]() |
| Beauty is the first test: there is no permanent place in this world for ugly mathematics. – G. H. Hardy (A Mathematician's Apology) | ||
|
| ← ADO.NET for the Java Programmer | Carrie Underwood's audition video → |

Hannibal wrote:
Keith (http://keithdevens.com/) wrote:
Ah, reader.GetName(i). Didn't see that one!
But HtmlTextWriter? Icky!
Hannibal wrote:
HtmlTextWriter is not icky. It is faster to type and the output is formatted.
Keith Gaughan (http://talideon.com/) wrote:
I agree with Keith: it is icky. For a start, There's the WriteFullBeginTag() and WriteEndTag() methods: ew! This is duplication of effort. You shouldn't have do this! You should be able to create an element, giving its name and attributes, and insert child elements and text elements into it. And when you request a child element to be added, the method should return an object representing it, which you can then manipulate further. When you're done, simply use ToString() and write the result wherever you want. In fact, if I have some free time tomorrow, I'll prototype it and throw it up online.
That, and HtmlTextWriter is overkill. You're not going looking at the source it generates (so nicely formatted output isn't a necessity), and all you really want is a dump of a table. It works just fine.
Feel free to post a comment below. Please see my comment policy.
Formatting Rules (No HTML):
Generated in about 0.21s.
(Used 8 db queries)

1. This code compiled...ship it!
2. I just finished my third martini.
3. I still think this will probably work (please let me know if it does not).
// call this like: KeithTakesADump(sqlDataReader, Response.Output);
public static void KeithTakesADump(IDataReader reader, TextWriter output)
{
HtmlTextWriter writer = new HtmlTextWriter(output);
writer.WriteFullBeginTag("table");
writer.WriteFullBeginTag("tr");
for (int i = 0; i < reader.FieldCount; ++i)
{
writer.WriteFullBeginTag("td");
writer.Write(reader.GetName(i));
writer.WriteEndTag("td");
}
writer.WriteEndTag("tr");
while (reader.Read())
{
writer.WriteFullBeginTag("tr");
for (int i = 0; i < reader.FieldCount; ++i)
{
writer.WriteFullBeginTag("td");
writer.Write(reader[i].ToString());
writer.WriteEndTag("td");
}
writer.WriteEndTag("tr");
}
}