DataReader to Dataset...?

Posted on 2008-10-31
Last Modified: 2012-06-21
I need to read data from a database on page load, then keep results in memory so to work "offline" and browse through records.

I do need a DataSet, right? If so, how do I fill a DataSet from a DataReader?
Question by:ziggie301
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Assisted Solution

rpkhare earned 150 total points
ID: 22853610
I am not following what you are saying. You will a DataSet through Data Adapter. What you are trying to achieve? If you want to simply fill the DataSet, it can easily be done by passing SQL Query to Data Adapter.
LVL 21

Expert Comment

ID: 22853703
Just plug this code in and pass your DataReader object...

DataSet yourset = DataReaderToDataSet(YourDataReader);

and here's the method:

static DataSet DataReaderToDataSet(IDataReader reader)
DataTable table = new DataTable();
int fieldCount = reader.FieldCount;
for (int i = 0; i < fieldCount; i++)
table.Columns.Add(reader.GetName(i), reader.GetFieldType(i));
object[] values = new object[fieldCount];
while (reader.Read())
table.LoadDataRow(values, true);
DataSet ds = new DataSet();
return ds;

with DataAdapter:

SqlDataReader dr = cmd.ExecuteReader();
DataSet ds = new DataSet();
DataSet dt = new DataTable();
DataReaderAdapter da = new DataReaderAdapter();
da.FillFromReader(dt, dr);
LVL 21

Expert Comment

ID: 22853718
By the way, IDataReader is used because it's just the interface that a DataReader must implement and therefore a DataReader is considered a IDataReader (i.e. it's considered a child class that's inheriting methods), so just pass a normal DataReader and it will work...
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 13

Accepted Solution

TechTiger007 earned 200 total points
ID: 22853909
Why do you need to get a datareader first and convert it into dataset?
Why dont you directly fill data into dataset.

Here is a sample
LVL 21

Assisted Solution

silemone earned 150 total points
ID: 22853964
I agree Tech, to a point so i gave him both ways because I thought similarly to let him see how small the code would be using a data adapter, however, the method for converting datareader to dataset is already written so he would only have one line of code to write that...Code for DataAdapter the code at the bottom of my post, but maybe he has a DataReader already and doesn't feel comfortable changing code around a  lot, so i took that into consideration since that's what he asked for and also gave him that code.  Also, this code is all over the web, so it appears many people convert DataReaders to Datasets.

Author Closing Comment

ID: 31512182
I just could'nt immagine how to fill a dataset. Now I do. :) Thank you

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity mvc , views, hidden values ? 2 22
C# Formatting DateTime String Value 5 50
c# DateTime Format validation 4 67
Updating a single record in forEach using C#/Entity 7 23
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question