Answered linq to sql problem

  • Monday, February 04, 2013 11:28 PM
     
     

    In a C# 2008 desktop application, I have the following statement that give when an error when the code points to a different database:
     eRPTDataContext rptDataaddRVW = new eRPTDataContext();
     var eRPTaddRVW = (from a in rptDataaddRVW.eRPT_Transaction_Trackings
                       where a.Package_ID == packageId
                       select a).FirstOrDefault();

    Here is the error message I get:
    2013-02-04 16:13:15.3731|ERROR|erptsampleclient.eRPTSample|Error Processing --> Specified cast is not valid.
    2013-02-04 16:13:15.3731|ERROR|erptsampleclient.eRPTSample|************* Stack Trace *******************
    2013-02-04 16:13:15.3887|ERROR|erptsampleclient.eRPTSample|   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.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
       at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source)
       at erptsampleclient.eRPTSample.addNewReviewPackage() in C:\_svn\Cas_client\erptsampleclient\eRPTSample.cs:line 1308
    2013-02-04 16:13:15.3887|ERROR|erptsampleclient.eRPTSample|Error Processing --> Specified cast is not valid.
    2013-02-04 16:13:15.3887|ERROR|erptsampleclient.eRPTSample|************* Stack Trace *******************
    2013-02-04 16:13:15.4043|ERROR|erptsampleclient.eRPTSample|   at erptsampleclient.eRPTSample.addNewReviewPackage() in C:\_svn\Cas_client\erptsampleclient\eRPTSample.cs:line 1369
       at erptsampleclient.eRPTSample.Main(String[] args) in C:\_svn\Cas_client\erptsampleclient\eRPTSample.cs:line 154
    2013-02-04 16:13:23.3135|INFO|erptsampleclient.eRPTSample|******************ADD (CREATE) REVIEW (RVW) (METHOD 2 CALL)**********************
    2013-02-04 16:13:28.8359|INFO|erptsample.ERPTProxy|The number of attachments in the list are ->1
    2013-02-04 16:13:54.2171|INFO|erptsample.ERPTProxy|add review package successfully for rvw pkg id: RVW0204201300524 return code = 0
    2013-02-04 16:13:58.5227|INFO|erptsampleclient.eRPTSample|Review Package was created successfully for Package id: RVW0204201300524
    2013-02-04 16:13:58.5695|ERROR|erptsampleclient.eRPTSample|Error Processing --> Specified cast is not valid.
    2013-02-04 16:13:58.5695|ERROR|erptsampleclient.eRPTSample|************* Stack Trace *******************
    2013-02-04 16:13:58.5851|ERROR|erptsampleclient.eRPTSample|   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.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
       at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source)
                                                          
     When I change the code to the following:
    var eRPTaddRVW = (from a in rptDataaddRVW.eRPT_Transaction_Trackings
                       where a.Package_ID == packageId
                       select new { erptPackageID = a.Package_ID, erptFileLocation = a.File_Location }).FirstOrDefault();

    I do not get an error.
    The problem is I want to be able to update the database row I just accessed and the linq will not compile clean to let me do that.

    I am getting the follwowing error message when I try to access a specific field:
    Error 10 'object' does not contain a definition for 'erptFileLocation' and no extension method 'erptFileLocation' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) C:\_svn\Cas_client\erptsampleclient\eRPTSample.cs 1320 56 ERPTsampleclient
    Here is the code I am trying to add:
        if (eRPTaddRVW != null)
           {
             eRPTaddRVW.erptFileLocation = RVWFile; // save location of original spreadshgeet
             rptDataaddRVW.SubmitChanges();
    }

    Can you tell me what I can do to solve my problem?

