0

I use this code to filter database records

if (!string.IsNullOrEmpty(_searchCriteria.MessageType))
        {
            var messageType = (AutotransferMessageType)Enum.Parse(typeof(AutotransferMessageType), _searchCriteria.MessageType, true);
            if (Enum.IsDefined(typeof(AutotransferMessageType), messageType))
            {
                criteriaQuery.CreateAlias("AutotransferInputRecord", "AutotransferInputRecord")
                    .Add(
                        Restrictions.Eq(
                            "AutotransferInputRecord." + AutotransferLogSearchCriteria.MessageTypePropertyName,
                            messageType));
            }
            else
            {
                criteriaQuery.Add(Restrictions.IsNull("AutotransferInputRecord"));
            }
        }

AutotransferMessageType is enumerable type

public enum AutotransferMessageType
    {
        [DisplayName("MT202")]
        [DatabaseName("MT202")]
        MT202,
        [DisplayName("MT210")]
        [DatabaseName("MT210")]
        MT210,
            //...
}

My filter outputs the results when I enter MT202, for example. (It's the right behavior).
When I input just number, for example, 202, I get no results (It's the right behavior too). But when I try to input some line, "mt", for example, I get error
Unexpected application error has been occured:
'Requested value 'mt' was not found.'

How to make the filter do not show any results when I input a line?

truslivii.lev
  • 701
  • 1
  • 8
  • 21

1 Answers1

1

Your error is coming from the line that parses the enum. Use Enum.TryParse instead:

AutotransferMessageType msgEnum;
var enumPrasedOk = Enum.TryParse(_searchCriteria.MessageType, true, out msgEnum);
if(enumPrasedOk){
    //Do something
}else{
    //Handle case where enum was not found for some reason (if need be)
}

Also please note that you can not look up the enum this way using it's description (in your case they are the same so it is ok).

JTMon
  • 3,189
  • 22
  • 24
  • Unfortunately, the project uses old version .NET Framework and I don't have Enum.TryParse() method here. – truslivii.lev Dec 05 '13 at 10:12
  • then wrap the parsing in a try/catch and in your catch do the failure code. – JTMon Dec 05 '13 at 10:17
  • I wrote this code http://pastebin.com/SffLg7ie and it works, but I don't know... is it right? It looks strange. – truslivii.lev Dec 05 '13 at 11:02
  • That is exactly what I meant, Except I would catch the specific exception as opposed to a general catch. I hate using try/catch this way, but sometimes it is the best solution. – JTMon Dec 05 '13 at 11:23
  • I cant understand what's wrong with my code, I tried this code http://pastebin.com/cEpfBNrD and filter outputs the right results when I enter MT202 or any other fully matching value, but if I input any value (not fully matching), filter outputs the all results from DB without any filtering. I break all my mind :( – truslivii.lev Dec 05 '13 at 13:07
  • That is because if the search criteria does not match any enum value, you are not restricting the query in any way (as far as I can tell from the pasted code anyway) and it will return all values. If you want the query to fail if the searchCriteria does not match an enum, you should have an else that returns without executing the query. – JTMon Dec 05 '13 at 14:42