Not all paths return value

0

TheCadastrareventisshowingupwiththeerror.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingCamadaModelo;usingSystem.Configuration;usingSystem.Data;usingSystem.Data.OleDb;usingCamadaControle;namespaceCamadaControle{publicclassctlTarefas{publicboolCadastrar(mdlTarefas_mdlTarefas){try{stringconexaoMSAccess=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();
            }
        }
    }
}
    
asked by anonymous 17.11.2018 / 23:53

1 answer

0

First, remove this try-catch from the code, it is not doing anything useful, so it is better to remove it, just put catch if you can recover from the error or do something useful.

Then use using to manage a resource, so you can close the resource correctly, This is wrong. See more . And another question is about the Method to execute while destroying instance of a class .

And simplify the code, I will not talk about everything, but all if can be replaced by

return cmd.ExecuteNonQuery() > 0;

Nothing else.

    
18.11.2018 / 00:17