1
I have a model calling for 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 called states with the following fields:
public int id { get; set; }
public string sigla { get; set; }
How would you do to link these two models? I tried to do so:
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 by putting the information on model, use in that 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 ends up presenting error.
Object Reference not set to an instance of an Object.
I know I need to instantiate the object but not without where or how to instantiate an object.
I don’t know if I’m making a mess, too models separated.
Thank you so much for the tips.. I am now starting to develop in Layers and although I am doing Analysis College, I have not yet reached this subject and I am without whom to ask some basic questions.. With respect to the default Constructor public Clientesmodel() { this.Stateosmodel = new Stateosmodel(); } would not be: public Clientedel() { this.status = new Status(); }
– Fabio Zanardo
Exactly. Corrected ^^
– Vinícius
Oh yes.!! You are Kra.!! Thank you very much for the help..!!
– Fabio Zanardo