How to create predicate for byte[] ?

Posts   
 
    
veljkoz
User
Posts: 25
Joined: 31-Mar-2010
# Posted on: 31-Mar-2014 18:11:33   

Hi,

In the following piece of code, the second predicate causes the query to fail:

            byte[] encryptedPassword = GetEncryptedPassword(username, password);
            filter.PredicateExpression.AddWithAnd(ApplicationUserFields.Username == username);
            filter.PredicateExpression.AddWithAnd(ApplicationUserFields.Password == encryptedPassword);
  • In DB (MSSQL), the Password column is binary(16)
  • The field of ApplicationUser.Password is byte[]

How do I compare to make sure the array of bytes are a match?

Currently, the above code fails with:


SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryExecutionException: An exception was caught during the execution of a retrieval query: Failed to convert parameter value from a Byte to a Byte[].. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception. ---> System.InvalidCastException: Failed to convert parameter value from a Byte to a Byte[]. ---> System.InvalidCastException: Invalid cast from 'System.Byte' to 'System.Byte[]'.
   at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
   at System.Byte.System.IConvertible.ToType(Type type, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType, Boolean& coercedToDataFeed, Boolean& typeChanged, Boolean allowStreaming)
   --- End of inner exception stack trace ---
   at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType, Boolean& coercedToDataFeed, Boolean& typeChanged, Boolean allowStreaming)
   at System.Data.SqlClient.SqlParameter.GetCoercedValue()
   at System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc)
   at System.Data.SqlClient.SqlCommand.BuildParamList(TdsParser parser, SqlParameterCollection parameters)
   at System.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior behavior, String commandText, SqlParameterCollection parameters, _SqlRPC& rpc)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
   at SD.LLBLGen.Pro.ORMSupportClasses.RetrievalQuery.Execute(CommandBehavior behavior)
   --- End of inner exception stack trace ---
   at SD.LLBLGen.Pro.ORMSupportClasses.RetrievalQuery.Execute(CommandBehavior behavior)
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityMaterializerBase.Materialize(Func`4 valueReadErrorHandler, String& failureErrorText)
   at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.ExecuteMultiRowRetrievalQuery(IRetrievalQuery queryToExecute, IEntityFactory2 entityFactory, IEntityCollection2 collectionToFill, IFieldPersistenceInfo[] fieldsPersistenceInfo, Boolean allowDuplicates, IEntityFields2 fieldsUsedForQuery)
   at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchEntityCollectionInternal(QueryParameters parameters)
   at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchEntityCollection(QueryParameters parameters)
   at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchEntityCollection(IEntityCollection2 collectionToFill, IRelationPredicateBucket filterBucket, Int32 maxNumberOfItemsToReturn, ISortExpression sortClauses, IPrefetchPath2 prefetchPath, ExcludeIncludeFieldsList excludedIncludedFields, Int32 pageNumber, Int32 pageSize)
   at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchEntityCollection(IEntityCollection2 collectionToFill, IRelationPredicateBucket filterBucket, Int32 maxNumberOfItemsToReturn, ISortExpression sortClauses, IPrefetchPath2 prefetchPath)

(the rest of the stacktrace is our code)

When the password predicate is left out, the query works.

LLBLGen v4.1, released November 26th, 2013 Assembly SD.LLBLGen.Pro.ORMSupportClasses.dll, v4.1.0.0 [ie, 4.1.13.1213] Using the Adapter template group Runtime is .NET 4.0 Database is MS SQL Server 2008R2

I'd appreciate any info/ideas/etc...

Thanks, Veljko

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 31-Mar-2014 22:09:13   

Use the FieldCompareValuePredicate class instead of the natural-language predicate:

filter.PredicateExpression.AddWithAnd( new FieldCompareValuePredicate(
    ApplicationUserFields.Password, null, ComparisonOperator.Equal, encryptedPassword));;

This is why: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=18991&StartAtMessage=0&#106814

David Elizondo | LLBLGen Support Team
veljkoz
User
Posts: 25
Joined: 31-Mar-2010
# Posted on: 01-Apr-2014 10:01:36   

Simple and effective - thanks!

(I'm sorry my search of the forums didn't came up with that thread)