I’m trying to filter a datatable with following code
private void Filter(string text)
{
int outText=0;
if (Int32.TryParse(text, out outText))
{
text = string.Empty;
}
DataTable DT = new DataTable();
DT = PinCDAO.GetArea().AsEnumerable().Where(r => r.Field<int>("AreaID")==Convert.ToInt32(outText) || (r.Field<string>("AreaDescription").Contains(text))).AsDataView().ToTable();
}
I’m getting the error “Specified cast is not valid”.because of the code
r => r.Field<int>("AreaID")==Convert.ToInt32(outText)
I’m sure about that AreaID column Contains Integers
plz help me to solve this.
Try out code – Handle null in you code
because “AreaID” is nullable field.
this code handles null value easily..
already answered question by me : "Specified cast is not valid" error in LINQ's orderby clause