如何解决这个问题? “你调用的对象是空的。”

时间:2019-04-22 01:07:41

标签: c# asp.net gridview

描述:在执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

enter code here

protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        int EmployeeID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
        string name = (row.FindControl("EmployeeName") as TextBox).Text;
        string address = (row.FindControl("Address") as TextBox).Text;
        string email = (row.FindControl("Email") as TextBox).Text;
        string contact = (row.FindControl("ContactNo") as TextBox).Text;
        string gender = (row.FindControl("Gender") as TextBox).Text;
        string bday = (row.FindControl("Birthday") as TextBox).Text;
        string status = (row.FindControl("Status") as TextBox).Text;
        string pos = (row.FindControl("Position") as TextBox).Text;
        string dept = (row.FindControl("Department") as TextBox).Text;
        string query = "UPDATE EMployeeInfo SET EmployeeName=@EmployeeName, Address=@Address Email=@Email ContactNo=@ContactNo Gender=@Gender Birthday=@Birthday Status=@Status Position=@Position Department=@Department WHERE EmployeeID=@EmployeeID";
        string constr = @"Data Source = (localdb)\MSSQLLocalDB; Initial Catalog = sampleDB; Integrated Security = True";
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                //cmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
                cmd.Parameters.AddWithValue("@EmployeeName", name);
                cmd.Parameters.AddWithValue("@Address", address);
                cmd.Parameters.AddWithValue("@Email", email);
                cmd.Parameters.AddWithValue("@ContactNo", contact);
                cmd.Parameters.AddWithValue("@Gender", gender);
                cmd.Parameters.AddWithValue("@Birthday", bday);
                cmd.Parameters.AddWithValue("@Status", status);
                cmd.Parameters.AddWithValue("@Position", pos);
                cmd.Parameters.AddWithValue("@Department", dept);
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
        GridView1.EditIndex = -1;
        ShowData();
    }

错误发生在:字符串名称=(row.FindControl(“ EmployeeName”)as TextBox).Text;

0 个答案:

没有答案