Home All Groups Group Topic Archive Search About

Linq to Sql Exception Specified cast is not valid

Author
30 Jun 2009 12:04 AM
Bill McCormick
I'm getting an exception *Specified cast is not valid* after calling
SubmitChanges for a DB insert. Seems to be some strange issue with an
association to a table. I've seen some postings from about a year ago that
some others were having the same problem, bu I didn't see any resolution.

Maybe this is a common problem that somebody knows about and could save me
the trouble of having to post all the details of my schema and code.

Thanks,

Bill

Author
30 Jun 2009 12:13 AM
Mr. Arnold
"Bill McCormick" <wpmccormick@community.nospam> wrote in message
news:%23N8XtYR%23JHA.1376@TK2MSFTNGP02.phx.gbl...
> I'm getting an exception *Specified cast is not valid* after calling
> SubmitChanges for a DB insert. Seems to be some strange issue with an
> association to a table. I've seen some postings from about a year ago that
> some others were having the same problem, bu I didn't see any resolution.
>
> Maybe this is a common problem that somebody knows about and could save me
> the trouble of having to post all the details of my schema and code.
>

The class object based on the schema has null data,  the data for a given
field based on the schema is an invalid type of data or the class object
itself is not the expected type of class.





__________ Information from ESET NOD32 Antivirus, version of virus signature database 4197 (20090629) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
Are all your drivers up to date? click for free checkup

Author
30 Jun 2009 2:02 AM
Bill McCormick
On 6/29/2009 7:13 PM, Mr. Arnold wrote:
Show quoteHide quote
>
> "Bill McCormick" <wpmccormick@community.nospam> wrote in message
> news:%23N8XtYR%23JHA.1376@TK2MSFTNGP02.phx.gbl...
>> I'm getting an exception *Specified cast is not valid* after calling
>> SubmitChanges for a DB insert. Seems to be some strange issue with an
>> association to a table. I've seen some postings from about a year ago
>> that some others were having the same problem, bu I didn't see any
>> resolution.
>>
>> Maybe this is a common problem that somebody knows about and could
>> save me the trouble of having to post all the details of my schema and
>> code.
>>
>
> The class object based on the schema has null data,  the data for a
> given field based on the schema is an invalid type of data or the class
> object itself is not the expected type of class.

Obviously. However, since I can make this problem disappear by removing an
Linq to SQL association (on a table my code is not even tying to update), I
think it runs a little deeper.

Thanks Anyway.
Author
30 Jun 2009 3:36 AM
Mr. Arnold
Show quote Hide quote
"Bill McCormick" <wpmccormick@community.nospam> wrote in message
news:4A49722D.6080002@community.nospam...
> On 6/29/2009 7:13 PM, Mr. Arnold wrote:
>>
>> "Bill McCormick" <wpmccormick@community.nospam> wrote in message
>> news:%23N8XtYR%23JHA.1376@TK2MSFTNGP02.phx.gbl...
>>> I'm getting an exception *Specified cast is not valid* after calling
>>> SubmitChanges for a DB insert. Seems to be some strange issue with an
>>> association to a table. I've seen some postings from about a year ago
>>> that some others were having the same problem, bu I didn't see any
>>> resolution.
>>>
>>> Maybe this is a common problem that somebody knows about and could save
>>> me the trouble of having to post all the details of my schema and code.
>>>
>>
>> The class object based on the schema has null data,  the data for a given
>> field based on the schema is an invalid type of data or the class object
>> itself is not the expected type of class.
>
> Obviously. However, since I can make this problem disappear by removing an
> Linq to SQL association (on a table my code is not even tying to update),
> I think it runs a little deeper.
>
> Thanks Anyway.

I remove all associations off the ADO.NET Entity Framework Entity Model to
avoid associations placed on the entities due to constraints and due to
navigation paths the model may take that can lead to a  constraint table
being inserted into or updated based on the navigation path from another
table being inserted into or updated.

