For my homework I have to make a XAML project using a csv file to show pictures of zoo animals along with other info which is included in the code.
It's almost done but when I run the program it gives me this error:
System.ObjectDisposedException: 'Cannot read from a closed TextReader.'
private async void Button_Click(object sender, RoutedEventArgs e)
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.FileTypeFilter.Add(".csv");
var file = await picker.PickSingleFileAsync();
if (file == null)
{
tbFileStatus.Text = "(not a valid file chosen)";
return;
}
tbFileStatus.Text = file.Path;
using (var fileAccess = await file.OpenReadAsync())
{
using (var stream = fileAccess.AsStreamForRead())
{
using (var reader = new StreamReader(stream))
{
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var lvAnimals = csv.GetRecords<Animal>();
}
while (!reader.EndOfStream)
{
var row = reader.ReadLine();
var parts = row.Split(',');
var number = (parts[0]);
var name = parts[1];
var species = parts[2];
var latin_name = parts[3];
var cage = parts[4];
var picture = parts[5];
var llvAnimals = new Animal
{
Number = number,
Name = name,
Species = species,
Latin_Name = latin_name,
Cage = cage,
Picture = picture
};
lvAnimals.ItemsSource = llvAnimals;
}
}
}
}
}