locked
How to check for Null Valu RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have below code and want to check for Null Value

    txtAddr.Text = Request.QueryString["Address"].ToString();

    Thanks

    Tuesday, July 3, 2018 5:03 AM

Answers

  • User-369506445 posted

    hi

    you can use string.IsNullOrEmpty below like

     if (string.IsNullOrEmpty(Request.QueryString["Address"]))
                {
                    //is null
                }
                else
                {
    txtAddr.Text = Request.QueryString["Address"].ToString();
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 3, 2018 5:14 AM
  • User-369506445 posted

    also another form

    if (Request.QueryString["Address"]==null)
                {
                    //is null
                }
                else
                {
                    String sss = Request.QueryString["Address"].ToString();
                }

    or another form

     txtAddr.Text = Request.QueryString["Address"]==null ? "": Request.QueryString["Address"].ToString();
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 3, 2018 5:16 AM
  • User-1171043462 posted

    txtAddr.Text = Request.QueryString["Address"].ToString();

    1. By default it is string so no need to convert to string.
    2.  You can get in variable and then check.
      string address = Request.QueryString["Address"];
      if(!string.isNullOrEmpty(address))
      {
           //Not NULL
      }
      else
      {
          //NULL
      }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 3, 2018 7:28 AM

All replies

  • User-369506445 posted

    hi

    you can use string.IsNullOrEmpty below like

     if (string.IsNullOrEmpty(Request.QueryString["Address"]))
                {
                    //is null
                }
                else
                {
    txtAddr.Text = Request.QueryString["Address"].ToString();
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 3, 2018 5:14 AM
  • User-369506445 posted

    also another form

    if (Request.QueryString["Address"]==null)
                {
                    //is null
                }
                else
                {
                    String sss = Request.QueryString["Address"].ToString();
                }

    or another form

     txtAddr.Text = Request.QueryString["Address"]==null ? "": Request.QueryString["Address"].ToString();
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 3, 2018 5:16 AM
  • User-1716253493 posted

    use string format to avoid null exception 

    txtAddr.Text = string.Format("{0}",Request.QueryString["Address"]);
            //check the querystring is exist (you can't use tostring here)
            if (Request.QueryString["Address"]!=null) 
            {
                //if exist then you can check that the value is not empty
                if (string.IsNullOrEmpty(Request.QueryString["Address"].ToString())) 
                {
    
                }
            }


    Tuesday, July 3, 2018 7:11 AM
  • User-1171043462 posted

    txtAddr.Text = Request.QueryString["Address"].ToString();

    1. By default it is string so no need to convert to string.
    2.  You can get in variable and then check.
      string address = Request.QueryString["Address"];
      if(!string.isNullOrEmpty(address))
      {
           //Not NULL
      }
      else
      {
          //NULL
      }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 3, 2018 7:28 AM
  • User-330142929 posted

    Hi jagjitSingh,

    In my opinion, string.isnullorempty method is the best practice. But I want to provide another way.

    txtAddr.Text = Request.QueryString["Address"] ??"";

    The operator “??” is used to detect nullable data type.

    Besides, As oned_gk says, string.format is a good idea to avoid null exception.

    Feel free to let me know if you have any question.

    Best Regards

    Abraham.

    Wednesday, July 4, 2018 6:32 AM