Problem with PredicateExpression and Byte array

Posts   
 
    
gkearney
User
Posts: 2
Joined: 12-Nov-2010
# Posted on: 12-Nov-2010 02:56:38   

Hello,

First time poster, so hopefully I get you the information you need to help...

I am using v2.6

I am trying to create a PredicateExpression that will compare a byte array, named password, created in code to a varbinary(250) field, named Password, stored in a SQL-Server table.

Some sample code:



Private Shared Function ValidateUserAccount(ByVal password As Byte()) As Boolean

  Dim passwordPredicateBucket As New RelationPredicateBucket()

  passwordPredicateBucket.PredicateExpression.Add(CandidateResumeFields.Password = password)

  Dim candiateEmailAddressResumeCollection As New EntityCollection(Of CandidateResumeEntity)(New CandidateResumeEntityFactory())

  candiateEmailAddressResumeCollection = GetEntityCollection(CandidateResumeGraphType.SingleEntity, passwordPredicateBucket)

.
.
.

  Return something

End Function


I have obviously left out some parts of the code, but the code does work when I create a PredicateExpression comparing strings or any other value really. It's just when I try to get the VarBinary type involved.

I am pretty sure the "password" variable being passed into the function is a properly formatted Byte array. Part of my reason for this belief is that I have been successful in storing the value into the SQL-Server table as a byte array using the same "password" value.

The error I get happens on the candiateEmailAddressResumeCollection = GetEntityCollection(CandidateResumeGraphType.SingleEntity, passwordPredicateBucket) line and is:

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.

I am somewhat lost.

Thanks in advance for any assistance...

Thanks, Gary

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 12-Nov-2010 03:43:53   

gkearney wrote:

First time poster, so hopefully I get you the information you need to help...

Welcome aboard Gary!

gkearney wrote:

Some sample code: The error I get happens on the candiateEmailAddressResumeCollection = GetEntityCollection(CandidateResumeGraphType.SingleEntity, passwordPredicateBucket) line and is:

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.

So the field 'Password' is varbinary (which is byte[]) and the operator overloading thinks you want to compare it to a range of bytes (as you pass in an array of bytes). So if you look at the Generated Sql I think you will see an IN operator in the query.

To avoid this, try using a FieldCompareValue predicate class instead of the natural language expression:

passwordPredicateBucket.PredicateExpression.Add(new FieldCompareValuePredicate(
    CandidateResumeFields.Password, null, ComparisonOperator.Equal, password));

David Elizondo | LLBLGen Support Team
gkearney
User
Posts: 2
Joined: 12-Nov-2010
# Posted on: 12-Nov-2010 17:16:52   

Hi David,

Thanks for the welcome and help!

I needed to change the code you provided slightly. I replaced "null" with "nothing", maybe it's a VB thing, and everything worked fine. I had tried something very similar to your code but it did not have the "null/Nothing" parameter and that seems to make all the difference.

But things seems to be working as needed now!

Thanks, Gary