Ezi
Ezi

Reputation: 2210

Sqlite with Entity framework - Error: "Specified cast is not valid"

The field type is money, if I put a '0' or a '1' in the field I get this error:

System.Reflection.TargetInvocationException:

Exception has been thrown by the target of an invocation. --->
System.InvalidCastException: Specified cast is not valid.

 at System.Data.SQLite.SQLiteDataReader.VerifyType(Int32 i, DbType typ)
 at System.Data.SQLite.SQLiteDataReader.GetBoolean(Int32 i)

 --- End of inner exception stack trace ---

This is from the model designer:

<Property Name="Amount" Type="decimal" Precision="53" />  

====

    <EdmScalarPropertyAttribute(EntityKeyProperty:=False, IsNullable:=True)>
<DataMemberAttribute()>
Public Property Amount() As Nullable(Of Global.System.Decimal)
    Get
        Return _Amount
    End Get
    Set(ByVal value As Nullable(Of Global.System.Decimal))
        OnAmountChanging(Value)
        ReportPropertyChanging("Amount")
        _Amount = StructuralObject.SetValidValue(value)
        ReportPropertyChanged("Amount")
        OnAmountChanged()
    End Set
End Property

Private _Amount As Nullable(Of Global.System.Decimal)
Private Partial Sub OnAmountChanging(value As Nullable(Of Global.System.Decimal))
End Sub

Private Partial Sub OnAmountChanged()
End Sub

Code to get error:

 Dim Query = From c In EnData.Transactions Where c.TranID = 660 ' this tran is the amount 0

        For Each tran In Query 'Error here

        Next

Upvotes: 3

Views: 4389

Answers (2)

Ezi
Ezi

Reputation: 2210

I Got the problem... its not the amount field. an other column which is a bit type, some times it doesn't like the way I wrote the false or true. that causes the problem.

The solution is to use 1 for true and 0 for false, that is always ok.

Upvotes: 4

user153923
user153923

Reputation:

I write SQL for Microsoft SQL Server. If the following example is invalid for SqLite, kindly let me know and I will remove this answer.

Question: What is: StructuralObject.SetValidValue(value)? Is this a custom function? If so, could you define that?

Perhaps modifying your VB code to do the following help:

Public Property Amount() As System.Decimal
    Get
        Return _Amount
    End Get
    Set(ByVal value As Global.System.Decimal)
        OnAmountChanging(Value)
        ReportPropertyChanging("Amount")
        If value = 0 Then
          _Amount = 0.00M
        Else If value = 1 Then
          _Amount = 1.00M
        Else
          _Amount = StructuralObject.SetValidValue(value)
        End If
        ReportPropertyChanged("Amount")
        OnAmountChanged()
    End Set
End Property

Typically, I prefer to place the "most true" case on the top, but my VB skills are not so good.

I hope this helps.

Upvotes: 0

Related Questions