I have a model named ClientesModel
. Inside it I have the fields:
public int id { get; set; }
public int codcli { get; set; }
public string nome { get; set; }
public string endereco { get; set; }
public string nr { get; set; }
public string bairro { get; set; }
public string cidade { get; set; }
I also have a model state called with the following fields:
public int id { get; set; }
public string sigla { get; set; }
How would you link these two models ? I tried to do this:
public int id { get; set; }
public int codcli { get; set; }
public string nome { get; set; }
public string endereco { get; set; }
public string nr { get; set; }
public string bairro { get; set; }
public string cidade { get; set; }
public EstadosModel estado { get; set; }
And when putting the information in model , use this way:
ClientesModel c = new ClientesModel();
c.id = Convert.ToInt32(Dr["id"]);
c.codcli = Convert.ToInt32(Dr["codcli"]);
c.nome = Convert.ToString(Dr["nome"]);
c.endereco = Convert.ToString(Dr["endereco"]);
c.nr = Convert.ToString(Dr["nr"]);
c.bairro = Convert.ToString(Dr["bairro"]);
c.cidade = Convert.ToString(Dr["cidade"]);
c.estado.id = 1;
c.estado.sigla = Convert.ToString(Dr["sigla"]);
But it ends up with an error.
Object reference not set to an instance of an object.
I know that I need to instantiate the object but not without where or how to instantiate an object.
I do not know if I'm messing around either and should use the 2 separate models .