One can take full control of the EF model. It doesn't look like one can do
that with a Link-2-SQL model.


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4198 (20090629) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
Author
30 Jun 2009 1:22 PM
Bill McCormick
On 6/29/2009 10:36 PM, Mr. Arnold wrote:
Show quoteHide quote
>
> "Bill McCormick" <wpmccormick@community.nospam> wrote in message
> news:4A49722D.6080002@community.nospam...
>> On 6/29/2009 7:13 PM, Mr. Arnold wrote:
>>>
>>> "Bill McCormick" <wpmccormick@community.nospam> wrote in message
>>> news:%23N8XtYR%23JHA.1376@TK2MSFTNGP02.phx.gbl...
>>>> I'm getting an exception *Specified cast is not valid* after calling
>>>> SubmitChanges for a DB insert. Seems to be some strange issue with
>>>> an association to a table. I've seen some postings from about a year
>>>> ago that some others were having the same problem, bu I didn't see
>>>> any resolution.
>>>>
>>>> Maybe this is a common problem that somebody knows about and could
>>>> save me the trouble of having to post all the details of my schema
>>>> and code.
>>>>
>>>
>>> The class object based on the schema has null data,  the data for a
>>> given field based on the schema is an invalid type of data or the
>>> class object itself is not the expected type of class.
>>
>> Obviously. However, since I can make this problem disappear by
>> removing an Linq to SQL association (on a table my code is not even
>> tying to update), I think it runs a little deeper.
>>
>> Thanks Anyway.
>
> I remove all associations off the ADO.NET Entity Framework Entity Model
> to avoid associations placed on the entities due to constraints and due
> to navigation paths the model may take that can lead to a  constraint
> table being inserted into or updated based on the navigation path from
> another table being inserted into or updated.
>
> One can take full control of the EF model. It doesn't look like one can
> do that with a Link-2-SQL model.
>

Sorry, I'm having a difficult time parsing what you said. Maybe the comma
key is broken on your keyboard? ;)

Anyway, the associations should work as advertised; if not, the EF is broken
and MS needs to fix it.


Thanks
Author
30 Jun 2009 1:40 PM
Mr. Arnold
"Bill McCormick" <wpmccormick@community.nospam> wrote in message
news:4A4A1190.5090106@community.nospam...
>
> Anyway, the associations should work as advertised; if not, the EF is
> broken and MS needs to fix it.
>

Associations work as far as I am concerned when I need them. But when I
don't needed associations due constraints on tables, I know how to remove
the associations and take full control of the model.

If one can't think outside the box and problem solve, then one has got
trouble in the wind.


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4198 (20090629) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
Author
30 Jun 2009 12:54 AM
Bill McCormick
On 6/29/2009 7:04 PM, Bill McCormick wrote:
> I'm getting an exception *Specified cast is not valid* after calling
> SubmitChanges for a DB insert. Seems to be some strange issue with an
> association to a table. I've seen some postings from about a year ago
> that some others were having the same problem, bu I didn't see any
> resolution.
>
> Maybe this is a common problem that somebody knows about and could save
> me the trouble of having to post all the details of my schema and code.
>

Here's the stacktrace:

    at
System.Data.Linq.IdentityManager.StandardIdentityManager.SingleKeyManager`2.TryCreateKeyFromValues(Object[]
values, V& v)
    at
System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache`2.Find(Object[]
keyValues)
    at
System.Data.Linq.IdentityManager.StandardIdentityManager.Find(MetaType type,
Object[] keyValues)
    at System.Data.Linq.CommonDataServices.GetCachedObject(MetaType type,
Object[] keyValues)
    at System.Data.Linq.ChangeProcessor.GetOtherItem(MetaAssociation assoc,
Object instance)
    at System.Data.Linq.ChangeProcessor.BuildEdgeMaps()
    at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
    at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
    at System.Data.Linq.DataContext.SubmitChanges()
    at ScAggScale.AppPages.NewTicket.btnTar_Click(Object sender,
RoutedEventArgs e) in mysourcefile.xaml.cs:line 104

