加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 数据源不支持服务器端数据分页

发布时间:2020-12-16 07:19:22 所属栏目:asp.Net 来源:网络整理
导读:我目前正在尝试为我的gridview做分页,但是一旦我在gridview中允许分页,它就会给我这个错误:数据源不支持服务器端数据分页. 这是我的gridview代码: SqlDataReader reader = cmd.ExecuteReader(); GridView1.DataSource = reader; GridView1.DataSourceID =
我目前正在尝试为我的gridview做分页,但是一旦我在gridview中允许分页,它就会给我这个错误:数据源不支持服务器端数据分页.

这是我的gridview代码:

SqlDataReader reader = cmd.ExecuteReader();
        GridView1.DataSource = reader;
        GridView1.DataSourceID = null;
        GridView1.Visible = true;
        GridView1.AllowPaging= true;
        GridView1.DataBind(); 
        conn.Close();

解决方法

SqlDataReader只是前瞻性的.服务器端分页需要能够向后和向前遍历数据源.使用不同的数据源,如 SqlDataAdapter,它支持双向遍历.

示例(根据要求):

string query = string.Empty;
SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataAdapter da = null;
DataSet ds = null;

try {
    query = "SELECT * FROM table WHERE field = @value";
    conn = new SqlConnection("your connection string");

    cmd = new SqlCommand(query,conn);
    cmd.Parameters.Add("value",SqlDbType.VarChar,50).Value = "some value";

    da = new SqlDataAdapter(cmd);
    ds = new DataSet();
    da.Fill(ds);

    if (ds.Tables.Count > 0) {
        GridView1.DataSource = ds.Tables(0);
        GridView1.AllowPaging = true;
        GridView1.DataBind();
    }
} catch (SqlException ex) {
//handle exception
} catch (Exception ex) {
//handle exception
} finally {
    if (da != null) {
        da.Dispose();
    }
    if (cmd != null) {
        cmd.Dispose();
    }
    if (conn != null) {
        conn.Dispose();
    }
}

SqlDataAdapter也来自System.Data.SqlClient命名空间.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读