0
I am having a problem in including objects from other classes. What would be the best solution for this?
For example, I have a Service class and attributes that are other classes, such as Customer and Vehicle. As listed below:
public class Servico
{
public int Id { get; set; }
public string Nome { get; set; }
public string Descricao { get; set; }
public Cliente cliente { get; set; }
public Veiculo veiculo{ get; set; }
public double Valor { get; set; }
public DateTime DataServico { get; set; }
public Servico()
{
new Cliente(),
new Veiculo()
}
}
Plus I have the method inside the controller
that will include the data and receive the objects.
As I demonstrate just below...
[HttpPost]
[Route("Incluir")]
public HttpResponseMessage Incluir(Servico servico)
{
try
{
bool res = false;
if (servico == null)
{
throw new ArgumentNullException("servico");
}
Conexao cx = new Conexao();
cx.ConectarBase();
SqlCommand command = new SqlCommand();
command.Connection = cx.connection;
command.CommandText = "EXEC INCLUIR_SERVICO @NOME, @DESCRICAO, @CLIENTE, @VEICULO, @VALOR";
command.Parameters.AddWithValue("nome", servico.Nome);
command.Parameters.AddWithValue("descricao", ((object)servico.Descricao) ?? DBNull.Value);
command.Parameters.AddWithValue("cliente", ((object)servico.veiculo.Id) ?? DBNull.Value);
command.Parameters.AddWithValue("veiculo", ((object)servico.cliente.Id) ?? DBNull.Value);
command.Parameters.AddWithValue("valor", ((object)servico.Valor) ?? DBNull.Value);
int i = command.ExecuteNonQuery();
res = i > 0;
cx.DesconectarBase();
return Request.CreateResponse(HttpStatusCode.OK, res);
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex);
}
}
However, in the objects I have null error Exception in the properties Servico.Cliente.Id and Servico.Veiculo.ID.
Please, can someone help me understand how I should resolve this issue?.