1

I have a group of 7 checkboxes in checkboxlist user control. I build a string in the selectedIndexchanged event for the boxes checked, pass to ViewState and then pass ViewState to the Property. I do this because in the instance when no checkboxes are selected I want to handle null. The problem is no matter how I check for null, the system is throwing object reference error. This current setup works fine if at least one checkbox is checked but if none are checked it fails. How do I check for null? Should I be checking for null in the property or the host aspx page? I have researched difference ways to do this and I have tried many. My thought is using IsNullOrEmpty or IsNullOrWhiteSpace would be the correct way to go but neither are working.

User Control class - global variable

private string _daysOffInputString = string.Empty;

User Control Property

 public string DaysOffSelectedValues
                    {
                        get
                        {
                            if (string.IsNullOrEmpty(ViewState["DaysOff"].ToString()))
                            {
                                 _daysOffInputString = string.Empty;
                            }
                            else
                            {
                                _daysOffInputString = ViewState["DaysOff"].ToString();
                            }
                            return _daysOffInputString;
                        }
                            set { _daysOffInputString = value; }

User Control event

    protected void CbDaysOff_SelectedIndexChanged(object sender, EventArgs e)
            {
                CheckBoxList chkbx = (CheckBoxList)sender;

                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < chkbx.Items.Count; i++)
                {
                    if (chkbx.Items[i].Selected)
                    {
                        sb.Append(chkbx.Items[i].Text + ", ");
                    }

                    if (!String.IsNullOrEmpty(sb.ToString()))
                    {
                        //Remove last comma & space from string
                        _daysOffInputString = sb.ToString().Substring(0, sb.ToString().Length - 2);
                    }
                    else
                    {
                        _daysOffInputString = string.Empty;
                    }
                }

                ViewState["DaysOff"] = _daysOffInputString;
            }

aspx page - snippet where I retrieve uc property value:

      case 2:
           blnFlag = false;
           ucDaysOff uc3 = row.Cells[3].FindControl("ucDaysOff3") as ucDaysOff;      
           strAnswer = uc3.DaysOffSelectedValues;      //e.g. "Sat, Sun"

                            break;   

SOLUTION: In the user control property DaysOffSelectedValues I was casting ViewState["DaysOff"] to string before checking for null which was the problem. Here's the code that works:

public string DaysOffSelectedValues
    {
        get
        {
            if (ViewState["DaysOff"] == null)
            {
                //_daysOffInputString = string.Empty; }

                _daysOffInputString = "Nothing to see here.";
            }
            else
            {
                _daysOffInputString = ViewState["DaysOff"].ToString();
            }
            return _daysOffInputString;
        }
        set { _daysOffInputString = value; }
    }
Doreen
  • 654
  • 2
  • 14
  • 33
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – VDWWD Mar 29 '18 at 06:38
  • 1
    Maybe you should check if `ViewState["DaysOff"]` is null before calling ToString on it? – VDWWD Mar 29 '18 at 06:41
  • Your suggestion was the tip I needed! I had not considered NOT casting ViewState to string before checking for null because I knew it was a string. If you make this an answer I will mark as an answer. Otherwise I will update my question with the code that solved the problem. Thank you. – Doreen Mar 29 '18 at 15:09

1 Answers1

0

You should always check if the object, in this case ViewState, is null before using it. Lets say ViewState["DaysOff"] has not been created or has been removed.

Then this will throw a nullreference:

string str = String.IsNullOrEmpty(ViewState["DaysOff"].ToString());

Because you are not checking the ViewState object for null, but the string it is supposed to hold.

So do this

if (ViewState["DaysOff"] != null)
{
    string str = ViewState["DaysOff"].ToString();
}
VDWWD
  • 32,238
  • 19
  • 56
  • 70