I googled on TryCreateKeyFromValues and found a few items that indicate this
may be a Linq bug and I might be able SP my way out of it. I thought I had
all the latest stuff ?!?!
Author
30 Jun 2009 1:58 AM
Bill McCormick
On 6/29/2009 7:54 PM, Bill McCormick wrote:
Show quoteHide quote
> On 6/29/2009 7:04 PM, Bill McCormick wrote:
>> I'm getting an exception *Specified cast is not valid* after calling
>> SubmitChanges for a DB insert. Seems to be some strange issue with an
>> association to a table. I've seen some postings from about a year ago
>> that some others were having the same problem, bu I didn't see any
>> resolution.
>>
>> Maybe this is a common problem that somebody knows about and could
>> save me the trouble of having to post all the details of my schema and
>> code.
>>
>
> Here's the stacktrace:
>
>    at
> System.Data.Linq.IdentityManager.StandardIdentityManager.SingleKeyManager`2.TryCreateKeyFromValues(Object[]
> values, V& v)
>    at
> System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache`2.Find(Object[]
> keyValues)
>    at
> System.Data.Linq.IdentityManager.StandardIdentityManager.Find(MetaType
> type, Object[] keyValues)
>    at System.Data.Linq.CommonDataServices.GetCachedObject(MetaType type,
> Object[] keyValues)
>    at System.Data.Linq.ChangeProcessor.GetOtherItem(MetaAssociation
> assoc, Object instance)
>    at System.Data.Linq.ChangeProcessor.BuildEdgeMaps()
>    at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode
> failureMode)
>    at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
>    at System.Data.Linq.DataContext.SubmitChanges()
>    at ScAggScale.AppPages.NewTicket.btnTar_Click(Object sender,
> RoutedEventArgs e) in mysourcefile.xaml.cs:line 104
>
> I googled on TryCreateKeyFromValues and found a few items that indicate
> this may be a Linq bug and I might be able SP my way out of it. I
> thought I had all the latest stuff ?!?!

Installed VS2008 SP1 and no change; same problem.

Can I get some MSDN help on this please.

Thanks,

Bill
Author
30 Jun 2009 8:30 AM
Patrice
Could it be that the datatype for the fk and the pk doesn't match ? Which db
are you using  ? Do you have a fk constraint defined in the db ?

--
Patrice

"Bill McCormick" <wpmccormick@community.nospam> a écrit dans le message de
groupe de discussion : #N8XtYR#JHA.1***@TK2MSFTNGP02.phx.gbl...
Show quoteHide quote
> I'm getting an exception *Specified cast is not valid* after calling
> SubmitChanges for a DB insert. Seems to be some strange issue with an
> association to a table. I've seen some postings from about a year ago that
> some others were having the same problem, bu I didn't see any resolution.
>
> Maybe this is a common problem that somebody knows about and could save me
> the trouble of having to post all the details of my schema and code.
>
> Thanks,
>
> Bill
>
Author
30 Jun 2009 1:28 PM
Bill McCormick
On 6/30/2009 3:30 AM, Patrice wrote:
> Could it be that the datatype for the fk and the pk doesn't match ? Which db
> are you using  ? Do you have a fk constraint defined in the db ?
>

Using MS Sql Server Express. The fk/pk datatype is VarChar(10). How can I
tell if I have a fk constraint?
Author
30 Jun 2009 12:24 PM
vanderghast
I get that error, sometimes, in the automatically generated code, when a
null value is involved. Have to explicitly test the IsFieldNull before to
avoid getting that error (bad design, but I think it predates nullable
valuetype with the framework, so I excuse those who wrote that code
generator).

Vanderghast, Access MVP



Show quoteHide quote
"Bill McCormick" <wpmccormick@community.nospam> wrote in message
news:%23N8XtYR%23JHA.1376@TK2MSFTNGP02.phx.gbl...
> I'm getting an exception *Specified cast is not valid* after calling
> SubmitChanges for a DB insert. Seems to be some strange issue with an
> association to a table. I've seen some postings from about a year ago that
> some others were having the same problem, bu I didn't see any resolution.
>
> Maybe this is a common problem that somebody knows about and could save me
> the trouble of having to post all the details of my schema and code.
>
> Thanks,
>
> Bill
>
Author
30 Jun 2009 2:02 PM
Bill McCormick
On 6/30/2009 7:24 AM, vanderghast wrote:
> I get that error, sometimes, in the automatically generated code, when a
> null value is involved. Have to explicitly test the IsFieldNull before
> to avoid getting that error (bad design, but I think it predates
> nullable valuetype with the framework, so I excuse those who wrote that
> code generator).
>

My code does not insert or update records in the table that is linked to
with the association in question. Maybe an illustration would be in order?
There are 3 tables in this example, X, Y ,Z.

X->Y, X<-Z  (- = one; <,> = many)


The code sort of looks like this, where now x,y and z are Entity objects of
the X,Y & Z Entity collections/lists:

x = comboBox.SelectedItem

y.field1 = val1
y.field2 = val2
y.field3 = val3

x.Y.Add(y)

db.SubmitChanges()

This works unless there is record in table z for the record x.

I stop here in the interest of not creating too much fog.

Thanks.

Bookmark and Share