0
Hello, I am having a write problem in Sql Server, I have two tables(post and user), when I try to save the C#record, the error appears: (System.Nullreferenceexception: 'Object Reference not set to an instance of an Object. ' Objectcransference.Usuario.Cargo.get returned null.)
My user table (Idcargo) refers to the position table (Idcargo and Description) so that Combobox presents the available positions for choice.
However, when trying to write stops on the line as per photo and informs the error message, my program is layered.
I would like the help of the community to solve this mistake. NOTE: My Cargo Object, is as object inside object.
Grateful !!
BLL = business layer for insertion
public String Inserir(Usuario usuario)
{
try
{
acessoBancoDeDadosSqlServer.LimparParametros();
//acessoBancoDeDadosSqlServer.AdicionarParametros("@IdUsuario", usuario.IdUsuario);
acessoBancoDeDadosSqlServer.AdicionarParametros("@Nome", usuario.Nome);
acessoBancoDeDadosSqlServer.AdicionarParametros("@StatusUsu", usuario.StatusUsu);
acessoBancoDeDadosSqlServer.AdicionarParametros("@CPF", usuario.CPF);
//acessoBancoDeDadosSqlServer.AdicionarParametros("@DataCadastro", usuario.DataCadastro);
acessoBancoDeDadosSqlServer.AdicionarParametros("@LoginUsu", usuario.LoginUsu);
acessoBancoDeDadosSqlServer.AdicionarParametros("@Senha", usuario.Senha);
acessoBancoDeDadosSqlServer.AdicionarParametros("@ConfirmarSenha", usuario.ConfirmarSenha);
acessoBancoDeDadosSqlServer.AdicionarParametros("@IdCargo", usuario.Cargo.IdCargo);
String idUsuario = acessoBancoDeDadosSqlServer.ExecutarManipulacao(CommandType.StoredProcedure, "uspUsuarioInserir").ToString();
return idUsuario;
}
catch ( Exception exception )
{
return exception.Message;
}
DTO = Object Transfer
namespace ObjetoTransferencia
{
public class Usuario
{
public Int32 IdUsuario { get; set; }
public String Nome { get; set; }
public String CPF { get; set; }
public DateTime DataCadastro { get; set; }
public String LoginUsu { get; set; }
public String Senha { get; set; }
public String ConfirmarSenha { get; set; }
public Cargo Cargo { get; set; }
public Boolean StatusUsu { get; set; }
}
}
GUI = Presentation
private void toolStripSalvar_Click_1(object sender, EventArgs e)
{
Usuario usuario = new Usuario();
usuario.CPF = Convert.ToString(maskedTextBoxCpf.Text);
usuario.Nome = Convert.ToString(txtNome.Text).ToUpper();
usuario.LoginUsu = Convert.ToString(txtLogin.Text);
Cargo cargo = new Cargo();
usuario.Cargo.IdCargo = Convert.ToInt32(usuario.Cargo.IdCargo);
usuario.Cargo.Descricao = Convert.ToString(usuario.Cargo.Descricao);
usuario.Senha = Convert.ToString(txtSenha.Text);
usuario.ConfirmarSenha = Convert.ToString(txtConfirmarSenha.Text);
if ( radioButtonAtivo.Checked == true)
{
usuario.StatusUsu = true;
}
else
{
usuario.StatusUsu = false;
}
UsuarioNegocios usuarioNegocios = new UsuarioNegocios();
String retorno = usuarioNegocios.Inserir(usuario);
try
{
Int32 idUsuario = Convert.ToInt32(retorno);
MessageBox.Show("Usuário inserido com sucesso.\n\nCódigo: " + idUsuario.ToString(), "SUCESSO !", MessageBoxButtons.OK, MessageBoxIcon.Information);
AtualizarGrid();
}
catch
{
MessageBox.Show("Não foi possível inserir registro.\n\nDetalhes: " + retorno, "ERRO !", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
debug your code and pay attention to the rank and file, because Idcargo comes from where? Another notorious thing is the number of
Convert.To
, since when the property.text
ofTextBox
andMaskTextBox
already return string. You are instantiating Cargo, if it does not feed Cargoid in the constructor,, soon it will be null. Put your Cargo class!!!– Paulo Ricardo
Change sea class User to have a constructor initialize Cargo.
– Marco Souza
Vagner, in that case, it would be interesting to accept the answer that provided the solution to help others in the community who have at least a problem. I believe it was @Therafa’s
– Diego Rafael Souza