Object reference not set to an instance

0

"Object reference not set to an instance" of an object I'm having this problem here in my project in ASP.NET and since for quite some time I can not get on with it, please can I take a look at my code and identify the error I need to help with it in the insert part part of the database I've already checked and it's not the problem I imagine it has to do with instantiating a new object but 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 remove all try :

Procedure or function 'uspInserirCliente' expects parameter '@Fixo', which was not supplied.
    
asked by anonymous 28.07.2014 / 19:40

1 answer

1

It may be that in the Source Form the field has a name other than Fixed. What would make the collection ["Fixed"] have null value.

    
31.07.2014 / 02:33