views:

63

answers:

2

Hi- I'm running a stored procedure through L2S and it's returning 'Specified cast is not valid'. The stored proc is returning data when ran manually and when I step thru it, everything is fine until it tries to create the row object in "foreach (var row in result)".

var q = new db();
var result = q.GetNearbyLocations(latitude, longitude,searchDistance);
foreach (var row in result)
   {
      var c = new Clinic()
                {
                   Name = row.CLINIC_NAME.Trim(),
                   Address1 = row.DRADR1.Trim()...

Ideas?

+1  A: 

Your sporc isn't actually invoked until a foreach statement is executed. Therefore, ensure that LINQ to SQL can correctly map data returned from the sproc to your objects.

Anton Gogolev
+1  A: 

That is usually caused by a data type mismatch, e.g. if the stored procedure returns a int and that is mapped to a string, or if the stored proc return a varchar(1) and that is mapped to a System.Char.

KristoferA - Huagati.com
The sproc dynamically creates a string variable and executes it. Linq doesn't like this. In order for Linq to read the return types in correctly, I have to comment it out and put in an equivalent sql statement. Somehow this wasn't happening properly as in the designer.cs, the return type was an int, rather that a recordset. I changed the sproc, reimported and change the sproc back to executing the string variable. All is happy now. Thanks All!
asp316