Asp.net c#网格视图更新错误

时间:2017-02-26 05:49:39

标签: c# asp.net

每次尝试使用onrowupdating函数都会抛出System.NullReferenceException错误。 Gridview对页面加载没有问题,我也可以删除没有问题的行。这是我的代码:

.aspx的..

<asp:GridView ID="datagrid" runat="server" DataKeyNames="Emp_ID" CssClass="EmployeeGridView" EditRowStyle-CssClass="GridViewEditRow" PagerStyle-CssClass="pager" HeaderStyle-CssClass="header" RowStyle-CssClass="rows" 
        AutoGenerateColumns="false" AllowPaging="true" OnRowCancelingEdit="OnRowCancelingEdit" OnRowDeleting="OnRowDeleting"
        OnRowEditing="OnRowEditing" OnRowUpdating="OnRowUpdating"
        OnPageIndexChanging="OnPageIndexChanging" PageSize="10"
        >
        <Columns>
        <asp:BoundField ItemStyle-Width="150px" DataField="Emp_ID" HeaderText="Employee ID" />
        <asp:BoundField ItemStyle-Width="150px" DataField="FName" HeaderText="First Name" />
        <asp:BoundField ItemStyle-Width="150px" DataField="LName" HeaderText="Last Name" />
        <asp:BoundField ItemStyle-Width="150px" DataField="Email" HeaderText="Email" />
        <asp:BoundField ItemStyle-Width="100px" DataField="DOB" HeaderText="DOB" />
        <asp:BoundField ItemStyle-Width="150px" DataField="EmpRole" HeaderText="Role" />
        <asp:BoundField ItemStyle-Width="150px" DataField="Notes" HeaderText="Notes" />
        <asp:CommandField ShowEditButton="true" />
        <asp:CommandField ShowDeleteButton="true" />
        </Columns>
       </asp:GridView>

C#..

protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = datagrid.Rows[e.RowIndex];
        int Emp_ID = Convert.ToInt32(datagrid.DataKeys[e.RowIndex].Values[0]);
        string FName = (row.Cells[2].Controls[0] as TextBox).Text;
        string LName = (row.Cells[3].Controls[0] as TextBox).Text;
        string Email = (row.Cells[4].Controls[0] as TextBox).Text;
        string DOB = (row.Cells[5].Controls[0] as TextBox).Text;
        string role = (row.Cells[6].Controls[0] as TextBox).Text;
        string notes = (row.Cells[7].Controls[0] as TextBox).Text;
        string constr = ConfigurationManager.ConnectionStrings["MainFYPConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("UPDATE tbl_Employees SET FName=@FName, LName=@LName, Email=@Email, DOB=@DOB, EmpRole = Emp@Role, Notes = @Notes  WHERE (Emp_ID = @Emp_ID)"))
            {
                cmd.Parameters.AddWithValue("@Emp_ID", Emp_ID);
                cmd.Parameters.AddWithValue("@FName", FName);
                cmd.Parameters.AddWithValue("@Lname", LName);
                cmd.Parameters.AddWithValue("@Email", Email);
                cmd.Parameters.AddWithValue("@DOB", DOB);
                cmd.Parameters.AddWithValue("@EmpRole", role);
                cmd.Parameters.AddWithValue("@Notes", notes);
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
        datagrid.EditIndex = -1;
        this.BindGrid();
    }

如果有人知道出了什么问题,我们将非常感谢帮助

0 个答案:

没有答案