I can’t change the form code C#

Asked

Viewed 74 times

0

Could you guys help me out? Well, I’m using C# with SQL Server database, I have a form with two codeCdd fields and descricaCdd both text type, I can include and delete data from these fields without problems, but when I change the two, only the Cdd description field changes and the code does not change. What can it be? I’m using layered programming, I’m going to post the class, data layer, business layer and button code layer. I think the problem is in the query update. Thank you from now on.

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 layer:

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

            obj.Alterar_cdd(cdd);
        }
    }

}

Layer of button code:

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);
                }

    }

    }

}

1 answer

1

When the method is called and you try to change:

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

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

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

Hugs!

  • The guy thanks so much for helping, my boss said that this field CD_CDD does not need to be changed. Hug

Browser other questions tagged

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