I am created List of object class and return from CreateListFromTable method. But always Virtual method is calling and throwing exception. Data is not loading into List.
What could be the problem?
public static List<Customer> GetCustomer(string ID)
{
DataTable dt = new DataTable();
try
{
DatabaseGateway da = new DatabaseGateway();
dt = da.QueryForDataTable("proc_GetCustomer", ID);
}
catch (Exception ex)
{
LogMessage(ex.Message + ": " + ex.StackTrace, EventLogEntryType.Error);
}
return CreateListFromTable<Customer>(dt);
}
protected static List<T> CreateListFromTable<T>(DataTable dt) where T : BusinessObject, new()
{
List<T> list = new List<T>();
try
{
if (dt != null)
foreach (DataRow row in dt.Rows)
{
T t = new T();
t.Load(row);
list.Add(t);
}
}
catch (Exception exception)
{
}
return list;
}
/// <summary>
/// Virtual method which should be overriden by inherited types that support loading.
/// </summary>
/// <param name="row"></param>
protected virtual void Load(DataRow row)
{
throw new NotSupportedException("The object of type '" + this.GetType().Name +
"' does not support loading from DataRow.");
}
Does
CustomerinheritBusinessObject?And, do you override the method properly (assuming the code for the
Loadmethod from this class is isBusinessObject)?Also, I noticed that the
Loadmethod isprotectedand it looks like you’re calling a publicLoadmethod ont. This could cause some confusion as to what methods are actually being called since you can’t override a protected method with a public one (Compiler error:Inconsistent accessibility: base class 'BaseClass.ProtectedMethod' is less accessible than class 'DerivedClass.ProtectedMethod').