无法计算未实现 ICollection 的数据源中的计数。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Web.HttpException: 无法计算未实现 ICollection 的数据源中的计数。源错误: 
行 119: conn.Open();
行 120: DataGrid1.DataSource = cmd.ExecuteReader();
行 121: DataGrid1.DataBind();
行 122: conn.Close();
行 123:源文件: d:\inetpub\wwwroot\geustbook\webform1.aspx.cs    行: 121 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
过程如下:
CREATE procedure hj
(@pagesize int,
@pageindex int,
@docount bit)
as
set nocount on
if(@docount=1)
select count(id) from news
else
begin
declare @indextable table(id int identity(1,1),nid int)
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound
insert into @indextable(nid) select id from news order by id desc
select O.* from news O,@indextable t where O.id=t.nid
and t.id>@PageLowerBound and t.id<=@PageUpperBound order by t.id
end
set nocount offGO调用代码如下:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
str_sql = System.Configuration.ConfigurationSettings.AppSettings["CnString"];
SqlConnection conn = new SqlConnection(str_sql);
SqlCommand cmd=new SqlCommand("hj",conn);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add("@pageindex",1);
cmd.Parameters.Add("@pagesize",1);
cmd.Parameters.Add("@docount",true);
conn.Open();
AspNetPager1.RecordCount=(int)cmd.ExecuteScalar();
conn.Close();
BindData();
}
} private void BindData()
{
SqlConnection conn = new SqlConnection(str_sql);
SqlCommand cmd=new SqlCommand("hj",conn);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add("@pageindex",AspNetPager1.CurrentPageIndex);
cmd.Parameters.Add("@pagesize",AspNetPager1.PageSize);
cmd.Parameters.Add("@docount",false);
conn.Open();
DataGrid1.DataSource = cmd.ExecuteReader();
DataGrid1.DataBind();
conn.Close(); AspNetPager1.CustomInfoText="记录总数:<font color=\"blue\"><b>"+AspNetPager1.RecordCount.ToString()+"</b></font>";
AspNetPager1.CustomInfoText+=" 总页数:<font color=\"blue\"><b>"+AspNetPager1.PageCount.ToString()+"</b></font>";
AspNetPager1.CustomInfoText+=" 当前页:<font color=\"red\"><b>"+AspNetPager1.CurrentPageIndex.ToString()+"</b></font>";
}
.......................................调试了几个小时,实在是搞不懂怎么回事情啊。
偶才开始学习asp.net 很多地方都搞不大清楚,请各位老兄帮忙解答一下,万分感激。

解决方案 »

  1.   

    你在 SQL 查询分析器中运行看看能不能有没有记录返回
    可能是你的查询没有返回记录
      

  2.   

    to 楼上:
    过程单独运行没问题 exec hj 5,1,0 
    返回5条记录,所以问题应该不是出在过程上。嘎嘎
      

  3.   

    不用SqlDataReader ,换成 SqlDataAdapter 填充DataSet看看可以没有?SqlConnection conn = new SqlConnection(str_sql);
    SqlCommand cmd=new SqlCommand("hj",conn);
    cmd.CommandType=CommandType.StoredProcedure;
    cmd.Parameters.Add("@pageindex",AspNetPager1.CurrentPageIndex);
    cmd.Parameters.Add("@pagesize",AspNetPager1.PageSize);
    cmd.Parameters.Add("@docount",false);
    conn.Open();
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    sda.Fill(ds,"test");
    DataGrid1.DataSource = ds.Tables[0].DefaultView;
    DataGrid1.DataBind();
    conn.Close();
      

  4.   

    当 docount <> 1 执行的是一条 insert 语句吧不能用 SqlDataReader
      

  5.   

    nnd 重新拖了一个DataGrid2过来 重新bind 问题解决
    纳闷啊,怎么会这样 我嘎嘎