Answer the question
In order to leave comments, you need to log in
Object reference does not point to an instance of an object, error. How to fix?
Hello! !Tell me PLEASE WHAT IS THE PROBLEM? Gives an error
System.NullReferenceException: "Object reference does not point to an instance of an object."
result was null.
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel Files|*.xls;*.xlsx;*.xlsm" })
{
if (ofd.ShowDialog() == DialogResult.OK)
{
FileStream fs = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read);
Excel.IExcelDataReader reader = Excel.ExcelReaderFactory.CreateBinaryReader(fs);
reader.IsFirstRowAsColumnNames = true;
result = reader.AsDataSet();
CLEAR.Items.Clear();
foreach (DataTable dt in result.Tables)
{
dt.TableName = "tab";
CLEAR.Items.Add(dt.TableName);
}
// dataGridView1.DataSource = dt.DefaultView;
reader.Close();
}
}
}
foreach (DataTable dt in result.Tables)
Answer the question
In order to leave comments, you need to log in
Your library only works with the xls format, or other methods are used to work with the xlsx format. Read the documentation for your Excel library.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question