Marco Dinatsoli
Marco Dinatsoli

Reputation: 10590

Specified cast is not valid exception

This is my code

var result = (from row1 in table.AsEnumerable()
                          join row2 in tabelPopup.AsEnumerable()
                          on row1.Field<string>("CallID") equals
                          row2.Field<string>("callID")
                          where row1.Field<string>("Direction") == "I"
                          select new
                          {
                              Agent = row1.Field<string>("Agent"),
                              StartTime = row1.Field<DateTime>("StartTime"),
                              Reason = row2.Field<string>("Reason")
                          });

where table and tablePopup are datatable variables.

I got this exception:

Specified cast is not valid

on this code:

 new
                          {
                              Agent = row1.Field<string>("Agent"),
                              StartTime = row1.Field<DateTime>("StartTime"),
                              Reason = row2.Field<string>("Reason")
                          }

Upvotes: 0

Views: 106

Answers (2)

artm
artm

Reputation: 8584

Make sure your column definitions match the type you're using in row1.field<>. i.e. Agent is string, StartTime is datetime and Reason is string. This is likely due to StartTime not being a datetime type.

Upvotes: 2

mybirthname
mybirthname

Reputation: 18137

Probably StartTime is not from Type DateTime. Because of that you receive this exception. Try to convert it. If this is correct you should convert it to DateTime or just retrieve string value.

Upvotes: 1

Related Questions