I can not change the form code C #

0

Good evening guys could you help me? Hi, I am using C # with SQL Server database, I have a form with two fields codeCdd and descriptionCdd both of type text, I can include and exclude data from these fields without problems, but when I change both, only the field descriptionCdd changes and the codeCdd does not change. What can it be? I'm using tiered programming, I'll post the class, the data layer, the business layer, and the button's code layer. I think the problem is in the query update. Thank you very much in advance.

Class:

namespace Biblioteca.Modelos
{
     public class CddModelos
     {
         private string _codigoCdd;

         public string CodigoCdd
         {
             get { return _codigoCdd; }
             set { _codigoCdd = value; }
         }



         private string _descricaoCdd;

         public string DescricaoCdd
         {
             get { return _descricaoCdd; }
             set { _descricaoCdd = value; }
         }
    }
}

Data layer:

namespace Biblioteca.DAL
{
    public class CddDAL
    {
        public void Alterar_cdd(CddModelos cdd)
        {
            // conexao
            SqlConnection cn = new SqlConnection();

            try
            {
                cn.ConnectionString = Dados.StringDeConexao;

                SqlCommand cmd = new SqlCommand();

                cmd.Connection = cn;

                cmd.CommandType = CommandType.Text;

                cmd.CommandText = "UPDATE Cdd SET CD_CDD = @CD_CDD, DS_CDD = @DS_CDD WHERE CD_CDD = @CD_CDD;";

                cmd.Parameters.AddWithValue("@CD_CDD", cdd.CodigoCdd);
                cmd.Parameters.AddWithValue("@DS_CDD", cdd.DescricaoCdd);

                cn.Open();

                cmd.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            throw new Exception("Servidor SQL Erro:" + ex.Number);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            cn.Close();
        }
    }
    }

}   

Business tier:

namespace Biblioteca.BLL
{
    public class CddBLL
    {
        public void Alterar_cdd(CddModelos cdd)
        {
            CddDAL obj = new CddDAL();

            obj.Alterar_cdd(cdd);
        }
    }

}

Button code layer:

namespace UIWindows
{
    public partial class CddForm : Form
    {
        public CddForm()
        {
            InitializeComponent();
        }

        private void bt_alterar_cdd_Click(object sender, EventArgs e)
        {

             if (textBoxCodigo_cdd.Text.Length == 0)
             {
                 MessageBox.Show("O cdd deve ser selecionado para alteração.");
             }

             else

                try
                {
                    CddModelos cdd = new CddModelos();

                    cdd.CodigoCdd = textBoxCodigo_cdd.Text;
                    cdd.DescricaoCdd = textBoxDescricao_cdd.Text;

                    CddBLL obj = new CddBLL();

                    obj.Alterar_cdd(cdd);

                    MessageBox.Show("O cdd foi alterado com sucesso!");

                    AtualizaGridCdd();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Erro: " + ex.Message);
                }

    }

    }

}
    
asked by anonymous 08.12.2016 / 20:42

1 answer

1

When the method is called and you try to change it:

UPDATE Cdd SET CD_CDD = @CD_CDD, DS_CDD = @DS_CDD WHERE CD_CDD = @CD_CDD;

You are looking for the new CD_CDD code, the same one you want to change: WHERE CD_CDD = @CD_CDD , it may not be in the database.

Ideally, your method should get the old CD_CDD, so you look for the previous code identifier and upgrade to the new one.

Hugs!

    
08.12.2016 / 22:47