linq - Add rows to a DataTable from a query expression result -


i trying dynamically add rows datatable query expression result. however, getting below exception : "cannot cast dbnull.value type 'system.int16'. please use nullable type."

this code:

foreach (var linqitem in linqresult)                     resultdt.rows.add(linqitem.gettype().getproperties().select(p => p.getvalue(linqitem, null)).toarray()); 

please me figure out figure out issue.

additional code:

 var linqresult = misdtrow in misuserdetailsdt.asenumerable()                              join deptdtrow in departmentinfodt.asenumerable()                              on misdtrow.field<int16>("departmentid") equals deptdtrow.field<int32>("deptid")                              select                              new                              {                                  username = misdtrow["loginname"],                                  employeeid = misdtrow["employeeid"],                                  employeename = misdtrow["employeename"],                                  foundin = misdtrow["foundin"],                                  statusinmis = (bool)misdtrow["isactive"] == true ? "inactive" : "active",                                  department = deptdtrow["deptname"]  }; 

datatable creation:

 datatable resultdt = new datatable();  propertyinfo[] propinfo = linqresult.first().gettype().getproperties();  foreach (propertyinfo property in propinfo)      resultdt.columns.add(property.name, property.propertytype); 

i creating datatable dynamically query result.

try change int? instead of int

"cannot cast dbnull.value type 'system.int16'. please use nullable type."

this because trying cast nullable type int.

check database table column allow null. can cast nullable type.

for example :

misdtrow.field<int?>("departmentid") equals deptdtrow.field<int?>("deptid") 

Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -