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