"Input string was not in a correct format" 用于文件上传

"Input string was not in a correct format" for file upload

我正在尝试使用 C# 将 .csv 文件数据导入(上传)到我在 asp.net 中的数据库,但收到通知说输入字符串的格式不正确。请帮忙。谢谢!

Error line: dt.Rows[dt.Rows.Count - 1][i] = cell;

// Upload and save file
    string csvPath = Server.MapPath("~/FileUploads/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
    FileUpload1.SaveAs(csvPath);

    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[11] { new DataColumn("Name", typeof(string)),
        new DataColumn("NRIC", typeof(string)),
        new DataColumn("Gender", typeof(string)),
        new DataColumn("BirthYear", typeof(int)),
        new DataColumn("Number", typeof(int)),
        new DataColumn("PostalCode", typeof(int)),
        new DataColumn("CoursesAttended", typeof(string)),
        new DataColumn("Language", typeof(string)),
        new DataColumn("DateAttended", typeof(string)),
        new DataColumn("TrainingPlaces", typeof(int)),
        new DataColumn("Remarks", typeof(string)) });

    string csvData = File.ReadAllText(csvPath);

    foreach (string row in csvData.Split('\n'))
    {
        if (!string.IsNullOrEmpty(row))
        {
            dt.Rows.Add();
            int i = 0;

            foreach (string cell in row.Split(','))
            {
                dt.Rows[dt.Rows.Count - 1][i] = cell;
                i++;
            }
        }
    }

    string conString = ConfigurationManager.ConnectionStrings["SilverInfocomm"].ConnectionString;

    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlBulkCopy bulk = new SqlBulkCopy(con))
        {
            // Set database table name
            bulk.DestinationTableName = "dbo.FileUploads";
            con.Open();
            bulk.WriteToServer(dt);
            con.Close();
        }
    }

检查.csv 文件数据和数据表的顺序是否相同。 在 BirthYear、Number、PostalCode、TrainingPlaces 等列中,如果不同格式的数据将引发此错误,则为 int 数据类型。 上传前检查。 使用 try catch 块显示消息。

我尝试时会出现此类错误消息 'System.ArgumentException' 类型的异常发生在 System.Data.dll 但未在用户代码中处理

Additional information: Input string was not in a correct format.Couldn't store <Family Name> in BirthYear Column.  Expected type is Int32.