I have a grid view in which I want to apply CRUD operation. When I click on Update button it says:

System.NullReferenceException: Object reference not set to an instance of an object at new_OCRS.Admin.gvReportCrime_RowUpdating(Object sender, GridViewUpdateEventArgs e) "

When I click on delete button , it does not show any error but value is not deleted.

Here is my code

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

        using (MySqlConnection con = new MySqlConnection(connectionString))
        {
            con.Open();
            string query = "UPDATE reportcrime SET [email protected],[email protected],Phone1,Phone2=Phone2,[email protected],[email protected],[email protected],[email protected],[email protected],[email protected] 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 Answers

0
skimad On

Your AddWithValue parameters do not match your Update Query. For example, you have below

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

But @ContactNo.1 is missing in your Update statement. Similarly there are others. You need to fix that.

0
Mishaal On
i have changed my code , @skimad but still same error.
string query = "UPDATE reportcrime SET [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected] 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();
0
marcelo On

The message "Object not set to instance of Object" means that you are trying to use an object which has not been initialized. That is, you either set it to null, or you never set it to anything at all. The runtime throwing a NullReferenceException always means the same thing: you are trying to use a reference.

Use a breakpoint in your method and check where the value is being null