0
if (System.Web.HttpContext.Current.Request.Form[day].ToString() != null)
{
    var test = System.Web.HttpContext.Current.Request.Form[day].ToString();
}

I have written these lines but when Form[day] doesn't contain any value, it gives an null object exception. How can I solve this ?

1
  • You can try maybe this, if (System.Web.HttpContext.Current.Request.Form[day] != null) { var test = System.Web.HttpContext.Current.Request.Form[day].ToString(); } Commented Aug 1, 2013 at 4:51

1 Answer 1

3

You have to check this value before access it. For example, like this:

var form = System.Web.HttpContext.Current.Request.Form;
if (form != null && !String.IsNullOrEmpty(day) && form.AllKeys.Contains(day))
{
    var test = form[day].ToString();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.