Not all paths return value

Asked

Viewed 219 times

0

Erro sendo mostrado

The event Cadastrar is appearing with the error.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CamadaModelo;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using CamadaControle;

namespace CamadaControle
    {
    public class ctlTarefas
    {
        public bool Cadastrar(mdlTarefas _mdlTarefas)
        {
            try
            {
                string conexaoMSAccess = ConfigurationManager.ConnectionStrings["conexaoMSAccess"].ToString();
                OleDbConnection conexaodb = new OleDbConnection(conexaoMSAccess);
                conexaodb.Open();

                string query = "INSERT INTO tbl_tarefas (nome, descrição, data) VALUES (@Nome, @Descrição, @Data)";
                OleDbCommand cmd = new OleDbCommand(query, conexaodb);

                var pmtNome = cmd.CreateParameter();
                pmtNome.ParameterName = "@Nome";
                pmtNome.DbType = DbType.String;
                pmtNome.Value = _mdlTarefas.nome;
                cmd.Parameters.Add(pmtNome);

                var pmtDescricao = cmd.CreateParameter();
                pmtDescricao.ParameterName = "@Descrição";
                pmtDescricao.DbType = DbType.String;
                pmtDescricao.Value = _mdlTarefas.nome;
                cmd.Parameters.Add(pmtDescricao);

                var pmtData = cmd.CreateParameter();
                pmtData.ParameterName = "@Data";
                pmtData.DbType = DbType.String;
                pmtData.Value = _mdlTarefas.nome;
                cmd.Parameters.Add(pmtData);

                if(cmd.ExecuteNonQuery() > 0)
                {
                    conexaodb.Close();
                    return true;
                }
                else
                {
                    conexaodb.Close();
                    return false;
                }
            }

            catch(Exception ex)
            {
                Console.WriteLine("Erro ao inserir informações!" + ex.Message);
            }
            finally
            {
                Console.WriteLine("Precione inicio");
                Console.ReadKey();
            }
        }
    }
}
  • Please adjust your question. It has formatting flaws. Put the error in text and not image.

  • Adjusted, thanks for the tip

  • @Patryckdalpont The answer solved your question? Do you think you can accept it? See [tour] if you don’t know how to do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

First, remove that try-catch of the code, it’s not doing anything useful, so it’s best to remove it, just put a catch if you can recover from the error or do something useful.

Afterward use the using to manage a resource, thus makes the closure of the resource in the correct way, this is wrong. See more. And another question is about the Method to execute when destroying class instance.

And simplify the code, I won’t talk about everything, but the whole if may be replaced by

return cmd.ExecuteNonQuery() > 0;

I put in the Github for future reference.

Nothing else.

  • Hello Maniero, if I take the Try will not interfere in the access process the access database connection I have?

  • No, because he’s not doing anything useful. Never use the resources without knowing what it’s for. This you don’t know yet. Only use it when you learn. Do what I told you in reply.

Browser other questions tagged

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