在网格视图中,我的行未更新或删除

时间:2019-05-12 08:17:37

标签: c# mysql asp.net

我有一个网格视图,我想在其中应用CRUD操作。当我单击“更新”按钮时,它说:

  

System.NullReferenceException:对象引用未设置为new_OCRS.Admin.gvReportCrime_RowUpdating(Object sender,GridViewUpdateEventArgs e)上的对象实例“

当我单击“删除”按钮时,它没有显示任何错误,但未删除值。

这是我的代码

 protected void gvReportCrime_RowUpdating(object sender, GridViewUpdateEventArgs e)
 {
    try
    {

        using (MySqlConnection con = new MySqlConnection(connectionString))
        {
            con.Open();
            string query = "UPDATE reportcrime SET Name=@Name,Phone1=@Phone1,Phone1,Phone2=Phone2,Email=@Email,Gender=@Gender,City=@City,Address=@Address,CrimeType=@CrimeType,CrimeDetail=@CrimeDetail WHERE CNIC = @CNIC";
            MySqlCommand mscmd = new MySqlCommand(query, con);
            mscmd.Parameters.AddWithValue("@Name", (gvReportCrime.Rows[e.RowIndex].FindControl("txtName") as TextBox).Text.Trim());
            mscmd.Parameters.AddWithValue("@ContactNo.1", (gvReportCrime.Rows[e.RowIndex].FindControl("txtPhone1") as TextBox).Text.Trim());
            mscmd.Parameters.AddWithValue("@ContactNo.2", (gvReportCrime.Rows[e.RowIndex].FindControl("txtPhone2") as TextBox).Text.Trim());
            mscmd.Parameters.AddWithValue("@EmailAddress", (gvReportCrime.Rows[e.RowIndex].FindControl("txtEmail") as TextBox).Text.Trim());
            mscmd.Parameters.AddWithValue("@Gender", (gvReportCrime.Rows[e.RowIndex].FindControl("txtGender") as TextBox).Text.Trim());
            mscmd.Parameters.AddWithValue("@CityName", (gvReportCrime.Rows[e.RowIndex].FindControl("txtCity") as TextBox).Text.Trim());
            mscmd.Parameters.AddWithValue("@PostalAddress", (gvReportCrime.Rows[e.RowIndex].FindControl("txtAddress") as TextBox).Text.Trim());
            mscmd.Parameters.AddWithValue("@CrimeType", (gvReportCrime.Rows[e.RowIndex].FindControl("txtCrimeType") as TextBox).Text.Trim());
            mscmd.Parameters.AddWithValue("@CrimeDetail", (gvReportCrime.Rows[e.RowIndex].FindControl("txtCrimeDetail") as TextBox).Text.Trim());
            mscmd.Parameters.AddWithValue("@CNICNO",Convert.ToString(gvReportCrime.DataKeys[e.RowIndex].Value.ToString()));
            mscmd.ExecuteNonQuery();
            gvReportCrime.EditIndex = -1;
            BindData();

        }
    }

    catch (Exception ex)
    {
            Response.Write("" + ex);
    }

 }

 protected void gvReportCrime_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {

            using (MySqlConnection con = new MySqlConnection(connectionString))
            {
                con.Open();
                string query = "DELETE FROM reportcrime SET WHERE CNIC = @CNIC";
                MySqlCommand mscmd = new MySqlCommand(query, con);
                mscmd.Parameters.AddWithValue("@CNIC", Convert.ToString(gvReportCrime.DataKeys[e.RowIndex].Value.ToString()));
                mscmd.ExecuteNonQuery();
                BindData();

            }
        }
        catch (Exception ex)
        {
            Response.Write("" + ex);
        }
    }

3 个答案:

答案 0 :(得分:0)

您的AddWithValue参数与您的更新查询不匹配。例如,您有以下

mscmd.Parameters.AddWithValue("@ContactNo.1",...)

但是您的Update语句中缺少@ ContactNo.1。 同样,还有其他人。您需要解决此问题。

答案 1 :(得分:0)

i have changed my code , @skimad but still same error.
string query = "UPDATE reportcrime SET Name=@Name,Phone1=@Phone1,Phone2=@Phone2,Email=@Email,Gender=@Gender,City=@City,Address=@Address,CrimeType=@CrimeType,CrimeDetail=@CrimeDetail WHERE CNIC = @CNIC";
                    MySqlCommand mscmd = new MySqlCommand(query, con);
                    mscmd.Parameters.AddWithValue("@Name", (gvReportCrime.Rows[e.RowIndex].FindControl("txtName") as TextBox).Text.Trim());
                    mscmd.Parameters.AddWithValue("@txtPhone1", (gvReportCrime.Rows[e.RowIndex].FindControl("txtPhone1") as TextBox).Text.Trim());
                    mscmd.Parameters.AddWithValue("@txtPhone2", (gvReportCrime.Rows[e.RowIndex].FindControl("txtPhone2") as TextBox).Text.Trim());
                    mscmd.Parameters.AddWithValue("@txtEmail", (gvReportCrime.Rows[e.RowIndex].FindControl("txtEmail") as TextBox).Text.Trim());
                    mscmd.Parameters.AddWithValue("@Gender", (gvReportCrime.Rows[e.RowIndex].FindControl("txtGender") as TextBox).Text.Trim());
                    mscmd.Parameters.AddWithValue("@City", (gvReportCrime.Rows[e.RowIndex].FindControl("txtCity") as TextBox).Text.Trim());
                    mscmd.Parameters.AddWithValue("@Address", (gvReportCrime.Rows[e.RowIndex].FindControl("txtAddress") as TextBox).Text.Trim());
                    mscmd.Parameters.AddWithValue("@CrimeType", (gvReportCrime.Rows[e.RowIndex].FindControl("txtCrimeType") as TextBox).Text.Trim());
                    mscmd.Parameters.AddWithValue("@CrimeDetail", (gvReportCrime.Rows[e.RowIndex].FindControl("txtCrimeDetail") as TextBox).Text.Trim());
                    mscmd.Parameters.AddWithValue("@CNIC",Convert.ToString(gvReportCrime.DataKeys[e.RowIndex].Value.ToString()));
                    mscmd.ExecuteNonQuery();

答案 2 :(得分:0)

消息“ 对象未设置为对象的实例”表示您正在尝试使用尚未初始化的对象。也就是说,您要么将其设置为null,要么根本不将其设置为任何值。 运行时抛出 NullReferenceException 的含义总是相同的:您正在尝试使用引用。

在您的方法中使用断点,并检查该值在哪里为空