Pergunta

I am using this filter in my AutoCompeteBox.

bool SearchBird(string search, object value)
        {
            if (value != null)
            {
                AllBirds datasourceValue = value as AllBirds;
                string name = datasourceValue.primary_language;

                if (name.ToLower().StartsWith(search.ToLower()))
                return true;

            }
            // If no match, return false. 
            return false;
        } 

Everything is working well when all entries have some value, but when there is any empty record it's crushing giving an error with null exception (which is understandable as there is no value). Tried do smth with extra if or else statement but still getting the same error.

Foi útil?

Solução

more better if u use if else?

add this after if, may be help

else {
 return false;
}

Outras dicas

I've done that like that.

if (value != null)
            {
                AllBirds datasourceValue = value as AllBirds;
                string name = datasourceValue.primary_language;

                if (name == null)
                    return false;

                if (name.ToLower().StartsWith(search.ToLower()))
                return true;

            }
            // If no match, return false. 
            return false;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top