o botao atualizar parou de funcionar depois que coloquei mais alguns campos no banco. Ele não retorna erro, mas também não atualiza. Tentei depurar a ExecuteNonQuery,e ela ta retornando 0. Ou seja, nenhuma linha é afetada, e dá: “erro ao atualizar”. Meu professor não conseguiu me ajudar tbm.
public bool Update(UserBLL u)
{
bool isSucess = false;
SqlConnection conn = new SqlConnection(myconnstring);
try
{
string sql = “UPDATE tbl_user SET first_name=@first_name, email=@email, username=@username, password=@password, contact=@contact, adress=@adress, gender=@gender, user_type=@user_type, added_date=@added_date, added_by=@added_by, rg=@rg, cpf=@cpf, city=@city, bairro=@bairro, num=@num, nascimento=@nascimento WHERE id=@id”;
//string where = " WHERE id=@id";
//if (novaFoto)
//{
// MemoryStream memory = new MemoryStream();
//bmp.Save(memory, ImageFormat.Bmp);
// foto = memory.ToArray();
// sql += ", image = @image";
//}
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@first_name", u.first_name);
cmd.Parameters.AddWithValue("@email", u.email);
cmd.Parameters.AddWithValue("@username", u.username);
cmd.Parameters.AddWithValue("@password", u.password);
cmd.Parameters.AddWithValue("@contact", u.contact);
cmd.Parameters.AddWithValue("@adress", u.adress);
cmd.Parameters.AddWithValue("@gender", u.gender);
cmd.Parameters.AddWithValue("@user_type", u.user_type);
cmd.Parameters.AddWithValue("@added_date", u.added_date);
cmd.Parameters.AddWithValue("@added_by", u.added_by);
cmd.Parameters.AddWithValue("@rg", u.rg);
cmd.Parameters.AddWithValue("@cpf", u.cpf);
cmd.Parameters.AddWithValue("@city", u.city);
cmd.Parameters.AddWithValue("@bairro", u.bairro);
cmd.Parameters.AddWithValue("@num", u.num);
cmd.Parameters.AddWithValue("@nascimento", u.nascimento);
cmd.Parameters.AddWithValue("@id", u.id);
//if(novaFoto){
// cmd.Parameters.Add("@image", SqlDbType.Binary).Value = u.Image;
//}
conn.Open();
int rows = cmd.ExecuteNonQuery();
MessageBox.Show(rows + "");
if (rows > 0)
{
isSucess = true;
}
else
{
isSucess = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return isSucess;
}
//----------------------------------------------------------------
private void btnAtualizar_Click(object sender, EventArgs e)
{
// try {
//tratamento dos campos Obrigatorios
if (txtTipoUsuario.SelectedItem == null || !txtCpf.MaskCompleted || !txtRg.MaskCompleted || txtNome.Text.Equals("") || txtLogin.Text.Equals("") || txtSenha.Text.Equals(""))
{
MessageBox.Show(“ATENTE-SE AO CAMPOS COM ‘*’, NÃO DEVEM FICAR VAZIOS”);
return;
}
//tratamento data
DateTime date = DateTime.Now.Date;
DateTime pDate = dateTimePicker1.Value.Date;
if (date <= pDate)
{
MessageBox.Show("DATA MAIOR OU IGUAL A DATA ATUAL");
return;
}
u.first_name = txtNome.Text.ToUpper();
u.email = txtEmail.Text.ToUpper();
u.username = txtLogin.Text;
u.password = txtSenha.Text;
u.contact = txtContato.Text;
u.adress = txtEndereco.Text.ToUpper();
u.gender = txtSexo.Text;
u.user_type = txtTipoUsuario.Text;
u.added_date = DateTime.Now;
u.added_by = 1;
u.cpf = txtCpf.Text;
u.rg = txtRg.Text;
u.city = txtCidade.Text.ToUpper();
u.bairro = txtBairro.Text.ToUpper();
u.num = txtNum.Text;
u.nascimento = dateTimePicker1.Value.Date;
if (!String.IsNullOrEmpty(pathImage)){
FileStream stream = new FileStream(pathImage, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(stream);
image = reader.ReadBytes((int)stream.Length);
reader.Close();
stream.Close();
u.Image = image;
}
bool sucess = dal.Update(u);
if (sucess == true)
{
MessageBox.Show("USUÁRIO ATUALIZADO COM SUCESSO");
Limpar();
}
else
{
MessageBox.Show("ERRO AO ATUALIZAR");
}
DataTable dt = dal.Select();
dataGridView1.DataSource = dt;
//}catch(Exception ex) { MessageBox.Show(ex + ""); }
}
//----------------------------------------------------------------------------
namespace Sistema_Vendas.BLLClasses
{
class UserBLL
{
public UserBLL(string first_name, string email, string username, string password, string contact, string adress, string gender, string user_type, DateTime added_date, int added_by, string rg, string cpf, string city, string bairro, string num, DateTime nascimento, byte[] image)
{
this.id = id;
this.first_name = first_name;
this.email = email;
this.username = username;
this.password = password;
this.contact = contact;
this.adress = adress;
this.gender = gender;
this.user_type = user_type;
this.added_date = added_date;
this.added_by = added_by;
this.rg = rg;
this.cpf = cpf;
this.city = city;
this.bairro = bairro;
this.num = num;
this.nascimento = nascimento;
Image = image;
}
public UserBLL(int id, string first_name, string email, string username, string password, string contact, string adress, string gender, string user_type, DateTime added_date, int added_by, string rg, string cpf, string city, string bairro, string num, DateTime nascimento, byte[] image)
: this (first_name, email, username, password, contact, adress, gender, user_type, added_date, added_by, rg, cpf, city, bairro, num, nascimento, image)
{
this.id = id;
}
public UserBLL()
{
this.id = id;
this.first_name = first_name;
this.email = email;
this.username = username;
this.password = password;
this.contact = contact;
this.adress = adress;
this.gender = gender;
this.user_type = user_type;
this.added_date = added_date;
this.added_by = added_by;
this.rg = rg;
this.cpf = cpf;
this.city = city;
this.bairro = bairro;
this.num = num;
this.nascimento = nascimento;
this.Image = Image;
}
public int id { get; set; }
public string first_name { get; set; }
public string email { get; set; }
public string username { get; set; }
public string password { get; set; }
public string contact { get; set; }
public string adress { get; set; }
public string gender { get; set; }
public string user_type { get; set; }
public DateTime added_date { get; set; }
public int added_by { get; set; }
public string rg { get; set; }
public string cpf { get; set; }
public string city { get; set; }
public string bairro { get; set; }
public string num { get; set; }
public DateTime nascimento { get; set; }
public byte[] Image { get; set; }
}
}