コード例 #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (!editar)
            {
                groupBox1.Controls.Cast <Control>().ToList()
                .ForEach(x => { x.Enabled = true; });
                groupBox2.Controls.Cast <Control>().ToList()
                .ForEach(x => { x.Enabled = true; });
                textBoxIdIgreja.Enabled = false;

                button1.Text = "Salvar";
                editar       = !editar;
            }
            else
            {
                bool sucess = true;
                try
                {
                    string   igrejaNome   = textBoxNomeIgreja.Text;
                    string   igrejaEstilo = textBoxEstiloIgreja.Text;
                    DateTime igrejaFund   = monthCalendarFundacaoIgreja.SelectionStart;
                    maskedTextBoxContatoIgreja.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
                    string contato = maskedTextBoxContatoIgreja.Text;
                    maskedTextBoxContatoIgreja.TextMaskFormat = MaskFormat.IncludePromptAndLiterals;
                    string descricao = textBoxDescricaoIgreja.Text;

                    string endTip = comboBoxEndTipoIgreja.SelectedItem.ToString();
                    string endLog = textBoxEndLogradouroIgreja.Text;
                    string endNum = textBoxEndNumeroIgreja.Text;
                    string endComp;
                    try
                    {
                        endComp = textBoxEndComplementoIgreja.Text;
                    }
                    catch (NullReferenceException)
                    {
                        endComp = "";
                    }
                    string endBairro = textBoxEndBairroIgreja.Text;
                    maskedTextBoxEndCepIgreja.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
                    string endCep = maskedTextBoxEndCepIgreja.Text;
                    maskedTextBoxEndCepIgreja.TextMaskFormat = MaskFormat.IncludePromptAndLiterals;
                    int cidade = int.Parse(comboBoxEndCidadeIgreja.SelectedValue.ToString());
                    if (cidade == 0)
                    {
                        throw new InvalidSelectValue("CidadeID must be different of 0");
                    }
                    int            id = int.Parse(textBoxIdIgreja.Text);
                    Classes.Igreja i  = new Classes.Igreja(id, igrejaFund, igrejaEstilo, null, 0, "Igreja", igrejaNome, contato, descricao, endTip, endLog, endNum, endComp, endBairro, endCep, cidade);
                    if (!adapter.atualizarIgreja(i))
                    {
                        sucess = false;
                        //MessageBox.Show("Falha", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    MySqlDataReader reader = adapter.lastInsertId();
                    reader.Read();
                    int igrejaId = int.Parse(textBoxIdIgreja.Text);

                    foreach (DataRow drow in selecionados.Rows)
                    {
                        int idFundador      = int.Parse(drow[0].ToString());
                        Classes.Fundador fu = new Classes.Fundador(idFundador, null, null, DateTime.Now, false, DateTime.Now, null, null);
                        Classes.Igreja   ig = new Classes.Igreja(igrejaId, DateTime.Now, null, null, 0, null, null, null, null, null, null, null, null, null, null, 0);
                        //if (!adapter.atualizarFundadapor(ig, fu))
                        //{
                        //sucess = false;
                        //MessageBox.Show("Falha", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        //}
                    }

                    if (sucess)
                    {
                        MessageBox.Show("Atualizado!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Falha", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                catch (NullReferenceException)
                {
                    //Erro ao resgatar valores dos componentes
                    MessageBox.Show("Verifique se os campos estão preenchidos corretamente", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (InvalidSelectValue)
                {
                    //Tratar se usuario não tenha selecionado uma cidade valida
                    MessageBox.Show("Verifique se os campos estão preenchidos corretamente", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }