请问我的这段代码有什么错呀?运行的时候说:未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。//获取连接字符串
        string connectionString = ConfigurationManager.ConnectionStrings["ttConnectionString"].ConnectionString;
        //创建并设置SqlConnection
        SqlConnection dbConnection = new SqlConnection(connectionString);
        //定义SQL查询语句
        string queryString = "Select * from text";
        //创建并设置SqlCommand
        SqlCommand dbCommand = new SqlCommand();
        dbCommand.Connection = dbConnection;
        dbCommand.CommandType = CommandType.Text;
        dbCommand.CommandText = queryString;
        //创建SqlDataAdapter,并获取数据
        SqlDataAdapter dataAdapter = new SqlDataAdapter(dbCommand);
        DataSet ds = new DataSet();
        dataAdapter.Fill(ds);
                
        PagedDataSource objds = new PagedDataSource();
        objds.DataSource = ds.Tables["text"].DefaultView;//dataset  数据源
        objds.AllowPaging = true;
        objds.PageSize = 6;
               DataList1.DataSource = objds;
        DataList1.DataBind();

解决方案 »

  1.   

    首先在设计页面上定义两个Label控件:ID分别为“labNowPage”和“labCount”,分别用来显示当前页数和总页数。
    再定义四个LinkButton控件,ID分别为“lnkbtnTop”、“lnkbtnPrve”、“lnkbtnNext”和“lnkbtnLast”,分别用来进行“首页”、“上一页”、“下一页”和“尾页”的翻页操作。
    当然还有一个DataList(ID="DataList1")控件了。//cs文件中的内容
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dlBind();
            }
        }
        public void dlBind()
        {
            int curpage = Convert.ToInt32(labNowPage.Text);
            PagedDataSource ps = new PagedDataSource();
            SqlConnection myCnn = new SqlConnection("server=(local);uid=sa;pwd=sa;database=pubs");
            myCnn.Open();
            SqlDataAdapter myAdp = new SqlDataAdapter("select * from authors", myCnn);
            DataSet ds = new DataSet();
            myAdp.Fill(ds, "authors");        ps.DataSource = ds.Tables["authors"].DefaultView;
            ps.AllowPaging = true;
            ps.PageSize = 4;
            ps.CurrentPageIndex = curpage - 1;        lnkbtnPrve.Enabled = true;
            lnkbtnTop.Enabled = true;
            lnkbtnNext.Enabled = true;
            lnkbtnLast.Enabled = true;
            if (curpage == 1)
            {
                lnkbtnTop.Enabled = false;
                lnkbtnPrve.Enabled = false;
            }
            if (curpage == ps.PageCount)
            {
                lnkbtnNext.Enabled = false;
                lnkbtnLast.Enabled = false;
            }
            labCount.Text = Convert.ToString(ps.PageCount);
            DataList1.DataSource = ps;
            DataList1.DataKeyField = "au_id";
            DataList1.DataBind();
        }
        protected void lnkbtnPrve_Click(object sender, EventArgs e)
        {
            labNowPage.Text = Convert.ToString(Convert.ToInt32(labNowPage.Text) - 1);
            dlBind();
        }
        protected void lnkbtnTop_Click(object sender, EventArgs e)
        {
            labNowPage.Text = "1";
            dlBind();
        }
        protected void lnkbtnNext_Click(object sender, EventArgs e)
        {
            labNowPage.Text = Convert.ToString(Convert.ToInt32(labNowPage.Text) + 1);
            dlBind();
        }
        protected void lnkbtnLast_Click(object sender, EventArgs e)
        {
            labNowPage.Text = labCount.Text;
            dlBind();
        }
      

  2.   

    存储过程
    create procedure wzl
    (@startIndex int,
    @pageSize int)
    as
    set nocount on 
    declare @MyTable table(id int identity(1,1),nid int);
    declare @PageUpperBound int;
    set @PageUpperBound=@startIndex+@pagesize-1;
    set rowcount @PageUpperBound;
    insert into @MyTable(nid) select orderid from orders;
    select * from @MyTable as x left outer join orders on orders.orderid = x.id
    where x.id between @startIndex and @PageUpperBound  
    set nocount off
    RETURN用哪个AspNetPager页面代码
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Security;
    using System.Text.RegularExpressions;
    using System.Data.SqlClient;
    namespace Test
    {public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataList DataList1;
    protected Wuqi.Webdiyer.AspNetPager AspNetPager1;private void Page_Load(object sender, System.EventArgs e)
    {
    SqlConnection conn = new SqlConnection("server=.;pwd=sa;uid=sa;database=northwind");
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.Text;
    cmd.Connection = conn;
    cmd.CommandText = "select count(orderid) from orders";
    conn.Open();
    this.AspNetPager1.RecordCount = (int)cmd.ExecuteScalar();
    conn.Close();
    bindData();
    }
    void bindData()
    {
    SqlConnection conn = new SqlConnection("server=.;pwd=sa;uid=sa;database=northwind");
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Connection = conn;
    cmd.CommandText = "wzl";
    cmd.Parameters.Add("@startIndex",this.AspNetPager1.StartRecordIndex);
    cmd.Parameters.Add("@pageSize",this.AspNetPager1.PageSize);
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    sda.Fill(ds);
    this.DataList1.DataSource = ds.Tables[0].DefaultView;//DataList1.DataSource = SqlHelper.ExecuteReader(CommandType.StoredProcedure,"P_GetPagedOrders2000",
    //new SqlParameter("@startIndex", AspNetPager1.StartRecordIndex),
    //new SqlParameter("@pageSize", AspNetPager1.PageSize));
    this.DataList1.DataBind();
    }#region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }/// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.AspNetPager1.PageChanged += new System.EventHandler(this.AspNetPager1_PageChanged);
    this.Load += new System.EventHandler(this.Page_Load);}
    #endregionprivate void AspNetPager1_PageChanged(object sender, System.EventArgs e)
    {
    bindData();
    }}
    }