Problem Trying to query Application c# + Database

Asked

Viewed 63 times

1

I’m making a list of customers, but is returning this error:

System.Argumentexception: 'Column 'Cpf_cpnj' does not Belong to table .'

This column, Cpf_Cnpj, is in my database:

inserir a descrição da imagem aqui

I can’t solve this problem...

Class code model:

using DnxVendas.Uteis;
using System.Collections.Generic;
using System.Data;

namespace DnxVendas.Models
{
    public class Cliente
    {
        public string ClienteId { get; set; }

        public string Nome { get; set; }

        public string Cpf_Cnpj { get; set; }

        public string Email { get; set; }

        public string Senha { get; set; }

        public List<Cliente> ListarTodosClientes()
        {
            List<Cliente> lista = new List<Cliente>();
            Cliente item;
            DAL objDAL = new DAL();
            string sql = "Select ClienteId, Nome, Cpf_Cnpj, Email, Senha From Cadastro.Clientes order by Nome asc";
            DataTable dt = objDAL.RetDaaTable(sql);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                item = new Cliente
                {
                    ClienteId = dt.Rows[i]["ClienteId"].ToString(),
                    Nome = dt.Rows[i]["Nome"].ToString(),
                    Cpf_Cnpj = dt.Rows[i]["Cpf_Cpnj"].ToString(), // Problema ocorre nesta linha
                    Email = dt.Rows[i]["Email"].ToString(),
                    Senha = dt.Rows[i]["Senha"].ToString()

                };

                lista.Add(item);
            }

            return lista;
        }
    }
}

I’ve circled my own string that makes the query in the database and returns the correct information:

string sql = "Select ClienteId, Nome, Cpf_Cnpj, Email, Senha From Cadastro.Clientes order by Nome asc";
  • Making explicit emails, Cpfs and people names.

2 answers

4


Cpf_Cpnj should be Cpf_Cnpj in the following line:

                Cpf_Cnpj = dt.Rows[i]["Cpf_Cpnj"].ToString(), // Problema ocorre nesta linha
  • Our missing attention rs, that’s right thanks.

2

The exception exists when some argument is not valid

On the line with the comment:

"- Problem occurs on this line"

You try to extract data from a column called Cpf_Cpnj, instead of Cpf_Cnpj.

Browser other questions tagged

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