Lazy-Man's DataReader Display

G

Guest

Hi. I have a whole bunch of queries that I'll be running and returning in
OleDbDataReader objects. Some of these DataReaders will just be a simple
singe table result and others will be more complex with Sub-Tables and
Summaries, etc., etc. Here's my question:

Is there a way to use any of the ASP.NET 2.0 Data Controls - or any other
mechanism - to display the DataReader results without having to define all of
the columns and all that in design-time? I'm really not all that concerned
about the appearance of the output, I just want an easy way to display it
more or less the way it's displayed when you use any kind of query console
program and get results.

Any help is much appreciated.

Alex
 
T

Teemu Keiski

Hi,

GridView with AutoGenerateColumns="True" would autogenerate the columns
based on the data, and wouldn't need much to display data in a table format.
It just cannot show relations out of the box, you could use data source
controls with filtering to work in scenarios like that.

If you purely do it in console software, you could take field count from
dataReader and always for every DataReader row, loop through the columns and
build the display manually.

Any way, on ASP.NET QuickStarts you can find lots of examples how to do it
quickly and easily:
http://www.asp.net/QuickStart/aspnet/doc/data/default.aspx
 
S

Steven Cheng[MSFT]

Thanks Teemu for your informative inputs.

Hi Alex,

Do you think the "AutogeneratedColumns" option is suitable for your
scenario. Based on my understanding, if you do not want to design any
column/layout for the Gridview or Table at design-time, you will need to
completely do them in code at runtime. And I suggest you consider directly
use the ASP.NET Table control and dynamically creating the tablerows and
tablecells(Table control is much more flexible than gridview or other
databound based control). For example, the following code dynamically
populate a ASP.NET table control through the data reading from a DataReader:

#there is an emtpy ASP.NET Table control on the page:
==========================
protected void Page_Load(object sender, EventArgs e)
{

PopulateTable();
}

protected void PopulateTable()
{
SqlConnection conn = new
SqlConnection(WebConfigurationManager.ConnectionStrings["LocalNorthWind"].Co
nnectionString);

conn.Open();


SqlCommand comm = new SqlCommand("select CategoryID, CategoryName,
Description from categories", conn);

SqlDataReader reader = comm.ExecuteReader();

int cols = reader.FieldCount;

TableHeaderRow thr = new TableHeaderRow();

for (int i = 0; i < cols; i++)
{
TableHeaderCell thc = new TableHeaderCell();
thc.Text = reader.GetName(i);

thr.Cells.Add(thc);
}

Table1.Rows.Add(thr);


while (reader.Read())
{
TableRow tr = new TableRow();

for (int i = 0; i < cols; i++)
{
TableCell tc = new TableCell();
tc.Text = reader.GetValue(i).ToString();
tr.Cells.Add(tc);
}

Table1.Rows.Add(tr);
}


reader.Close();
conn.Close();
}
=================================

also, if you have queried multiple results in the datareader, you can use
the DataReader.NextResult method to get the next resultset (after you've
finishing looping through the former resultset).

Hope this helps.

Regards,

Steven Cheng

Microsoft MSDN Online Support Lead


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Hi Alex,

Does those suggestion help you some on this issue? If there is anything
else we can help, please feel free to post here.

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top