gofor.net
gofor.net

Reputation: 4298

asp.net - Specified cast not valid Linq

I got this error when I am trying the below code

IEnumerable<DataRow> projjType = from projT in dtProjList.AsEnumerable()
                                 where projT.Field<int?>("ProjectId") == projId && 
                                 projT.Field<int?>("FilterId") == 1 
                                 select projT;

and the stack trace of error is

at System.Data.DataRowExtensions.UnboxT`1.NullableField[TElem](Object value)
   at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
   at Folder.Projects.<>c__DisplayClass28.<repProjectList_ItemDataBound>b__1e(DataRow projT) in D:\Folder\Project.aspx.cs:line 1061
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()

where the dtProjList is DataTable,and Table Structure is as follows,

ProjectId (int)
ProjectName (string)
ProjectThumb (string)
FilterTypeId (int)
FilterId (int)
FilterType (string)

all are not null values.Can anybody please help me out.

thanks in advance.

Update :: This is onlyrecord in my table,is the error because of the single record ??

ProjectId   ProjectName ProjectThumb    FilterTypeId    FilterId    FilterType
   1    Datamaster  small_14.jpg         1             1               Final

Upvotes: 0

Views: 995

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

If all fields are not null, then cast fields to int instead of nullable int?

var projjType = from projT in dtProjList.AsEnumerable()
                where projT.Field<int>("ProjectId") == projId && 
                      projT.Field<int>("FilterId") == 1 
                select projT;

Upvotes: 4

Related Questions