I am having trouble understanding why I am getting this exception thrown !
This is the code :
public static List<Employee> LoadEmployees()
{
List<Employee> emp = new List<Employee>();
try
{
using (SQLiteConnection cnn = new SQLiteConnection(LoadConnectionString()))
{
cnn.Open();
string query = "select * from Employee;";
SQLiteCommand cmd = new SQLiteCommand(query, cnn);
using (var reader = cmd.ExecuteReader())
{
int i = 0;
while (reader.Read())
{
emp[i].Id = reader.GetString(0);
emp[i].Name = reader.GetString(1);
emp[i].Gender = reader.GetString(2);
emp[i].Department = reader.GetString(3);
emp[i].Doj = reader.GetString(4);
emp[i].Email = reader.GetString(5);
emp[i].Phone = reader.GetString(6);
i++;
}
}
return emp;
}
}
catch (Exception ex)
{
emp = null;
return emp;
}
}
I tried debugging and found that - id = 'emp[i].Id' threw an exception of type 'System.ArgumentOutOfRangeException' Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.dll
I am not understanding why it throwing it because i have initialized i = 0;