コード例 #1
0
        private void SaveReg()
        {
            if (MessageBox.Show("Gravar os dados?", "Confirmação", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                GTI_Models.Models.Cidadao reg = new GTI_Models.Models.Cidadao {
                    Nomecidadao = NomeText.Text,
                    Rg          = String.IsNullOrWhiteSpace(RGText.Text) ? null : RGText.Text,
                    Orgao       = String.IsNullOrWhiteSpace(OrgaoText.Text) ? null : OrgaoText.Text
                };
                if (PessoaList.SelectedIndex == 0)
                {
                    if (CPFMask.Text != "")
                    {
                        reg.Cpf = CPFMask.Text;
                    }
                }
                else
                {
                    if (CNPJMask.Text != "")
                    {
                        reg.Cnpj = CNPJMask.Text;
                    }
                }

                reg.Juridica = JuridicaCheck.Checked ? true : false;
                if (DataNasctoMask.MaskCompleted && gtiCore.IsDate(DataNasctoMask.Text))
                {
                    reg.Data_nascimento = Convert.ToDateTime(DataNasctoMask.Text);
                }
                else
                {
                    reg.Data_nascimento = null;
                }
                if (ProfissaoList.SelectedIndex > -1)
                {
                    reg.Codprofissao = Convert.ToInt32(ProfissaoList.SelectedValue);
                }

                if (!string.IsNullOrWhiteSpace(LogradouroRText.Text))
                {
                    reg.Codpais        = PaisRText.Tag == null ? 0 : Convert.ToInt32(PaisRText.Tag.ToString());
                    reg.Siglauf        = UFRText.Text;
                    reg.Codcidade      = string.IsNullOrWhiteSpace(CidadeRText.Tag.ToString()) ? (short)0 : Convert.ToInt16(CidadeRText.Tag.ToString());
                    reg.Codbairro      = string.IsNullOrWhiteSpace(BairroRText.Tag.ToString())? (short)0 : Convert.ToInt16(BairroRText.Tag.ToString());
                    reg.Codlogradouro  = string.IsNullOrWhiteSpace(LogradouroRText.Tag.ToString()) ? 0 : Convert.ToInt32(LogradouroRText.Tag.ToString());
                    reg.Nomelogradouro = reg.Codcidade != 413 ? LogradouroRText.Text : "";
                    reg.Numimovel      = NumeroRText.Text == "" ? (short)0 : Convert.ToInt16(NumeroRText.Text);
                    reg.Complemento    = ComplementoRText.Text;
                    reg.Cep            = reg.Codcidade != 413 ? CepRText.Text == "" ? 0 : Convert.ToInt32(CepRText.Text) : 0;
                    reg.Email          = EmailRText.Text;
                    reg.Etiqueta       = EtiquetaRCheck.Checked ? "S" : "N";
                    reg.Telefone       = String.IsNullOrWhiteSpace(FoneRText.Text) ? null : FoneRText.Text;
                    reg.Temfone        = TemFoneRCheck.Checked;
                    reg.Whatsapp       = WhatsAppRCheck.Checked;
                }

                if (!string.IsNullOrWhiteSpace(LogradouroCText.Text))
                {
                    reg.Codpais2        = PaisCText.Tag == null ? 0 : Convert.ToInt32(PaisCText.Tag.ToString());
                    reg.Siglauf2        = UFCText.Text;
                    reg.Codcidade2      = string.IsNullOrWhiteSpace(CidadeCText.Tag.ToString()) ? (short)0 : Convert.ToInt16(CidadeCText.Tag.ToString());
                    reg.Codbairro2      = string.IsNullOrWhiteSpace(BairroCText.Tag.ToString()) ? (short)0 : Convert.ToInt16(BairroCText.Tag.ToString());
                    reg.Codlogradouro2  = string.IsNullOrWhiteSpace(LogradouroCText.Tag.ToString()) ? 0 : Convert.ToInt32(LogradouroCText.Tag.ToString());
                    reg.Nomelogradouro2 = reg.Codcidade2 != 413 ? LogradouroCText.Text : "";
                    reg.Numimovel2      = NumeroCText.Text == "" ? (short)0 : Convert.ToInt16(NumeroCText.Text);
                    reg.Complemento2    = ComplementoCText.Text;
                    reg.Cep2            = reg.Codcidade2 != 413 ? CepCText.Text == "" ? 0 : Convert.ToInt32(CepCText.Text) : 0;
                    reg.Email2          = EmailCText.Text;
                    reg.Etiqueta2       = EtiquetaCButton.Checked ? "S" : "N";
                    reg.Telefone2       = String.IsNullOrWhiteSpace(FoneCText.Text) ? null : FoneCText.Text;
                    reg.Temfone2        = TemFoneCCheck.Checked;
                    reg.Whatsapp2       = WhatsAppCCheck.Checked;
                }

                Cidadao_bll clsCidadao = new Cidadao_bll(_connection);
                Exception   ex;

                if (bAddNew)
                {
                    ex = clsCidadao.Incluir_cidadao(reg);
                    if (ex != null)
                    {
                        ErrorBox eBox = new ErrorBox("Atenção", ex.Message, ex);
                        eBox.ShowDialog();
                    }
                    else
                    {
                        int nLastCod = clsCidadao.Retorna_Ultimo_Codigo_Cidadao();
                        LoadReg(nLastCod);
                        ControlBehaviour(true);
                    }
                }
                else
                {
                    reg.Codcidadao = Convert.ToInt32(CodigoText.Text);
                    ex             = clsCidadao.Alterar_cidadao(reg);
                    if (ex != null)
                    {
                        ErrorBox eBox = new ErrorBox("Atenção", ex.Message, ex);
                        eBox.ShowDialog();
                    }
                    else
                    {
                        ControlBehaviour(true);
                    }
                }

                int nCodigo = 0;
                if (bAddNew)
                {
                    nCodigo = clsCidadao.Retorna_Ultimo_Codigo_Cidadao();
                }
                else
                {
                    nCodigo = Convert.ToInt32(CodigoText.Text);
                }
            }
        }