Question linq to sql error

jassie

Well-known member
Joined
Nov 13, 2012
Messages
61
Programming Experience
1-3
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(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(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?
 
Last edited by a moderator:
You said you pointed the application to a different database. Are the table structures identical? Open your data context file with an XML editor (right click, open with)

find the table you are querying in the XML and make sure the data types match.
 
Are you saying that fields and/or the entire table type could be different?

Also I wanted to mention that the table I am referring to now has triggers on them that I did not drag onto the designer? If I am not using the triggers in the code, do I need to drag those items onto the designer surface?
 
I am just saying if you open the data context file in XML view (not designer), if the table columns have even slightly different types you will get the error. For example be sure if a column is nvarchar(10) in your database table, then it must also be a nvarchar(10) in the datacontext file.

So look at the eRPT_Transaction_Trackings table details in the XML file.
 
Back
Top Bottom