c# - Getting an InvalidCastException was Unhandled -


this take on experimental code @tim schmelter pointed me in correct direction towards earlier afternoon. majority of same worked earler, throwing invalidcastexception on last line or second last line, depending whichever try. cannot see why is.

boolean test = false; string filepathstudent = system.io.path.getfullpath("studentinfo.txt"); datatable studentdatatable = new datatable();  studentdatatable.columns.add("id", typeof(int)); studentdatatable.columns.add("studentid"); studentdatatable.columns.add("firstname"); studentdatatable.columns.add("lastname"); studentdatatable.columns.add("streetadd"); studentdatatable.columns.add("city"); studentdatatable.columns.add("state"); studentdatatable.columns.add("zip"); studentdatatable.columns.add("choice1"); studentdatatable.columns.add("credithrs1"); studentdatatable.columns.add("choice2"); studentdatatable.columns.add("credithrs2"); studentdatatable.columns.add("choice3"); studentdatatable.columns.add("credithrs3"); studentdatatable.columns.add("choice4"); studentdatatable.columns.add("credithrs4"); studentdatatable.columns.add("choice5"); studentdatatable.columns.add("credithrs5"); studentdatatable.columns.add("choice6"); studentdatatable.columns.add("credithrs6");  foreach (string line in file.readlines(filepathstudent)) {     datarow row = studentdatatable.rows.add();     string[] fields = line.split(new[] { (char)9 });     int id;     if (fields.length == 19 && int.tryparse(fields[0], out id))     {         row.setfield("id", id);         row.setfield("studentid", fields[1]);         row.setfield("firstname", fields[2]);         row.setfield("lastname", fields[3]);         row.setfield("streetadd", fields[4]);         row.setfield("city", fields[5]);         row.setfield("state", fields[6]);         row.setfield("zip", fields[7]);         row.setfield("choice1", fields[8]);         row.setfield("credithrs1", fields[9]);         row.setfield("choice2", fields[10]);         row.setfield("credithrs2", fields[11]);         row.setfield("choice3", fields[12]);         row.setfield("credithrs3", fields[13]);         row.setfield("choice4", fields[14]);         row.setfield("credithrs4", fields[15]);         row.setfield("choice5", fields[16]);         row.setfield("credithrs5", fields[17]);         row.setfield("choice6", fields[18]);         row.setfield("credithrs6", fields[19]);     } }  using (streamreader reader = new streamreader(filepathstudent)) {     string line1 = reader.readline();     if (line1 == null)         maxidstdtable = 0;     else         test = true;      reader.dispose();     reader.close(); }  if(test)     int maxidstdtable = studentdatatable.asenumerable().max(r => r.field<int>("id"));     //int maxidstdtable = (int)studentdatatable.compute("max(id)", ""); 

you have made 2 mistakes :
1) have create new datarow datatable.newrow()
2) sfter setting datarow have add datatable datatable.rows.add(youdatarow).
update code , try:

 foreach (string line in file.readlines(filepathstudent))     {         datarow row = studentdatatable.newrow();         string[] fields = line.split(new[] { (char)9 });         int id;         if (fields.length == 19 && int.tryparse(fields[0], out id))         {             row["id"]= id;             row["studentid"]= fields[1];             row["firstname"]= fields[2];             row[lastname"]= fields[3];             row["streetadd"]= fields[4];             row["city"]=fields[5];             row["state"]= fields[6];             row["zip"]=fields[7];             row["choice1"]= fields[8];             row["credithrs1"]= fields[9];             row["choice2"]= fields[10];             row["credithrs2"]= fields[11];             row[("choice3"]= fields[12];             row["credithrs3"]=, fields[13];             row["choice4"]= fields[14];             row["credithrs4"]= fields[15];             row["choice5"]= fields[16];             row["credithrs5"]= fields[17];             row["choice6"]= fields[18];             row["credithrs6"] =fields[19];         }        studentdatatable.rows.add(row);      } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -