本文介绍了获取InvalidCastException未处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个实验代码,@Tim Schmelter在今天早些时候向我指出正确的方向。大多数几乎完全一样,在最耳目一新的工作,但它是在最后一行或第二行最后一行抛出一个 invalidCastException ,取决于我尝试。我不明白为什么会这样。

This is a take on some experimental code that @Tim Schmelter pointed me in the correct direction towards earlier this afternoon. The majority of it is almost exactly the same as what worked earler, but it is throwing a invalidCastException on the last line or second last line, depending whichever i try. I cannot see why this 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)", "");


推荐答案

可能不是最好的解决方案, 。

It might not be the best solution, but it works.

            string filePathStudent = System.IO.Path.GetFullPath("StudentInfo.txt");
        DataTable studentDataTable = new DataTable();

        studentDataTable.Columns.Add("Id", typeof(Int32));
        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");

        // Read in a file line-by-line, and store it
        var txtFileLine = File.ReadAllLines(filePathStudent).ToList();

        //Reads line splits data to colums at tab (ASCII value 9)
        txtFileLine.ForEach(line => studentDataTable.Rows.Add(line.Split((char)9)));


        List<int> rowsForColumn1 = studentDataTable.AsEnumerable().Select(x => x.Field<int>(0)).ToList();

        //Tests for empty Datatable
        foreach (DataRow row in studentDataTable.Rows)
        {
            if (row.IsNull("Id"))
                break;
            else
                //get max value from "Id" row.
                maxIDStdTable = rowsForColumn1.Max();
        }
    }

这篇关于获取InvalidCastException未处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 19:32