locked
Trim().IsEmpty() Control RRS feed

  • Question

  • User-770252936 posted

    Hey. I'm doing a check on form submission in the following way

    var tAdsoyad = Request.Form["tAdSoyad"];
    
    if (IsPost)
    {
       if (tAdSoyad.Trim().IsEmpty())
       {
          ModelState.AddError("tAdSoyad", "• Adınızı ve soyadınızı giriniz");
       }
    }

    Send form by clicking on the Send button when no problem. After you submit the form, but when I refresh the page with F5 like I'm getting an error screen.

    Friday, June 27, 2014 11:48 AM

Answers

  • User281315223 posted

    You might want to ensure that your tAdSoyad variable exists prior to attempting to Trim it :

    var tAdsoyad = Request.Form["tAdSoyad"];
    
    if (IsPost)
    {
       // Ensure your variable isn't null before attmpeting to trim it
       if (tAdSoyad != null && tAdSoyad.Trim().IsEmpty())
       {
            ModelState.AddError("tAdSoyad", "• Adınızı ve soyadınızı giriniz");
       }
    }

    Additionally, you could probably use either the String.IsNullOrEmpty() method or String.IsNullOrWhiteSpace() method as an easy way of resolving this :

    if (IsPost)
    {
       if (String.IsNullOrEmpty(tAdSoyad))
       {
           ModelState.AddError("tAdSoyad", "• Adınızı ve soyadınızı giriniz");
       }
    }

    or :

    if (IsPost)
    {
       if (String.IsNullOrWhiteSpace(tAdSoyad))
       {
           ModelState.AddError("tAdSoyad", "• Adınızı ve soyadınızı giriniz");
       }
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 27, 2014 3:02 PM

All replies

  • User1853794821 posted

    Changes are the form field is empty.  if you try to do .Trimon a null object, you get that error.

    Try 

    if(tAdSoyad.IsEmpty()))
    
    {
        ModelState.AddError(..........
    }
    else
    {
        tAdSoyad=tAdSoyad.Trim();
    }



    Friday, June 27, 2014 2:01 PM
  • User281315223 posted

    You might want to ensure that your tAdSoyad variable exists prior to attempting to Trim it :

    var tAdsoyad = Request.Form["tAdSoyad"];
    
    if (IsPost)
    {
       // Ensure your variable isn't null before attmpeting to trim it
       if (tAdSoyad != null && tAdSoyad.Trim().IsEmpty())
       {
            ModelState.AddError("tAdSoyad", "• Adınızı ve soyadınızı giriniz");
       }
    }

    Additionally, you could probably use either the String.IsNullOrEmpty() method or String.IsNullOrWhiteSpace() method as an easy way of resolving this :

    if (IsPost)
    {
       if (String.IsNullOrEmpty(tAdSoyad))
       {
           ModelState.AddError("tAdSoyad", "• Adınızı ve soyadınızı giriniz");
       }
    }

    or :

    if (IsPost)
    {
       if (String.IsNullOrWhiteSpace(tAdSoyad))
       {
           ModelState.AddError("tAdSoyad", "• Adınızı ve soyadınızı giriniz");
       }
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 27, 2014 3:02 PM
  • User-821857111 posted

    Additionally, you could probably use either the String.IsNullOrEmpty() method or String.IsNullOrWhiteSpace() method as an easy way of resolving this :

    IsEmpty uses String.IsNullOrEmpty:

    public static bool IsEmpty(this string value)
    {
        return String.IsNullOrEmpty(value);
    }

    Saturday, June 28, 2014 4:12 AM