I have a conversion error running a C#code

Asked

Viewed 48 times

0

Running the code says the following:

inserir a descrição da imagem aqui

If I ok the application runs normally but the data entered in the database does not appear in Datagridview. What would be my error?

   using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using AcessoDados.Contracts;
    using AcessoDados.Entities;
    using System.Data;
    using System.Data.SqlClient;


    namespace AcessoDados.Repositories
    {
    public class FuncionarioRepository : MasterRepository, IFuncionarioRepository
    {
        private string selectAll;
        private string insert;
        private string update;
        private string delete;

        public FuncionarioRepository()
        {
            selectAll = "Select Nome, Telefone, Cpf, Endereco, Numero, Bairro, Cep, Cidade, Estado, Salario from Funcionario";
            insert = "insert into Funcionario values(@Nome,@Telefone,@Cpf,@Endereco,@Numero,@Bairro,@Cep,@Cidade,@Estado,@Salario)";
            update = "update Funcionario set Nome=@Nome,Telefone=@Telefone,Cpf=@Cpf,Endereco=@Endereco,Numero=@Numero,Bairro=@Bairro,Cep=@Cep,Cidade=@Cidade,Estado=@Estado,Salario@Salario where Id=@Id";
            delete = "delete from Funcionario where Id=@Id";
        }
        public int Add(Funcionario entity)
        {
            parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@Nome", entity.Nome));
            parameters.Add(new SqlParameter("@Telefone", entity.Telefone));
            parameters.Add(new SqlParameter("@Cpf", entity.Cpf));
            parameters.Add(new SqlParameter("@Endereco", entity.Endereco));
            parameters.Add(new SqlParameter("@Numero", entity.Numero));
            parameters.Add(new SqlParameter("@Bairro", entity.Bairro));
            parameters.Add(new SqlParameter("@Cep", entity.Cep));
            parameters.Add(new SqlParameter("@Cidade", entity.Cidade));
            parameters.Add(new SqlParameter("@Estado", entity.Estado));
            parameters.Add(new SqlParameter("@Salario", entity.Salario));
            return ExecuteNonQuery(insert);


        }

        public int Editar(Funcionario entity)
        {
            parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@Id", entity.Id));
            parameters.Add(new SqlParameter("@Nome", entity.Nome));
            parameters.Add(new SqlParameter("@Telefone", entity.Telefone));
            parameters.Add(new SqlParameter("@Cpf", entity.Cpf));
            parameters.Add(new SqlParameter("@Endereco", entity.Endereco));
            parameters.Add(new SqlParameter("@Numero", entity.Numero));
            parameters.Add(new SqlParameter("@Bairro", entity.Bairro));
            parameters.Add(new SqlParameter("@Cep", entity.Cep));
            parameters.Add(new SqlParameter("@Cidade", entity.Cidade));
            parameters.Add(new SqlParameter("@Estado", entity.Estado));
            parameters.Add(new SqlParameter("@Salario", entity.Salario));
            return ExecuteNonQuery(update);
        }

        public IEnumerable<Funcionario> GetAll()
        {
            var tableResult = ExecuteReader(selectAll);
            var listFuncionario = new List<Funcionario>();
            foreach (DataRow item in tableResult.Rows)
            {
                listFuncionario.Add(new Funcionario { 
                    Id = Convert.ToInt32(item[0]),
                    Nome = item[1].ToString(),
                    Telefone = item[2].ToString(),
                    Cpf = item[3].ToString(),
                    Endereco = item[4].ToString(),
                    Numero = Convert.ToInt32(item[5]),
                    Bairro = item[6].ToString(),
                    Cep = item[7].ToString(),
                    Cidade = item[8].ToString(),
                    Estado = item[9].ToString(),
                    Salario = (float) Convert.ToDouble(item[0])
                });
            }
            return listFuncionario;
        }

        public int Remover(int Id)
        {
            parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@Id", Id));
            return ExecuteNonQuery(delete);
        }
    }
}

Stack kind of bugged the indentation!

  • Read https://pt.meta.stackoverflow.com/questions/5483/manual-de-como-n%C3%83o-fazer-perguntas/5485#5485 ,repeat your question with the code instead of image.

  • I put the image because the code is pretty big but I’ll just put this part then.

  • You must be doing wrong, only what you posted has no way to help

  • I put all the code.

1 answer

0

By the image shown, the error is in converting a string to an integer. The only places where such a conversion is made are Convert.ToInt32(item[0]) and Convert.ToInt32(item[5]).

When looking at your select it is possible to verify that the first returned item is the name and not the id, as you expect in your foreach, and therefore the 1st ToInt32 returns error.

Add the id to your query and check that the other properties are in the correct order. One thing that could help, in such cases, is to change all properties to string, so you could already verify that some field of the database would not be filled in the correct property.

  • The image shows that he did Numero = Convert.ToInt32(item[0]) where it should be Numero = Convert.ToInt32(item[5]) also.

  • Yes, as there was the error right in the first property, all the others are also wrong, only they will not present error because they are strings.

Browser other questions tagged

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