Error searching by name Access ASP.NET C#

Asked

Viewed 103 times

0

I have an error searching by name(value entered by user) in the access database he says that the values in the inside of the parameters are null. Follow the code. Person.Cs responsible for the logic of the bank.

public Contato obterCadastroPorNome(string Nome)
{
    ConexaoDAL conexaoAccess = null;
    try
    {
        conexaoAccess = new ConexaoDAL();
        conexaoAccess.AbrirConexao();
        OleDbCommand ComandoAccess = new OleDbCommand("select*from Contato where NOME=@NOME", conexaoAccess.ConexaoAccess);
        ComandoAccess.Parameters.AddWithValue("@NOME", Nome);
        conexaoAccess.ComandoDataReaderAccess.Read();
        return null;

    }
    catch (Exception ex)
    {
        throw new Exception("Erro ao pesquisar clientes" + ex.Message);
    }
    finally
    {
        conexaoAccess.FecharConexao();
    }
} // metodo para pesquisar cadastro por id 

public List<Contato> ListaDeContato()
{
    ConexaoDAL conexaoAccess = null;
    try
    {
        conexaoAccess.AbrirConexao();
        OleDbCommand ComandoAccess = new OleDbCommand("select * from Contato ", conexaoAccess.ConexaoAccess);
        conexaoAccess.ComandoDataReaderAccess = ComandoAccess.ExecuteReader();
        List<Contato> lista = new List<Contato>();
        while (conexaoAccess.ComandoDataReaderAccess.Read())
        {
            lista.Add(new Contato(conexaoAccess.ComandoDataReaderAccess));
        }
        return lista;
    }
    catch (Exception ex)
    {
        throw new Exception("Erro ao pesquisar Lista de clientes " + ex.Message);
    }
    finally
    {
        conexaoAccess.FecharConexao();
    }

Contact.Cs

namespace DAL.Model
{
    public class Contato
    {
        public int Codigo { get; private set; }
        public string Nome, Email, Fone;

        public Contato(string nome, string email, string fone)
        {
            Nome = nome;
            Email = email;
            Fone = fone;
        }
        public Contato(int codigo, string nome, string email, string fone)
        {
            Codigo = codigo;
            Nome = nome;
            Email = email;
            Fone = fone;
        }

        public Contato(OleDbDataReader comandoDataReader)
        {
            Codigo = Convert.ToInt32(comandoDataReader["CODIGO"]);
            Nome = Convert.ToString(comandoDataReader["NOME"]);
            Email = Convert.ToString(comandoDataReader["EMAIL"]);
            Fone = Convert.ToString(comandoDataReader["FONE"]);
        }

    }
}


public void btnPesquisar_Click(object sender, EventArgs e)
{
    garregarInfo();
}

public void garregarInfo()
{
    try
    {
        string NOME = Convert.ToString(txtPesquisar.Text);
        Pessoa pessoa = new Pessoa();
        Contato contato = pessoa.obterCadastroPorNome(NOME);
        if (contato != null)
        {
            pnlPesquisa.Visible = true;
            txtNovoNome.Text = contato.Nome;
        }
        else
        {
            Message.Text = "Registro não encontrado";
        }

    }
    catch (Exception ex)
    {
        throw new Exception(Message.Text = ex.Message);
    }

}

1 answer

3


As the error message itself already says, it is not possible to stop a parameter null pro method AddWithValue. You will need to perform a validation and if the string is null, send the value as "" (empty string).

ComandoAccess.Parameters.AddWithValue("@NOME", Nome ?? "");
  • it is better to check if the variable Name is null and to give a Return (or an error for the user to fill the field) so going to the database to search for "" makes no sense.

  • 1

    I think it’s more of a mistake in query of it. In the comments he says he wants to fetch all when it is null or empty. So he should exchange the = for like.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.