public static DataTable TotalArticle(int currentPage)
    {
SqlConnection conn=dataLink.DbConnet();
SqlCommand cmd=conn.CreateCommand();
cmd.CommandType=CommandType.StoredProcedure;
cmd.CommandText="TotalArticle";
cmd.Parameters.Add("@currentPage",currentPage);
SqlDataAdapter da=new SqlDataAdapter(cmd);
    DataSet ds=new DataSet();
    try
    {
    da.Fill(ds);
    da.Dispose();
    }
    catch
    {
    ds=null;
    }
    finally
    {
    conn.Close();
    }
return ds.Tables[0];
    }currentPage 为1时 这段代码没问题,
但当currentPage为2,3,4...时 就出现:System.NullReferenceException: 未将对象引用设置到对象的实例。
我的分页为每页显示20个记录,哪里错了!难道只能显示第一页,奇怪了,SQL语句 存储过程 没有问题的。

解决方案 »

  1.   

    如果存储过程没问题,那么很简单,在 SqlConnection conn=dataLink.DbConnet();
    设置端点,检查currentPage.
    看上去应该是存储过程的问题.不过既然你很确信.那只能是currentPage不对.
      

  2.   


    CREATE PROCEDURE TotalArticle
      @currentPage int 
    AS
    if (@currentPage=1)
      select top 20 ID,Topic,Author,AddTime,ViewNum,Poster,Classic from SundyArticle order by ID desc
    else
      select top 20 ID,Topic,Author,AddTime,ViewNum,Poster,Classic from SundyArticle where ID not in (' + select top (@currentPage-1)*20 ID from SundyArticle order by ID desc + ') order by ID descGO谢谢楼上的。存储过程是这样的,写错了吗?
      

  3.   

    你可以自己调试一下存储过程啊,比如说输入2给参数@currentPage看看返回结果是什么.
      

  4.   

    在SQL里手动执下 TotalArticle 这个Proc 
    看返回记录集是否正确
      

  5.   

    都SQL里执行过了,正常的,有返回值的。
      

  6.   


    select top 20 ID,Topic,Author,AddTime,ViewNum,Poster,Classic from SundyArticle where ID not in (' + select top 2*20 ID from SundyArticle order by ID desc + ') order by ID desc
    MS这个应该不能执行
      

  7.   

    where ID not in (' + select top 2*20 ID from SundyArticle order by ID desc + ')
    这句有问题
      

  8.   

    ALTER PROCEDURE dbo.StoredProcedure6 (
    @skip int,
    @to int
    )AS
    set rowcount @to
    SELECT  t1.theID 
    from(select ROW_NUMBER() OVER(ORDER BY t0.theID) as [Row_NUMBER],t0.theID from dbo.Table_2 AS t0) as t1 
    where t1.[Row_NUMBER]>@skip
    set rowcount 0
    RETURN
      

  9.   

    select top 20 ID,Topic,Author,AddTime,ViewNum,Poster,Classic from SundyArticle where ID not in (select top (@currentPage-1)*20 ID from SundyArticle order by ID desc) order by ID desc
      

  10.   

    楼上的这句作为存储过程是错误的。not in 后面应该为varchar