Cannot use "fill" with Datareader

N

.Net Sports

I have an open datareader that gets data from an sql statement, but
when I try to put it into a dataset so that my datagrid can pick it up,
I get a 'System.Data.SqlClient.SqlDataReader' does not contain a
definition for 'Fill' error

// dsRevenue is my datareader object
ds = new DataSet( );
drRevenue.Fill(ds);
 
M

Marina

Right.

The SqlDataReader class doesn't have a Fill method (as the message says).

You are looking for a SqlDataAdapter.
 
J

John Horst

Marina is right here. The SqlDataAdapter takes, at the very least, a
SelectCommand, but also can have Insert, Update and Delete commands.
Have your web page up in VS and go to your toolbox and click the Data
tab. Double click the SqlDataAdapter tool and a wizard will guide you
through creating the data adapter. Then you just do this:

Page_Load(<<args>>)
{
// get data.
daAdapter.Fill(dsDataSet.dtDataTable);

// check for postback.
if (!this.IsPostBack)
{
grdGrid.DataBind();
}
}

Make sure to fill before checking for the postback and only bind when
not a postback. Also, the the Fill method creates a SqlDataReader
behind the scenes and uses it get the data before populating the
datatable with datarows, so it is basically the same thing you were
trying to do.

John
 
N

.Net Sports

Thanks for the help - . I would have to start this webform over to go
into the VS.net sqldataAdapter wizard to do that. I would just need to
put the data from my DataReader (drRevenue) into a sqlDataAdapter

here's my creation of the datareader

SqlCommand objCommand = new SqlCommand(strSQL, objConn2);
objConn2.Open();

SqlDataReader drRevenue = objCommand.ExecuteReader();

I would now need to do something like:

da = new SqlDataAdapter(drRevenue);

but that is invalid, as SqlDataAdapter wants a command object
 
P

Paul Clement

¤ Thanks for the help - . I would have to start this webform over to go
¤ into the VS.net sqldataAdapter wizard to do that. I would just need to
¤ put the data from my DataReader (drRevenue) into a sqlDataAdapter
¤
¤ here's my creation of the datareader
¤
¤ SqlCommand objCommand = new SqlCommand(strSQL, objConn2);
¤ objConn2.Open();
¤
¤ SqlDataReader drRevenue = objCommand.ExecuteReader();
¤
¤ I would now need to do something like:
¤
¤ da = new SqlDataAdapter(drRevenue);
¤
¤ but that is invalid, as SqlDataAdapter wants a command object

You're still using a DataReader. See the doc for an example of how to use the Fill method with the
DataAdapter:

http://msdn.microsoft.com/library/d...stemdatasqlclientsqldataadapterclasstopic.asp


Paul
~~~~
Microsoft MVP (Visual Basic)
 
J

John Horst

Don't create the datareader. Use the command to create a dataadapter.
The data adapter will create its own data reader behind the scenes
anyway.

John

-----Original Message-----
From: .Net Sports [mailto:[email protected]]
Posted At: Friday, August 26, 2005 4:38 PM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: Cannot use "fill" with Datareader
Subject: Re: Cannot use "fill" with Datareader


Thanks for the help - . I would have to start this webform over to go
into the VS.net sqldataAdapter wizard to do that. I would just need to
put the data from my DataReader (drRevenue) into a sqlDataAdapter

here's my creation of the datareader

SqlCommand objCommand = new SqlCommand(strSQL, objConn2);
objConn2.Open();

SqlDataReader drRevenue
= objCommand.ExecuteReader();

I would now need to do something like:

da = new SqlDataAdapter(drRevenue);

but that is invalid, as SqlDataAdapter wants a command object
 

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,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top