0

I want to make a multiple delete checkbox where user can delete multiple data from gridview easily. However I stumble an error:

System.NullReferenceException: Object reference not set to an instance of an object.

Line 242:               if (chk != null)
Line 243:               {
Line 244:                   chk.Checked = arr.Contains(grdadmin.DataKeys[i].Value);
Line 245:                   if (!chk.Checked)
Line 246:                       chkAll.Checked = false;

There it shows where the error occur, can someone help me, what need to be fixed?

private void SetData()
    {
        int currentCount = 0;
        CheckBox chkAll = (CheckBox)grdadmin.HeaderRow.Cells[0].FindControl("chkAll");
        chkAll.Checked = true;
        ArrayList arr = (ArrayList)ViewState["SelectedRecords"];
        for (int i = 0; i < grdadmin.Rows.Count; i++)
        {
            CheckBox chk = (CheckBox)grdadmin.Rows[i].Cells[0].FindControl("chk");
            if (chk != null)
            {
                chk.Checked = arr.Contains(grdadmin.DataKeys[i].Value);
                if (!chk.Checked)
                    chkAll.Checked = false;
                else
                    currentCount++;
            }
        }
        hfCount.Value = (arr.Count - currentCount).ToString(); 
    }

Here is my code, help is really much appreciated.

0

1 Answer 1

0

Try

 CheckBox chkAll = (CheckBox)grdadmin.HeaderRow.FindControl("chkAll");

(or show the aspx markup)

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, I have tried it but it still giving me the same error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.