私はリストに次のデータを含むCSVファイルを保存しようとしています:入力文字列
Iに成功App_DataににCSVを保存し、次のコードを持っていますフォルダ:
Input string was not in a correct format.
Source Error:
Line 31: CSVProducts = from line in File.ReadAllLines(path).Skip(1)
Line 32: let columns = line.Split(',')
Line 33: select new Product
Line 34: {
Line 35: Id = int.Parse(columns[0]),
Source File: C:\Users\Documents\Visual Studio 2015\Projects\Website\Models\Repository.cs
Line: 33
:
public static IEnumerable<Product> CSVProducts;
public static IEnumerable<Product> CSVToList(HttpPostedFileBase CSVFile)
{
if (CSVFile == null || CSVFile.ContentLength == 0)
{
throw new InvalidOperationException("No file has been selected for upload or the file is empty.");
}
// saves the file into a directory in the App_Data folder
var fileName = Path.GetFileName(CSVFile.FileName);
var path = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data/"), fileName);
CSVFile.SaveAs(path);
CSVProducts = from line in File.ReadAllLines(path).Skip(1)
let columns = line.Split(',')
select new Product
{
Id = int.Parse(columns[0]),
Barcode = columns[13],
Name = columns[1],
CategoryName = columns[9],
Description = columns[2],
Price = int.Parse(columns[4])
};
return CSVProducts;
}
しかし、リストを取得するLINQクエリは、次のエラーメッセージを表示します
私は、Visual Studioでデバッグするとき、私は、LINQクエリ内の変数の内部で何を見ることはできませんが、私は内部のCSVProductsあり、それは「現在」プロパティがnullの値を持っているかを見ることができます。
これは、製品とカテゴリのクラスです:
public class Product
{
public int Id { get; set; }
public string Barcode { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
[Required]
public Byte[] Image { get; set; }
[Required]
public Decimal Price { get; set; }
[Required]
public bool ShowOnIndex { get; set; }
}
public class Category
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public Byte[] Image { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
[0] ''エラーが発生した列の値は何ですか? – David
'columns [0]'はProductIDですか? – IAbstract
これは基本的に解析の問題です。あなたのコードごとに、0と4の2つの列を解析しています。これらの値を確認してください。数字以外の文字がいくつかあります。 – Kalyan