Excel Data reader read data from Excel sheet in order compare it with the UI dropdown values and see if they exists
I am trying to read data from excel sheet using below code
// methods to read data and parse the data into populate collection
using ExcelDataReader;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
namespace GorillaUITests.PageObjects
{
public class ExcelRead
{
public static List<Datacollection> dataCol = new List<Datacollection>();
public static DataTable ExcelToDataTable(string fileName)
{
using (var stream = File.Open(fileName, FileMode.Open, FileAccess.Read))
{
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
var result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
UseColumnDataType = true,
ConfigureDataTable = (data) => new ExcelDataTableConfiguration()
{
UseHeaderRow = true
}
});
//Get all the Tables
DataTableCollection table = result.Tables;
//Store it in DataTable
DataTable resultTable = table["Sheet1"];
//return
return resultTable;
}
}
}
public static void PopulateInCollection(string fileName)
{
DataTable table = ExcelToDataTable(fileName);
//Iterate through the rows and columns of the Table
for (int row = 1; row <= table.Rows.Count; row++)
{
for (int col = 0; col < table.Columns.Count; col++)
{
Datacollection dtTable = new Datacollection()
{
rowNumber = row,
colName = table.Columns[col].ColumnName,
colValue = table.Rows[row - 1][col].ToString()
};
//Add all the details for each row
dataCol.Add(dtTable);
}
}
}
/// <summary>
/// Read data from Collection
/// </summary>
/// <param name="rowNumber"></param>
/// <param name="columnName"></param>
/// <returns></returns>
public static string ReadData(int rowNumber, string columnName)
{
try
{
//Retriving Data using LINQ to reduce much of iterations
string data = (from colData in dataCol
where colData.colName == columnName && colData.rowNumber == rowNumber
select colData.colValue).SingleOrDefault();
//var datas = dataCol.Where(x => x.colName == columnName && x.rowNumber == rowNumber).SingleOrDefault().colValue;
return data.ToString();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
}
}
public class Datacollection
{
public int rowNumber { get; set; }
public string colName { get; set; }
public string colValue { get; set; }
}
}
// Test to see if we are able to read data
public void ReadDataFromExcel(string user, string pass)
{
// populate data in collection
try
{
ExcelRead.PopulateInCollection(@"C:\Users\username\Documents\GorillaUITests\GorillaUITests\Tests\Data\ReadDataTest.xlsx");
Debug.WriteLine("UserName:" + ExcelRead.ReadData(1, "UserName"));
Debug.WriteLine("Email:" + ExcelRead.ReadData(1, "Email"));
Debug.WriteLine("***********");
Debug.WriteLine("UserName:" + ExcelRead.ReadData(2, "UserName"));
Debug.WriteLine("Email:" + ExcelRead.ReadData(2, "Email"));
Debug.WriteLine("***********");
Debug.WriteLine("Username:" + ExcelRead.ReadData(2, "UserName"));
Debug.WriteLine("Email:" + ExcelRead.ReadData(2, "Email"));
Debug.WriteLine("***********");
catch (Exception e)
{
Console.WriteLine(e.Message);
}
And I see this error below
“Object reference not set to an instance of an object.”
Not sure what I am missing, Any inputs would be appreciated