4
votes

I got a problem whenever I pass char ":" from the user interface. NHibernate mistakes it as a named parameter and throws an error, since there isn't any value for it.

Exception is :-

Not all named parameters have been set: [%] [SELECT COUNT (*) FROM Table t WHERE t.FirstName LIKE ':%' AND t.ID IN (38, 20)]"

Is there any work around?

2

2 Answers

2
votes

You are probably creating the query in a wrong way (concatenating strings, maybe?)

All of these work:

session.CreateCriteria<Test2>()
       .Add(Restrictions.Like("FirstName", ":%"))
       .UniqueResult<Test2>();

session.CreateQuery("from Test2 where FirstName like :expr")
       .SetParameter("expr", ":%")
       .UniqueResult<Test2>();
0
votes

You need to escape special characters when using SQL LIKE. Try passing the parameter as @"\" + ":";.