tags:

views:

189

answers:

1

Hi, I have the following classes (I've trimmed the code):

public class SqlWeightTrackerRepository : IWeightTrackerRepository
{
    private Table<WeightTracker> m_weightTrackerTable;

    //... constructor + other code here

    public IQueryable<WeightTracker> WeightTracker
    {
        get { return m_weightTrackerTable; }
    }
}

//CODE THAT GIVES THE ERROR

[IGNORE THIS LINE ->] *private IMembersRepository m_membersRepository = new SqlWeightTrackerRepository();* [I WAS TIRED AND ADDED IT INCORRECTLY]

private IWeightTrackerRepository m_weightTrackerRepository = new SqlWeightTrackerRepository();

//... some other code

        if (report.PreviousWeightTrackerId == 0)
            try { //If no session was found try find an entry in the database
                var tmp = 
                    (from x
                       in m_weightTrackerRepository.WeightTracker
                    where x.MemberId == report.Member.MemberId
                  orderby x.Created_On descending
                   select x).First();

            } catch (Exception e) {}

//STACK TRACE

at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) at System.Data.Linq.DataQuery1.System.Linq.IQueryProvider.Execute[S](Expression expression) at System.Linq.Queryable.First[TSource](IQueryable1 source) at WebUI.Controllers.MembersController.Welcome() in D:\PROJECTS\3DData\LoseOnlyToday\WebUI\Controllers\MembersController.cs:line 59'

This code gives an exception "Specified cast is not valid"

I know for a fact the query works - I used scott gu's linq debugger to check it - It returns 3 rows ... (DEBUGGER: http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx )

I'm at a total lose ... been looking for hours on google for a solution. Posting this is the last resort. Can someone help? Would be most appreciated!

+3  A: 

SqlWeightTrackerRepository implements IWeightTrackerRepository, but you're trying to cast it to IMembersRepository (w/ "IMembersRepository m_membersRepository").

Change:

private IMembersRepository m_membersRepository = new SqlWeightTrackerRepository();

to:

private IWeightTrackerRepository m_membersRepository = new SqlWeightTrackerRepository();

and you'd be golden.

Jess
Thanks for the reply! I feel like such a noob ...
etangreal
No prob - make sure to flag correct answers so others can benefit.
Jess