Estou tendo problema ao atualizar meu banco quando o pathImage está vázio. Alguém me ajuda?
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
int rowIndex = e.RowIndex;
txtID.Text = dataGridView1.Rows[rowIndex].Cells[0].Value.ToString();
txtNome.Text = dataGridView1.Rows[rowIndex].Cells[1].Value.ToString();
txtEmail.Text = dataGridView1.Rows[rowIndex].Cells[2].Value.ToString();
txtLogin.Text = dataGridView1.Rows[rowIndex].Cells[3].Value.ToString();
txtSenha.Text = dataGridView1.Rows[rowIndex].Cells[4].Value.ToString();
txtContato.Text = dataGridView1.Rows[rowIndex].Cells[5].Value.ToString();
txtEndereco.Text = dataGridView1.Rows[rowIndex].Cells[6].Value.ToString();
txtSexo.Text = dataGridView1.Rows[rowIndex].Cells[7].Value.ToString();
txtTipoUsuario.Text = dataGridView1.Rows[rowIndex].Cells[8].Value.ToString();
txtRg.Text = dataGridView1.Rows[rowIndex].Cells[11].Value.ToString();
txtCpf.Text = dataGridView1.Rows[rowIndex].Cells[12].Value.ToString();
txtCidade.Text = dataGridView1.Rows[rowIndex].Cells[13].Value.ToString();
txtBairro.Text = dataGridView1.Rows[rowIndex].Cells[14].Value.ToString();
txtNum.Text = dataGridView1.Rows[rowIndex].Cells[15].Value.ToString();
txtRg.Enabled = false;
txtCpf.Enabled = false;
try
{
dateTimePicker1.Value = Convert.ToDateTime(dataGridView1.Rows[e.RowIndex].Cells[16].Value.ToString());
}catch(Exception ex) { }
try
{
int id = Convert.ToInt32(txtID.Text);
//carregar a imagem a partir do banco.
UserDal studentDao = new UserDal();
UserBLL Student = studentDao.Read(id);
MemoryStream ms = new MemoryStream(Student.Image);
pcbImage.Image = Image.FromStream(ms);
image = Student.Image;
novaFoto = false;
}
catch (Exception ex) { }
}
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, novaFoto, image);
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 + ""); }
}
#region Atualizar os dados do banco de dados
public bool Update(UserBLL u, bool novaFoto, byte []foto)
{
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”;
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 + where, 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 = foto;
}
conn.Open();
int rows = cmd.ExecuteNonQuery();
novaFoto = false;
if (rows > 0)
{
isSucess = true;
}
else
{
isSucess = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return isSucess;
}
#endregion