0
"Undefined object reference for an object" instance I have this problem here in my project in Asp.net and I have not been able to move forward with it for a long time, please would you take a look at my code and identify the precise error of help with it in the part of the database Insert I have already checked and it is not the problem I imagine that has to do with instantiating a new object only that I can not identify it.
//Acesso ao banco de dados
public class BDAcesso
{
private static SqlConnection _connection()
{
return new SqlConnection(Settings.Default.stringConexao);
}
private readonly SqlParameterCollection _sqlParameterCollection = new SqlCommand().Parameters;
public void LimparParamentros()
{
_sqlParameterCollection.Clear();
}
public void AdicionarParamentros(string nomeParametro, object valorParametro)
{
_sqlParameterCollection.Add(new SqlParameter(nomeParametro, valorParametro));
}
public object ExecutarManipulacao(CommandType commandType, string stroredProcedure)
{
try
{
var sqlConnection = _connection();
sqlConnection.Open();
var sqlCommand = sqlConnection.CreateCommand();
sqlCommand.CommandType = commandType;
sqlCommand.CommandText = stroredProcedure;
sqlCommand.CommandTimeout = 60;
foreach (SqlParameter sqlParameter in _sqlParameterCollection)
{
sqlCommand.Parameters.Add(new SqlParameter(sqlParameter.ParameterName, sqlParameter.Value));
}
return sqlCommand.ExecuteScalar();
}
catch (Exception exception)
{
throw new Exception(exception.Message);
}
}
public DataTable ExecutarConsulta(CommandType commandType, string stroredProcedure)
{
try
{
var sqlConnection = _connection();
sqlConnection.Open();
var sqlCommand = sqlConnection.CreateCommand();
sqlCommand.CommandType = commandType;
sqlCommand.CommandText = stroredProcedure;
sqlCommand.CommandTimeout = 60;
foreach (SqlParameter sqlParameter in _sqlParameterCollection)
{
sqlCommand.Parameters.Add(new SqlParameter(sqlParameter.ParameterName, sqlParameter.Value));
}
var sqlDataAdapter = new SqlDataAdapter(sqlCommand);
var dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
return dataTable;
}
catch (Exception exception)
{
throw new Exception(exception.Message);
}
}
}
//DAO------------------------------------------------
public string InserirCliente(Clientes clientes)
{
try
{
_bdAcesso.LimparParamentros();
_bdAcesso.AdicionarParamentros("@Nome", clientes.Nome);
_bdAcesso.AdicionarParamentros("@NomeFantasia", clientes.NomeFantasia);
_bdAcesso.AdicionarParamentros("@CPF_CNPJ", clientes.CPF_CNPJ);
_bdAcesso.AdicionarParamentros("@RG_IE", clientes.RG_IE);
_bdAcesso.AdicionarParamentros("@Obs", clientes.Obs);
_bdAcesso.AdicionarParamentros("@Responsavel", clientes.Responsavel);
_bdAcesso.AdicionarParamentros("@Logradouro", clientes.Endereco.Logradouro);
_bdAcesso.AdicionarParamentros("@Bairro", clientes.Endereco.Bairro);
_bdAcesso.AdicionarParamentros("@Cidade", clientes.Endereco.Cidade);
_bdAcesso.AdicionarParamentros("@Estado", clientes.Endereco.Estado);
_bdAcesso.AdicionarParamentros("@Uf", clientes.Endereco.Uf);
_bdAcesso.AdicionarParamentros("@Cep", clientes.Endereco.Cep);
_bdAcesso.AdicionarParamentros("@Complemento", clientes.Endereco.Complemento);
_bdAcesso.AdicionarParamentros("@Fixo", clientes.Contato.Fixo);
_bdAcesso.AdicionarParamentros("@Celular", clientes.Contato.Celular);
_bdAcesso.AdicionarParamentros("@Comercial", clientes.Contato.Comercial);
_bdAcesso.AdicionarParamentros("@Email", clientes.Contato.Email);
_bdAcesso.AdicionarParamentros("@HomePage", clientes.Contato.HomePage);
var idCliente = _bdAcesso.ExecutarManipulacao(CommandType.StoredProcedure, "uspInserirCliente").ToString();
return idCliente;
}
catch (Exception exception)
{
throw new Exception(exception.Message);
}
}
//Controle
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
var clienteDao = new ClienteDao();
var cliente = new Clientes
{
Nome = collection["Nome"],
NomeFantasia = collection["NomeFantasia"],
CPF_CNPJ = collection["CPF_CNPJ"],
RG_IE = collection["RG_IE"],
Obs = collection["Obs"],
AlteradoPor = collection["AlteradoPor"],
Responsavel = collection["Responsavel"],
Contato =
new Contato
{
Fixo = collection["Fixo"],
Celular = collection["Celular"],
Comercial = collection["Comercial"],
Email = collection["Email"],
HomePage = collection["HomePage"]
},
Endereco =
new Endereco
{
Logradouro = collection["Logradouro"],
Bairro = collection["Bairro"],
Cidade = collection["Cidade"],
Estado = collection["Estado"],
Uf = collection["Uf"],
Cep = collection["Cep"],
Complemento = collection["Complemento"]
}
};
clienteDao.InserirCliente(cliente);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
This is the error after you delete all the try
:
Procedure or function 'uspInserirCliente' expects parameter '@Fixo', which was not supplied.
On which line the error occurs?
– Leonel Sanches da Silva
First of all remove all error handling from your Bdaccess class, it is doing nothing, it is totally unnecessary and the way it is in fact it prevents you from finding out where the error is. After removing this error handling the released Exception must have the correct Stacktrace including the line that generates the error when compiled as Debug
– Leandro Godoy Rosa
@Leandro Ferreira error "Procedure or Function 'uspInserirCliente' expects Parameter '@Fixo', which was not supplied." occurs because your previous project expects a value to be passed to this field. The ideal is to create a basic validation for the required ones before sending the data to be recorded/updated. At first, fill in all fields to perform your tests.
– Renan
_bdAcesso.AdicionarParamentros("@Nome", clientes.Nome);
 _bdAcesso.AdicionarParamentros("@NomeFantasia", clientes.NomeFantasia);
 _bdAcesso.AdicionarParamentros("@CPF_CNPJ", clientes.CPF_CNPJ);
 _bdAcesso.AdicionarParamentros("@RG_IE", customers.RG_IE); _bdAccess.Add wires("@Obs", customers.Note); _bdAccess.Add wires("@Responsavel", customers.Responsavel); are filled by only the contact part and address that I’m having problems think is in the part of instantiating the contacts objects and addresses
– Leandro Ferreira
Where that
_bdAcesso
is being initialized?– Oralista de Sistemas
readonly Bdaccess _bdAccess = new Bdaccess(); in Class Clientedao
– Leandro Ferreira
The error is quite clear: the precedent expects a parameter
@Fixo
that has not been provided.– Leonel Sanches da Silva
Do you have the easy trial? for a better analysis of the problem
– Edvaldo Farias