Datagrid + Datareader + Paging

  • Thread starter David A. Coursey
  • Start date
D

David A. Coursey

This code works if I don't use paging, but then I get a mile long page. As
soon as I put in paging it throws the error:

AllowCustomPaging must be true and VirtualItemCount must be set for a
DataGrid with ID dgWebLog when AllowPaging is set to true and the selected
datasource does not implement ICollection.

I have seen this before, and always fixed it by using a DataSet to fill the
grid instead of a DataReader. But now I use this Database Access Class that
I found online that works great, but uses a DataReader. Is there anything I
can do to still use a DataReader?

Anyhoo, here is my code:
protected void bindWebLogData(string login)
{
DataSet ds = new DataSet();
SqlDataReader dataReader = null;
Database data = new Database();
try
{
SqlParameter[] prams = {data.MakeInParam("@login" ,SqlDbType.VarChar,
75, login)};
data.RunProc("cma_getWebLogData", prams, out dataReader);
}
catch(Exception ex)
{
string errorMessage = "";
Exception curEx = ex;
while (curEx != null)
{
//collect all inner exceptions
errorMessage += curEx.Message + "\n";
curEx = curEx.InnerException;
}
//throw a new exception with errorMessage as it's message
throw new Exception(errorMessage);
}
finally
{
if (dataReader.HasRows)
{
dataReader.Read();
dgWebLog.DataSource = dataReader;
dgWebLog.DataBind();
}
}

}

Thanks for your help
David A. Coursey
 
R

Robert Koritnik

Well If you have paging, the error telss you, that the DataSource type MUST
implement ICollection. DataReader is not one of them. If you assign
DataReader to DataSource it actually reads the data connected way when data
binding, but if you use anything else than DataReader, databinding works
disconnected and you can manipulate data in-memory on the app server.

If you have the source code for that Database access class, I suggest you
apropriately change things so it will be able to use DataReader and DataSet
as well.

If you don't have the source, you have two choices:
1. Open and read your DataReader to DataTable and assign it to DataSource
2. Don't use that class from the net that can operate only with DataReader

Actually your code isn't that complicated so you could easily use
SqlConnection/SqlCommand or DataAdapter for the job without complicating
things with lots of extra code.

Or you could also write your own class Database and make it suit you and
your project if that's not too compliacted for you. I normally don't just
use some third party code from the net. Maybe I pick up some lines of code
but normally I do it myself to have total control and I know how things
behave.

There's another possibility (off the record). Use Lutz Roeder's .Net
Reflector and examine the code and write your own class called Database...
;)

--
RobertK
{ Clever? No just smart. }


David A. Coursey said:
This code works if I don't use paging, but then I get a mile long page. As
soon as I put in paging it throws the error:

AllowCustomPaging must be true and VirtualItemCount must be set for a
DataGrid with ID dgWebLog when AllowPaging is set to true and the selected
datasource does not implement ICollection.

I have seen this before, and always fixed it by using a DataSet to fill the
grid instead of a DataReader. But now I use this Database Access Class that
I found online that works great, but uses a DataReader. Is there anything I
can do to still use a DataReader?

Anyhoo, here is my code:
protected void bindWebLogData(string login)
{
DataSet ds = new DataSet();
SqlDataReader dataReader = null;
Database data = new Database();
try
{
SqlParameter[] prams = {data.MakeInParam("@login" ,SqlDbType.VarChar,
75, login)};
data.RunProc("cma_getWebLogData", prams, out dataReader);
}
catch(Exception ex)
{
string errorMessage = "";
Exception curEx = ex;
while (curEx != null)
{
//collect all inner exceptions
errorMessage += curEx.Message + "\n";
curEx = curEx.InnerException;
}
//throw a new exception with errorMessage as it's message
throw new Exception(errorMessage);
}
finally
{
if (dataReader.HasRows)
{
dataReader.Read();
dgWebLog.DataSource = dataReader;
dgWebLog.DataBind();
}
}

}

Thanks for your help
David A. Coursey
 
D

David A. Coursey

Yeah, I have the source so that is exactly what I will do. I was just
hoping to find an easy way to save myself some time.

Thanks for your reply
dave


Robert Koritnik said:
Well If you have paging, the error telss you, that the DataSource type
MUST
implement ICollection. DataReader is not one of them. If you assign
DataReader to DataSource it actually reads the data connected way when
data
binding, but if you use anything else than DataReader, databinding works
disconnected and you can manipulate data in-memory on the app server.

If you have the source code for that Database access class, I suggest you
apropriately change things so it will be able to use DataReader and
DataSet
as well.

If you don't have the source, you have two choices:
1. Open and read your DataReader to DataTable and assign it to DataSource
2. Don't use that class from the net that can operate only with DataReader

Actually your code isn't that complicated so you could easily use
SqlConnection/SqlCommand or DataAdapter for the job without complicating
things with lots of extra code.

Or you could also write your own class Database and make it suit you and
your project if that's not too compliacted for you. I normally don't just
use some third party code from the net. Maybe I pick up some lines of code
but normally I do it myself to have total control and I know how things
behave.

There's another possibility (off the record). Use Lutz Roeder's .Net
Reflector and examine the code and write your own class called Database...
;)

--
RobertK
{ Clever? No just smart. }


David A. Coursey said:
This code works if I don't use paging, but then I get a mile long page. As
soon as I put in paging it throws the error:

AllowCustomPaging must be true and VirtualItemCount must be set for a
DataGrid with ID dgWebLog when AllowPaging is set to true and the
selected
datasource does not implement ICollection.

I have seen this before, and always fixed it by using a DataSet to fill the
grid instead of a DataReader. But now I use this Database Access Class that
I found online that works great, but uses a DataReader. Is there
anything I
can do to still use a DataReader?

Anyhoo, here is my code:
protected void bindWebLogData(string login)
{
DataSet ds = new DataSet();
SqlDataReader dataReader = null;
Database data = new Database();
try
{
SqlParameter[] prams = {data.MakeInParam("@login" ,SqlDbType.VarChar,
75, login)};
data.RunProc("cma_getWebLogData", prams, out dataReader);
}
catch(Exception ex)
{
string errorMessage = "";
Exception curEx = ex;
while (curEx != null)
{
//collect all inner exceptions
errorMessage += curEx.Message + "\n";
curEx = curEx.InnerException;
}
//throw a new exception with errorMessage as it's message
throw new Exception(errorMessage);
}
finally
{
if (dataReader.HasRows)
{
dataReader.Read();
dgWebLog.DataSource = dataReader;
dgWebLog.DataBind();
}
}

}

Thanks for your help
David A. Coursey
 

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

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top