I have a config data from a DHCP server that I put into classes like this:
public class DHCP
{
public DHCP()
{
this.Scopes = new List<Scope>();
}
public List<Scope> Scopes;
public class Scope
{
public Scope(string ScopeAddress, string SubnetMask, string State, string ScopeName, string Comment)
{
this.ScopeAddress = ScopeAddress;
this.SubnetMask = SubnetMask;
this.State = State;
this.ScopeName = ScopeName;
this.Comment = Comment;
}
public Scope(string ScopeAddress, string SubnetMask, string State, string ScopeName, string Comment, bool initClients)
{
this.ScopeAddress = ScopeAddress;
this.SubnetMask = SubnetMask;
this.State = State;
this.ScopeName = ScopeName;
this.Comment = Comment;
if (initClients)
this.Clients = new List<Client>();
}
public void InitClients()
{
this.Clients = new List<Client>();
}
public void InitReservations()
{
this.Reservations = new List<Reservation>();
}
public string ScopeAddress { get; set; }
public string SubnetMask { get; set; }
public string State { get; set; }
public string ScopeName { get; set; }
public string Comment { get; set; }
public List<Client> Clients;
public List<Reservation> Reservations;
}
public class Client
{
public Client(string IPAddress, string SubnetMask, string UniqueID, string LeaseExpires, string ClientType)
{
this.IPAddress = IPAddress;
this.SubnetMask = SubnetMask;
this.UniqueID = UniqueID;
this.LeaseExpires = LeaseExpires;
this.ClientType = ClientType;
}
public string IPAddress { get; set; }
public string SubnetMask { get; set; }
public string UniqueID { get; set; }
public string LeaseExpires { get; set; }
public string ClientType { get; set; }
public Reservation ClientReservation { get; set; }
}
public class Reservation
{
public Reservation(string IPAddress, string UniqueID, bool ReservationActive)
{
this.IPAddress = IPAddress;
this.UniqueID = UniqueID;
this.ReservationActive = ReservationActive;
}
public string IPAddress { get; set; }
public string UniqueID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Type { get; set; }
public bool ReservationActive { get; set; }
}
}
Then I have a List<DHCP.Client> that I use for DataSource on the gridview. When I set one of the data fields to ClientReservation.ReservationActive I get a not found error.
I have tried this with a List<> where this is no NULL data at all.
So I have to questions:
Can this be done at all?
If I have an object where Client.ClientReservation == null how can I handle that on DataBind() without an error?
I found a solution for both issues. By using a template field like this:
It get’s the value of the nested class (sub property) and ignores it without error if ClientReservation is NULL.