I am trying to implement a function that retrieves the records that have been checked, the function then adds them to an ArrayList and then saves the ArrayList to ViewState. Essentially, there is a button that deletes the rows selected (via checkboxes) from the table. So after the Page_Load event, I click the button to delete the selected rows but I get a nullreference exception:
An exception of type 'System.NullReferenceException' occurred
but was not handled in user code
Additional information: Object reference not set to an instance of an object.
here is the function:
protected void GetSelectedRecords()
{
ArrayList arr;
if (ViewState["SelectedRecords"] != null)
arr = (ArrayList)ViewState["SelectedRecords"];
else
arr = new ArrayList();
CheckBox chkAll = new CheckBox();
chkAll = (CheckBox)grdUnsubscribe.FindControl("chkAll");
for (int i = 0; i < grdUnsubscribe.Rows.Count; i++)
{
if (chkAll.Checked == true)
{
if (!arr.Contains(grdUnsubscribe.DataKeys[i].Value))
{
arr.Add(grdUnsubscribe.DataKeys[i].Value);
}
}
else
{
CheckBox chk = (CheckBox)grdUnsubscribe.Rows[i].Cells[0].FindControl("chk");
if (chk.Checked)
{
if (!arr.Contains(grdUnsubscribe.DataKeys[i].Value))
{
arr.Add(grdUnsubscribe.DataKeys[i].Value);
}
}
else
{
if (arr.Contains(grdUnsubscribe.DataKeys[i].Value))
{
arr.Remove(grdUnsubscribe.DataKeys[i].Value);
}
}
}
}
ViewState["SelectedRecords"] = arr;
}
Thanks in advance