ASP.net Grid View异常帮助 [英] ASP.net Grid View exception help

查看:56
本文介绍了ASP.net Grid View异常帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net应用程序中使用Gridview
我对此有疑问

我正在使用一个下拉列表选择一个用户,并从该用户中填充Gridview中的记录
当我从下拉列表中选择用户时,它将引发异常数据源不支持服务器端数据分页."

选择的代码是==>

I am using Gridview in my asp.net application
I am having a problem with it

I am using a dropdown to select a user and from which the records in Gridview will populated
when i select a user from dropdown it throws an exception "The data source does not support server-side data paging."

The code of Selecting is ==>

protected void btnok_Click(object sender, ImageClickEventArgs e)
{
    try
    {
        if (Convert.ToInt32(dropdownuser.SelectedIndex) != 0)
        {
            DBConnection d = new DBConnection();
            d.OpenConnection();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT  tblUser.FirstName, tblUser.LastName, tblUser.Designation,tblAttendance.TimeArrive, tblAttendance.TimeLeave, tblAttendance.WorkingHour, tblAttendance.DayofAttend as Day, tblAttendance.DateofAttend as Date,tblAttendance.Reasonoflate as Reason, tblAttendance.Status FROM tblAttendance INNER JOIN  tblUser ON tblAttendance.UserID = tblUser.UserId AND tblUser.UserId =" + dropdownuser.SelectedValue;
            cmd.Connection = d.myconn;
            GridView1.DataSource = cmd.ExecuteReader();
            GridView1.DataBind();
            d.CloseConnection();
        }
        //LoadUsers();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}




GridView的代码是==>




And the code of GridView is ==>

<asp:GridView id="GridView1" runat="server" Width="567px" ForeColor="#333333" CellPadding="4"

                            GridLines="None" Height="140px" AllowPaging="True">
<FooterStyle BackColor="#990000" ForeColor="White" Font-Bold="True"></FooterStyle>
<RowStyle BackColor="#FFFBD6" ForeColor="#333333"></RowStyle>
<SelectedRowStyle BackColor="#FFCC66" ForeColor="Navy" Font-Bold="True"></SelectedRowStyle>
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center"></PagerStyle>
<HeaderStyle BackColor="#990000" ForeColor="White" Font-Bold="True"></HeaderStyle>
<AlternatingRowStyle BackColor="White"></AlternatingRowStyle>
</asp:GridView>

推荐答案

使用sqladapter绑定gridview来填充数据表或数据集
Bind the gridview using sqladapter to fill a datatable or dataset
DataTable dt = new DataTable();       
        SqlDataAdapter ad1 = new SqlDataAdapter(cmd);
        ad1.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();


仅需详细说明, SqlDataReader仅转发,因此不支持服务器端数据分页,因此会出现错误.根据其他答案的建议,使用DataAdapter填充数据集,然后使用它.

类似的讨论:链接1 [ ^ ],链接2 [ ^ ]
Just to elaborate more, SqlDataReader is forward only, thus does not support server side data paging and hence the error. As suggested in other answer, use DataAdapter to fill a dataset and then use it.

Similar discussions: Link 1[^], Link 2[^]


这篇关于ASP.net Grid View异常帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