Load Excel in DatagridView

0

using the package nuget ExcelDataReader v.2.1.0

Excel upload button:

using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "call_center|*.xlsx",ValidateNames = true })
        {
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read);
                IExcelDataReader reader = ExcelReaderFactory.CreateBinaryReader(fs);
                reader.IsFirstRowAsColumnNames = true;
                result = reader.AsDataSet();

                cmbTables.Items.Clear();
                foreach(DataTable dt in result.Tables)
                {
                    cmbTables.Items.Add(dt.TableName);
                    reader.Close();
                }
            }
        }

Combo where I show the tables:

private void cmbTables_SelectedIndexChanged(object sender, EventArgs e)
    {
        dataLlamadas.DataSource = result.Tables[cmbTables.SelectedIndex];
    }

The problem is that it marks me:

System.NullReferenceException: 'Referencia a objeto no establecida como instancia de un objeto.'

Error occurred in:

foreach(DataTable dt in result.Tables)

The file currently has information.

    
asked by DoubleM 05.02.2018 в 22:22
source

0 answers