asp.net linq查询数据表

时间:2018-09-05 10:46:12

标签: c# asp.net linq webforms

我一直在关注本教程,以通过asp.net中的模式弹出窗口实现编辑/更新功能:

http://msdnaspdotnettuto.blogspot.com/2015/01/aspnet-gridview-crud-using-twitter.html

这是我的代码:

public partial class GroupSummary1 : System.Web.UI.Page
    {
        DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                LoadGroupSummary();

            }
        }

        private void LoadGroupSummary()
        {
            try
            { 
            UserBLL userBLL = new UserBLL();
            dt = userBLL.GetGroupSummary(2, 2017);
            gvGroupSummary.DataSource = dt;
            gvGroupSummary.DataBind();
            }
            catch (SqlException ex)
            {
                System.Console.Error.Write(ex.Message);
            }
        }

        protected void gvGroupSummary_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName.Equals("detail"))
            {
                string code = gvGroupSummary.DataKeys[index].Value.ToString();
                IEnumerable<DataRow> query = from i in dt.AsEnumerable()
                                             where i.Field<int>("GroupID").Equals(code)
                                             select i;
                DataTable detailTable = query.CopyToDataTable<DataRow>();
                DetailsView1.DataSource = detailTable;
                DetailsView1.DataBind();
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append(@"<script type='text/javascript'>");
                sb.Append("$('#detailModal').modal('show');");
                sb.Append(@"</script>");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "DetailModalScript", sb.ToString(), false);
            }
        }
    }

当我在网格视图上选择“详细信息”按钮时,会发生以下错误:

  

System.ArgumentNullException HResult = 0x80004003消息=值   不能为null。参数名称:source Source = StackTrace:

在这一行:

 IEnumerable<DataRow> query = from i in dt.AsEnumerable()
                                             where i.Field<int>("GroupID").Equals(code)
                                             select i;

dt对象为NULL,我怀疑这是问题的根源。但是,我已经在上面像在教程中一样声明了它。

有什么想法吗?

谢谢

1 个答案:

答案 0 :(得分:1)

LoadGroupSummary仅在初始页面加载时触发,而在回发时不触发。单击您的详细信息按钮以呼叫gvGroupSummary_RowCommand()将导致回发。

只需从页面加载中删除if (!IsPostBack)

protected void Page_Load(object sender, EventArgs e)
{
    LoadGroupSummary();
}

编辑:

可能值得一提的是,如果userBLL.GetGroupSummary()返回的任何数据都是静态的,则可能只应加载一次。例如:

private void LoadGroupSummary()
{
    try
    { 
        if (Session["GroupSummary"] != null)
        {
            dt = (DataTable)Session["GroupSummary"];
        }
        else
        {
            UserBLL userBLL = new UserBLL();
            dt = userBLL.GetGroupSummary(2, 2017);
            Session["GroupSummary"] = dt;                    
        }
        gvGroupSummary.DataSource = dt;
        gvGroupSummary.DataBind();
    }
    catch (SqlException ex)
    {
          System.Console.Error.Write(ex.Message);
    }
}