All Replies

  • Monday, February 04, 2013 11:26 PM
     
     

    In a C# 2008 desktop application, I have the following statement that give when an error when the code points to a different database:
     eRPTDataContext rptDataaddRVW = new eRPTDataContext();
     var eRPTaddRVW = (from a in rptDataaddRVW.eRPT_Transaction_Trackings
                       where a.Package_ID == packageId
                       select a).FirstOrDefault();

    Here is the error message I get:
    2013-02-04 16:13:15.3731|ERROR|erptsampleclient.eRPTSample|Error Processing --> Specified cast is not valid.
    2013-02-04 16:13:15.3731|ERROR|erptsampleclient.eRPTSample|************* Stack Trace *******************
    2013-02-04 16:13:15.3887|ERROR|erptsampleclient.eRPTSample|   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.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
       at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source)
       at erptsampleclient.eRPTSample.addNewReviewPackage() in C:\_svn\Cas_client\erptsampleclient\eRPTSample.cs:line 1308
    2013-02-04 16:13:15.3887|ERROR|erptsampleclient.eRPTSample|Error Processing --> Specified cast is not valid.
    2013-02-04 16:13:15.3887|ERROR|erptsampleclient.eRPTSample|************* Stack Trace *******************
    2013-02-04 16:13:15.4043|ERROR|erptsampleclient.eRPTSample|   at erptsampleclient.eRPTSample.addNewReviewPackage() in C:\_svn\Cas_client\erptsampleclient\eRPTSample.cs:line 1369
       at erptsampleclient.eRPTSample.Main(String[] args) in C:\_svn\Cas_client\erptsampleclient\eRPTSample.cs:line 154
    2013-02-04 16:13:23.3135|INFO|erptsampleclient.eRPTSample|******************ADD (CREATE) REVIEW (RVW) (METHOD 2 CALL)**********************
    2013-02-04 16:13:28.8359|INFO|erptsample.ERPTProxy|The number of attachments in the list are ->1
    2013-02-04 16:13:54.2171|INFO|erptsample.ERPTProxy|add review package successfully for rvw pkg id: RVW0204201300524 return code = 0
    2013-02-04 16:13:58.5227|INFO|erptsampleclient.eRPTSample|Review Package was created successfully for Package id: RVW0204201300524
    2013-02-04 16:13:58.5695|ERROR|erptsampleclient.eRPTSample|Error Processing --> Specified cast is not valid.
    2013-02-04 16:13:58.5695|ERROR|erptsampleclient.eRPTSample|************* Stack Trace *******************
    2013-02-04 16:13:58.5851|ERROR|erptsampleclient.eRPTSample|   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.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
       at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source)
                                                          
     When I change the code to the following:
    var eRPTaddRVW = (from a in rptDataaddRVW.eRPT_Transaction_Trackings
                       where a.Package_ID == packageId
                       select new { erptPackageID = a.Package_ID, erptFileLocation = a.File_Location }).FirstOrDefault();

    I do not get an error.
    The problem is I want to be able to update the database row I just accessed and the linq will not compile clean to let me do that.

    I am getting the follwowing error message when I try to access a specific field:
    Error 10 'object' does not contain a definition for 'erptFileLocation' and no extension method 'erptFileLocation' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) C:\_svn\Cas_client\erptsampleclient\eRPTSample.cs 1320 56 ERPTsampleclient
    Here is the code I am trying to add:
        if (eRPTaddRVW != null)
           {
             eRPTaddRVW.erptFileLocation = RVWFile; // save location of original spreadshgeet
             rptDataaddRVW.SubmitChanges();
    }

    Can you tell me what I can do to solve my problem?

  • Tuesday, February 05, 2013 12:19 AM
     
     
    Does the table schema match on both databases?
  • Tuesday, February 05, 2013 2:56 AM
     
     

    As far as I know the schema match each other? Is there something in particular that I should look at?

  • Tuesday, February 05, 2013 8:06 AM
     
     Answered

    When you are doing  ... select a).FirstOrDefault(); LINQ selects all values from database, however select new { erptPackageID = a.Package_ID, erptFileLocation = a.File_Location }).FirstOrDefault();, only specific ones. Maybe you've updated database and haven't manage to update DBML file?

    Try to debug it, step inside while executing the LINQ select statement, Visual Studio will show you the exact variable where Specified cast is not valid.


    Please Mark as Reply and Vote as Helpful if I helped.

    Also please visit my blog http://msguy.net/

    • Marked As Answer by wendy elizabeth Saturday, February 09, 2013 1:09 AM
    •  
  • Tuesday, February 05, 2013 1:29 PM
     
     

    How do you step into a linq statement with visual studio? I click on the statement before the var variable gets its value from the linq statement. I click to go to the next statement and that is where the problem occurs? How can I use visual studio to see what the problem is?

    Is there a way with linq to do a select so that the next statement can be an update statement?

  • Tuesday, February 05, 2013 2:31 PM
     
     
    Step in is performed by hitting F11 key.

    Please Mark as Reply and Vote as Helpful if I helped.

    Also please visit my blog http://msguy.net/

    • Marked As Answer by wendy elizabeth Wednesday, February 06, 2013 2:30 AM
    • Unmarked As Answer by wendy elizabeth Wednesday, February 06, 2013 2:30 AM
    •  
  • Tuesday, February 05, 2013 4:02 PM
     
     
    helpful
  • Tuesday, February 05, 2013 10:29 PM
     
     
    make sure the column names, type, and order match in both tables.
    • Marked As Answer by wendy elizabeth Wednesday, February 06, 2013 2:30 AM
    • Unmarked As Answer by wendy elizabeth Wednesday, February 06, 2013 2:30 AM
    •  
  • Wednesday, February 06, 2013 2:31 AM
     
     
    How do you 'make sure the column names, type, and order match in both tables.'? This there a way to compare tables? If so, how do you do this type of comparison?
  • Thursday, February 07, 2013 2:30 AM
     
     Answered

    The easiest way is to look at them using SQL Server Management Studio (SSMS) or Visual Studio -> Server Explorer.

    If you're using a VS designer generated DBML file for LINQ to SQL; you have to make sure the SQL Database and DBML file match. If you change something on the database you will have to update your DBML too. Michael Samteladze mentioned it above, thought Id reitterate incase you missed it.

    Edit: Guess I should add, you still have to update both even if you create your own table mappings, etc.