1

Why am I getting a runtime binder exception when trying to execute the following query in Massive?

  public dynamic Find(string email, string type)
    {
        dynamic result = new ExpandoObject();
        result = this.Query(@"SELECT * FROM Addresses a 
            INNER JOIN Members m ON m.Id = a.MemberId 
            INNER JOIN AddressType at ON at.Id = a.AddressTypeId 
            WHERE m.Email = @0 AND at.Type = @1", new {email, type});
        return result;
    }

EDIT TO SHOW SOLUTION: I needed to change my query to ensure only one column with the name 'Id' was returned. I was getting a binding error because multiple columns in Members and Addresses had a column named 'Id'. To get a single result in my query I had to modify it to this:

result = this.Query(@"SELECT a.* FROM Addresses a 
            INNER JOIN Members m ON m.Id = a.MemberId 
            INNER JOIN AddressType at ON at.Id = a.AddressTypeId 
            WHERE m.Email = @0 AND at.Type = @1", new object[] { email, type }).Single();

Hope this helps someone else.

lloydphillips
  • 2,775
  • 2
  • 33
  • 58

1 Answers1

2

You are passing the args in an anonymous object, instead of an object array (params). It therefore becomes the first argument and fails to bind as your query expects 2 arguments. In addition Query returns type IEnumerable<dynamic>.

Change your code to this:

public dynamic Find(string email, string type)
{
    IEnumerable<dynamic> result;
    result = this.Query(@"SELECT a.* FROM Addresses a 
        INNER JOIN Members m ON m.Id = a.MemberId 
        INNER JOIN AddressType at ON at.Id = a.AddressTypeId 
        WHERE m.Email = @0 AND at.Type = @1", email, type);

    // decide how you want to handle multiple results here
    return result.FirstOrDefault();
}

Or you could use explicit object array:

        WHERE m.Email = @0 AND at.Type = @1", new object[] {email, type});
TheCodeKing
  • 19,064
  • 3
  • 47
  • 70
  • I'm still getting the binding error. I tried both ways you suggested. :s – lloydphillips Sep 10 '11 at 23:50
  • When debugging on the return line. I'm drilling down into result[].DynamicView and all the properties are present and populated but I'm still getting the binding error. I realised I had to change SELECT * to SELECT a.* as it was returning multiple Id fields. I've checked the SQL now in Management Studio and it seems fine. Not sure why I'm getting the binding error still. – lloydphillips Sep 11 '11 at 00:13
  • Hmm, digging deeper seems if I do a foreach loop on the result I can see the data. I can't access the first result though by doing result[0].Id. In an ideal world I need to be returning a single result. I'm not sure .Single() has the ability to pass in this sql. Will keep looking. – lloydphillips Sep 11 '11 at 00:25
  • Looking at the code `Query` returns type `IEnumerable`. – TheCodeKing Sep 11 '11 at 00:27
  • ok, got it working. Will add my code to my question so I can format it nicely. I added .Single to the end of my .Query(). This ensured only one result was returned. I'm still a tad confused though. :) – lloydphillips Sep 11 '11 at 00:29