Exemplo n.º 1
0
        private void btnACadastrar_Click(object sender, EventArgs e)
        {
            AGENDAMENTOEntities db = new AGENDAMENTOEntities();

            try
            {
                TELEFONE telefone = new TELEFONE();
                if (cbxTipoTel.SelectedIndex == 2)
                {
                    telefone.TIPO_TEL = txtTipoTel.Text;
                }
                else
                {
                    telefone.TIPO_TEL = cbxTipoTel.SelectedItem.ToString();
                }
                telefone.AGENDA_ID_AGENDA = VerAgendamento.idContato;
                telefone.TEL = txtTelefone.Text;
                db.TELEFONE.Add(telefone);
                db.SaveChanges();
                MessageBox.Show("telefone cadastrado com sucesso", "SUCESSO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                db.Dispose();
            }
        }
Exemplo n.º 2
0
        private void btnCarregar_Click(object sender, EventArgs e)
        {
            AGENDAMENTOEntities db = new AGENDAMENTOEntities();

            try
            {
                gdvEmail.DataSource = db.EMAIL.Where(s => s.AGENDA_ID_AGENDA == VerAgendamento.idContato).Select(s => new
                {
                    ID    = s.ID_EMAIL,
                    TIPO  = s.TIPO_EMAIL,
                    EMAIL = s.EMAIL1
                }).ToList();
                gdvTelefone.DataSource = db.TELEFONE.Where(s => s.AGENDA_ID_AGENDA == VerAgendamento.idContato).Select(s => new
                {
                    ID    = s.ID_TELEFONE,
                    TIPO  = s.TIPO_TEL,
                    EMAIL = s.TEL
                }).ToList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                db.Dispose();
            }
        }
Exemplo n.º 3
0
        private void btnCadastrar_Click(object sender, EventArgs e)
        {
            AGENDAMENTOEntities db = new AGENDAMENTOEntities();

            try
            {
                EMAIL email = new EMAIL();
                if (cbxTipoEmail.SelectedIndex == 2)
                {
                    email.TIPO_EMAIL = txtTipoEmail.Text;
                }
                else
                {
                    email.TIPO_EMAIL = cbxTipoEmail.SelectedItem.ToString();
                }
                email.EMAIL1           = txtEmail.Text;
                email.AGENDA_ID_AGENDA = VerAgendamento.idContato;
                db.EMAIL.Add(email);
                db.SaveChanges();
                MessageBox.Show("Email alterado com sucesso", "SUCESSO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                db.Dispose();
            }
        }
Exemplo n.º 4
0
        private void btnApagar_Click(object sender, EventArgs e)
        {
            AGENDAMENTOEntities db = new AGENDAMENTOEntities();

            try
            {
                DialogResult deletar = MessageBox.Show("Deseja apagar esse contato", "AVISO", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (deletar == DialogResult.Yes)
                {
                    var contato = db.AGENDA.Where(s => s.ID_AGENDA == VerAgendamento.idContato).FirstOrDefault();
                    contato.FLAG_EXCLUIDO = "S";
                    db.SaveChanges();
                    MessageBox.Show("Contato Excluido com sucesso", "SUCESSO", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    VerAgendamento va = new VerAgendamento();
                    va.carregarGrid();
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                db.Dispose();
            }
        }
Exemplo n.º 5
0
        private void btnAlterar_Click(object sender, EventArgs e)
        {
            AGENDAMENTOEntities db = new AGENDAMENTOEntities();

            try
            {
                var telefone = db.TELEFONE.Where(s => s.ID_TELEFONE == VerContatos.idTel).FirstOrDefault();
                if (cbxTipoTel.SelectedIndex == 2)
                {
                    telefone.TIPO_TEL = txtTipoTel.Text;
                }
                else
                {
                    telefone.TIPO_TEL = cbxTipoTel.SelectedItem.ToString();
                }
                telefone.TEL = txtTelefone.Text;
                db.SaveChanges();
                MessageBox.Show("telefone alterado com sucesso", "SUCESSO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                db.Dispose();
            }
        }
Exemplo n.º 6
0
        private void btnApagar_Click(object sender, EventArgs e)
        {
            AGENDAMENTOEntities db = new AGENDAMENTOEntities();

            try
            {
                var telefone = db.TELEFONE.Where(s => s.ID_TELEFONE == VerContatos.idTel).FirstOrDefault();
                db.TELEFONE.Remove(telefone);
                db.SaveChanges();
                MessageBox.Show("Email apagado com sucesso", "SUCESSO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                db.Dispose();
            }
        }
Exemplo n.º 7
0
        public void carregarGrid()
        {
            AGENDAMENTOEntities db = new AGENDAMENTOEntities();

            try
            {
                gdvContatos.DataSource = db.AGENDA.Where(s => s.FLAG_EXCLUIDO != "S").OrderBy(s => s.NOME).Select(s => new
                {
                    ID                 = s.ID_AGENDA,
                    Contato            = s.NOME,
                    Telefone_principal = s.TEL_PRINCIPAL,
                    Logradouro         = s.LOCALIDADE_RESIDENCIAL
                }).ToList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                db.Dispose();
            }
        }
Exemplo n.º 8
0
        private void btnUltimosContatos_Click(object sender, EventArgs e)
        {
            AGENDAMENTOEntities db = new AGENDAMENTOEntities();

            try
            {
                gdvContatos.DataSource = db.AGENDA.Where(s => s.FLAG_EXCLUIDO != "S").OrderByDescending(s => s.ID_AGENDA).Select(s => new
                {
                    ID                 = s.ID_AGENDA,
                    Contato            = s.NOME,
                    Telefone_principal = s.TEL_PRINCIPAL,
                    Logradouro         = s.LOCALIDADE_RESIDENCIAL
                }).ToList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                db.Dispose();
            }
        }
Exemplo n.º 9
0
        private void AlterarTelOpcional_Load(object sender, EventArgs e)
        {
            AGENDAMENTOEntities db = new AGENDAMENTOEntities();

            try
            {
                cbxTipoTel.Items.Add("Casa");
                cbxTipoTel.Items.Add("Trabalho");
                cbxTipoTel.Items.Add("Outros");

                var telefone = db.TELEFONE.Where(s => s.ID_TELEFONE == VerContatos.idTel).FirstOrDefault();
                if (telefone.TIPO_TEL != "Casa")
                {
                    cbxTipoTel.SelectedIndex = 0;
                }
                if (telefone.TIPO_TEL != "Trabalho")
                {
                    cbxTipoTel.SelectedIndex = 1;
                }
                if (telefone.TIPO_TEL != "Casa" && telefone.TIPO_TEL != "Trabalho")
                {
                    cbxTipoTel.SelectedIndex = 2;
                    txtTipoTel.Visible       = true;
                    txtTipoTel.Text          = telefone.TIPO_TEL;
                }
                txtTelefone.Text = telefone.TEL;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                db.Dispose();
            }
        }
Exemplo n.º 10
0
        private void AlterarEmailOpcional_Load(object sender, EventArgs e)
        {
            AGENDAMENTOEntities db = new AGENDAMENTOEntities();

            try
            {
                cbxTipoEmail.Items.Add("Casa");
                cbxTipoEmail.Items.Add("Trabalho");
                cbxTipoEmail.Items.Add("Outros");

                var email = db.EMAIL.Where(s => s.ID_EMAIL == VerContatos.idEmail).FirstOrDefault();
                if (email.TIPO_EMAIL != "Casa")
                {
                    cbxTipoEmail.SelectedIndex = 0;
                }
                if (email.TIPO_EMAIL != "Trabalho")
                {
                    cbxTipoEmail.SelectedIndex = 1;
                }
                if (email.TIPO_EMAIL != "Casa" && email.TIPO_EMAIL != "Trabalho")
                {
                    cbxTipoEmail.SelectedIndex = 2;
                    txtTipoEmail.Visible       = true;
                    txtTipoEmail.Text          = email.TIPO_EMAIL;
                }
                txtEmail.Text = email.EMAIL1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                db.Dispose();
            }
        }
Exemplo n.º 11
0
        private void btnRelatorio_Click(object sender, EventArgs e)
        {
            AGENDAMENTOEntities db = new AGENDAMENTOEntities();

            try
            {
                saveFileDialog1.InitialDirectory = "c:";
                saveFileDialog1.Title            = "Contatos";
                saveFileDialog1.FileName         = "";
                saveFileDialog1.Filter           = "Excel (2007)|*.xlsx";
                if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
                {
                    var contatos = db.AGENDA.Where(s => s.FLAG_EXCLUIDO != "S").OrderBy(s => s.NOME).ToList();

                    Console.WriteLine("Gerando arquivo Excel...");

                    var wb = new XLWorkbook();
                    //Cria a Planilha com o Nome
                    var ws = wb.Worksheets.Add("CONTATOS DA AGENDA");
                    ws.Cell("A1").Value = "CONTATOS DA AGENDA";
                    var range = ws.Range("A1:T2");
                    range.Merge().Style.Font.SetBold().Font.FontSize = 33;
                    // Cabeçalhos do Relatório
                    ws.Cell("A3").Value = "ID NO SISTEMA";
                    ws.Cell("B3").Value = "NOME DO CONTATO";
                    ws.Cell("C3").Value = "TIPO DE TELEFONE PRINCIPAL";
                    ws.Cell("D3").Value = "TELEFONE PRINCIPAL";
                    ws.Cell("E3").Value = "CEP RESIDENCIAL";
                    ws.Cell("F3").Value = "UF RESIDENCIAL";
                    ws.Cell("G3").Value = "LOCALIDADE RESIDENCIAL";
                    ws.Cell("H3").Value = "LOGRADOURO RESIDENCIAL";
                    ws.Cell("I3").Value = "NUMERO RESIDENCIAL";
                    ws.Cell("J3").Value = "BAIRRO RESIDENCIAL";
                    ws.Cell("K3").Value = "NOME DA EMPRESA";
                    ws.Cell("L3").Value = "CARGO NA EMPRESA";
                    ws.Cell("M3").Value = "CEP EMPRESA";
                    ws.Cell("N3").Value = "UF EMPRESA";
                    ws.Cell("O3").Value = "LOCALIDADE EMPRESA";
                    ws.Cell("P3").Value = "LOGRADOURO EMPRESA";
                    ws.Cell("Q3").Value = "NUMERO EMPRESA";
                    ws.Cell("R3").Value = "BAIRRO EMPRESA";
                    ws.Cell("S3").Value = "TELEFONES OPCIONAIS";
                    ws.Cell("T3").Value = "EMAIS OPCIONAIS";

                    int linha = 4;
                    foreach (var c in contatos)
                    {
                        ws.Cell("A" + linha.ToString()).Value = c.ID_AGENDA.ToString();
                        ws.Cell("B" + linha.ToString()).Value = c.NOME.ToString();
                        ws.Cell("C" + linha.ToString()).Value = c.TIPO_TEL_PRINCIPAL.ToString();
                        ws.Cell("D" + linha.ToString()).Value = c.TEL_PRINCIPAL.ToString();
                        if (c.CEP_RESIDENCIAL != null)
                        {
                            ws.Cell("E" + linha.ToString()).Value = c.CEP_RESIDENCIAL.ToString();
                        }
                        if (c.UF_RESIDENCIAL != null)
                        {
                            ws.Cell("F" + linha.ToString()).Value = c.UF_RESIDENCIAL.ToString();
                        }
                        if (c.LOCALIDADE_RESIDENCIAL != null)
                        {
                            ws.Cell("G" + linha.ToString()).Value = c.LOCALIDADE_RESIDENCIAL.ToString();
                        }
                        if (c.LOGRADOURO_RESIDENCIAL != null)
                        {
                            ws.Cell("H" + linha.ToString()).Value = c.LOGRADOURO_RESIDENCIAL.ToString();
                        }
                        if (c.NUMERO_RESIDENCIAL != null)
                        {
                            ws.Cell("I" + linha.ToString()).Value = c.NUMERO_RESIDENCIAL.ToString();
                        }
                        if (c.BAIRRO_RESIDENCIAL != null)
                        {
                            ws.Cell("J" + linha.ToString()).Value = c.BAIRRO_RESIDENCIAL.ToString();
                        }
                        if (c.NOME_EMPRESA != null)
                        {
                            ws.Cell("K" + linha.ToString()).Value = c.NOME_EMPRESA.ToString();
                        }
                        if (c.CARGO_EMPRESA != null)
                        {
                            ws.Cell("L" + linha.ToString()).Value = c.CARGO_EMPRESA.ToString();
                        }
                        if (c.CEP_EMPRESA != null)
                        {
                            ws.Cell("M" + linha.ToString()).Value = c.CEP_EMPRESA.ToString();
                        }
                        if (c.UF_RESIDENCIAL != null)
                        {
                            ws.Cell("N" + linha.ToString()).Value = c.UF_RESIDENCIAL.ToString();
                        }
                        if (c.LOCALIDADE_EMPRESA != null)
                        {
                            ws.Cell("O" + linha.ToString()).Value = c.LOCALIDADE_EMPRESA.ToString();
                        }
                        if (c.LOGRADOURO_EMPRESA != null)
                        {
                            ws.Cell("P" + linha.ToString()).Value = c.LOGRADOURO_EMPRESA.ToString();
                        }
                        if (c.NUMERO_EMPRESA != null)
                        {
                            ws.Cell("Q" + linha.ToString()).Value = c.NUMERO_EMPRESA.ToString();
                        }
                        if (c.BAIRRO_EMPRESA != null)
                        {
                            ws.Cell("R" + linha.ToString()).Value = c.BAIRRO_EMPRESA.ToString();
                        }

                        string telefoneOpcional = "";
                        string emailOpcional    = "";
                        var    telOP            = db.TELEFONE.Where(s => s.AGENDA_ID_AGENDA == c.ID_AGENDA).ToList();
                        foreach (var t in telOP)
                        {
                            if (telefoneOpcional == "")
                            {
                                telefoneOpcional = t.TIPO_TEL + ":" + t.TEL + "\n";
                            }
                            else
                            {
                                telefoneOpcional = telefoneOpcional + t.TIPO_TEL + ":" + t.TEL + "\n";
                            }
                        }
                        var emOP = db.EMAIL.Where(s => s.AGENDA_ID_AGENDA == c.ID_AGENDA).ToList();
                        foreach (var em in emOP)
                        {
                            if (emailOpcional == "")
                            {
                                emailOpcional = em.TIPO_EMAIL + ":" + em.EMAIL1 + "\n";
                            }
                            else
                            {
                                emailOpcional = emailOpcional + em.TIPO_EMAIL + ":" + em.EMAIL1 + "\n";
                            }
                        }
                        if (telefoneOpcional != "")
                        {
                            ws.Cell("S" + linha.ToString()).Value = telefoneOpcional;
                        }
                        if (emailOpcional != null)
                        {
                            ws.Cell("T" + linha.ToString()).Value = emailOpcional;
                        }
                        linha++;
                    }
                    range.CreateTable();
                    wb.SaveAs(saveFileDialog1.FileName.ToString());
                    ws.Dispose();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                db.Dispose();
            }
        }
Exemplo n.º 12
0
        private void btnAlterar_Click(object sender, EventArgs e)
        {
            AGENDAMENTOEntities db = new AGENDAMENTOEntities();

            try
            {
                var contato = db.AGENDA.Where(s => s.ID_AGENDA == VerAgendamento.idContato).FirstOrDefault();
                contato.NOME = txtNomeContato.Text;

                string tipoTelPrincipal = "";
                if (cbxTipoTelPrincipal.SelectedIndex == 2)
                {
                    tipoTelPrincipal = txtTipoTelPrincipal.Text;
                }
                if (cbxTipoTelPrincipal.SelectedIndex != 2)
                {
                    tipoTelPrincipal = cbxTipoTelPrincipal.SelectedItem.ToString();
                }
                contato.TIPO_TEL_PRINCIPAL = tipoTelPrincipal;
                contato.TEL_PRINCIPAL      = txtTelPrincipal.Text;
                if (chkResidencial.Checked)
                {
                    //passa todos os campos de texto para as variaveis
                    string cepResidencial         = txtCepResidencial.Text;
                    string UFResidencial          = txtUFResidencial.Text;
                    string localidadeResidencial  = txtLocalidadeResidencial.Text;
                    string logradouroResidencial  = txtLogradouroResidencial.Text;
                    string numeroResidencial      = txtNumeroResidencial.Text;
                    string complementoResidencial = txtComplementoResidencial.Text;
                    string bairroResidencial      = txtBairroResidencial.Text;

                    //insere os dados na tabela
                    contato.CEP_RESIDENCIAL         = cepResidencial;
                    contato.UF_RESIDENCIAL          = UFResidencial;
                    contato.LOCALIDADE_RESIDENCIAL  = localidadeResidencial;
                    contato.LOGRADOURO_RESIDENCIAL  = logradouroResidencial;
                    contato.NUMERO_RESIDENCIAL      = numeroResidencial;
                    contato.COMPLEMENTO_RESIDENCIAL = complementoResidencial;
                    contato.BAIRRO_RESIDENCIAL      = bairroResidencial;
                }
                else
                {
                    //passa todos os campos de texto para as variaveis
                    string cepResidencial         = null;
                    string UFResidencial          = null;
                    string localidadeResidencial  = null;
                    string logradouroResidencial  = null;
                    string numeroResidencial      = null;
                    string complementoResidencial = null;
                    string bairroResidencial      = null;

                    //insere os dados na tabela
                    contato.CEP_RESIDENCIAL         = cepResidencial;
                    contato.UF_RESIDENCIAL          = UFResidencial;
                    contato.LOCALIDADE_RESIDENCIAL  = localidadeResidencial;
                    contato.LOGRADOURO_RESIDENCIAL  = logradouroResidencial;
                    contato.NUMERO_RESIDENCIAL      = numeroResidencial;
                    contato.COMPLEMENTO_RESIDENCIAL = complementoResidencial;
                    contato.BAIRRO_RESIDENCIAL      = bairroResidencial;
                }

                //Caso a checkbox de inserir dados de endereço da empresa estiver selecionada sera incluido tambem no banco
                if (chkEmpresa.Checked)
                {
                    //passa todos os campos de texto para as variaveis
                    string nomeEmpresa        = txtNomeEmpresa.Text;
                    string cargo              = txtCargo.Text;
                    string cepEmpresa         = txtCepEmpresa.Text;
                    string UFEmpresa          = txtUFEmpresa.Text;
                    string localidadeEmpresa  = txtLocalidadeEmpresa.Text;
                    string logradouroEmpresa  = txtLogradouroEmpresa.Text;
                    string numeroEmpresa      = txtNumeroEmpresa.Text;
                    string complementoEmpresa = txtComplementoEmpresa.Text;
                    string bairroEmpresa      = txtBairroEmpresa.Text;

                    //insere os dados na tabela
                    contato.NOME_EMPRESA        = nomeEmpresa;
                    contato.CARGO_EMPRESA       = cargo;
                    contato.CEP_EMPRESA         = cepEmpresa;
                    contato.UF_EMPRESA          = UFEmpresa;
                    contato.LOCALIDADE_EMPRESA  = localidadeEmpresa;
                    contato.LOGRADOURO_EMPRESA  = logradouroEmpresa;
                    contato.NUMERO_EMPRESA      = numeroEmpresa;
                    contato.COMPLEMENTO_EMPRESA = complementoEmpresa;
                    contato.BAIRRO_EMPRESA      = bairroEmpresa;
                }
                else
                {
                    //passa todos os campos de texto para as variaveis
                    string nomeEmpresa        = null;
                    string cargo              = null;
                    string cepEmpresa         = null;
                    string UFEmpresa          = null;
                    string localidadeEmpresa  = null;
                    string logradouroEmpresa  = null;
                    string numeroEmpresa      = null;
                    string complementoEmpresa = null;
                    string bairroEmpresa      = null;

                    //insere os dados na tabela
                    contato.NOME_EMPRESA        = nomeEmpresa;
                    contato.CARGO_EMPRESA       = cargo;
                    contato.CEP_EMPRESA         = cepEmpresa;
                    contato.UF_EMPRESA          = UFEmpresa;
                    contato.LOCALIDADE_EMPRESA  = localidadeEmpresa;
                    contato.LOGRADOURO_EMPRESA  = logradouroEmpresa;
                    contato.NUMERO_EMPRESA      = numeroEmpresa;
                    contato.COMPLEMENTO_EMPRESA = complementoEmpresa;
                    contato.BAIRRO_EMPRESA      = bairroEmpresa;
                }
                db.SaveChanges();
                MessageBox.Show("Contato Alterado com sucesso", "SUCESSO", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                db.Dispose();
            }
        }
Exemplo n.º 13
0
        private void VerContatos_Load(object sender, EventArgs e)
        {
            AGENDAMENTOEntities db = new AGENDAMENTOEntities();

            try
            {
                //Adiciona os tipos de Telefone na combobox cbxTipoTelPrincipal
                cbxTipoTelPrincipal.Items.Add("Casa");
                cbxTipoTelPrincipal.Items.Add("Trabalho");
                cbxTipoTelPrincipal.Items.Add("Outros");

                //Busca o Contato no banco
                var contato = db.AGENDA.Where(s => s.ID_AGENDA == VerAgendamento.idContato).FirstOrDefault();

                //Insere os dados nos campos
                txtNomeContato.Text = contato.NOME;

                if (contato.TIPO_TEL_PRINCIPAL != "Casa")
                {
                    cbxTipoTelPrincipal.SelectedIndex = 0;
                }
                if (contato.TIPO_TEL_PRINCIPAL != "Trabalho")
                {
                    cbxTipoTelPrincipal.SelectedIndex = 1;
                }
                if (contato.TIPO_TEL_PRINCIPAL != "Casa" || contato.TIPO_TEL_PRINCIPAL != "Trabalho")
                {
                    cbxTipoTelPrincipal.SelectedIndex = 2;
                    txtTipoTelPrincipal.Visible       = true;
                    txtTipoTelPrincipal.Text          = contato.TIPO_TEL_PRINCIPAL;
                }
                txtTelPrincipal.Text = contato.TEL_PRINCIPAL;
                if (contato.CEP_RESIDENCIAL != null)
                {
                    chkResidencial.Checked         = true;
                    txtCepResidencial.Text         = contato.CEP_RESIDENCIAL;
                    txtUFResidencial.Text          = contato.UF_RESIDENCIAL;
                    txtLocalidadeResidencial.Text  = contato.LOCALIDADE_RESIDENCIAL;
                    txtLogradouroResidencial.Text  = contato.LOGRADOURO_RESIDENCIAL;
                    txtNumeroResidencial.Text      = contato.NUMERO_RESIDENCIAL;
                    txtComplementoResidencial.Text = contato.COMPLEMENTO_RESIDENCIAL;
                    txtBairroResidencial.Text      = contato.BAIRRO_RESIDENCIAL;
                }
                if (contato.NOME_EMPRESA != null || contato.CEP_EMPRESA != null)
                {
                    chkEmpresa.Checked         = true;
                    txtNomeEmpresa.Text        = contato.NOME_EMPRESA;
                    txtCargo.Text              = contato.CARGO_EMPRESA;
                    txtCepEmpresa.Text         = contato.CEP_EMPRESA;
                    txtUFEmpresa.Text          = contato.UF_EMPRESA;
                    txtLocalidadeEmpresa.Text  = contato.LOCALIDADE_EMPRESA;
                    txtLogradouroEmpresa.Text  = contato.LOGRADOURO_EMPRESA;
                    txtNumeroEmpresa.Text      = contato.NUMERO_EMPRESA;
                    txtComplementoEmpresa.Text = contato.COMPLEMENTO_EMPRESA;
                    txtBairroEmpresa.Text      = contato.BAIRRO_EMPRESA;
                }
                gdvEmail.DataSource = db.EMAIL.Where(s => s.AGENDA_ID_AGENDA == contato.ID_AGENDA).Select(s => new
                {
                    ID    = s.ID_EMAIL,
                    TIPO  = s.TIPO_EMAIL,
                    EMAIL = s.EMAIL1
                }).ToList();
                gdvTelefone.DataSource = db.TELEFONE.Where(s => s.AGENDA_ID_AGENDA == contato.ID_AGENDA).Select(s => new
                {
                    ID    = s.ID_TELEFONE,
                    TIPO  = s.TIPO_TEL,
                    EMAIL = s.TEL
                }).ToList();
            }
            catch (Exception ex)
            {
            }
            finally
            {
                db.Dispose();
            }
        }
Exemplo n.º 14
0
        //insere os dados no banco
        private void btnCadastrar_Click(object sender, EventArgs e)
        {
            AGENDAMENTOEntities db = new AGENDAMENTOEntities();

            try
            {
                AGENDA agen = new AGENDA();
                //Atribui em variaveis osc campo do nome de contato e o telefone principal
                string nomeContato      = txtNomeContato.Text;
                string tipoTelPrincipal = "";
                if (cbxTipoTelPrincipal.SelectedIndex == 2)
                {
                    tipoTelPrincipal = txtTipoTelPrincipal.Text;
                }
                if (cbxTipoTelPrincipal.SelectedIndex != 2)
                {
                    tipoTelPrincipal = cbxTipoTelPrincipal.SelectedItem.ToString();
                }
                string telPrincipal = txtTelPrincipal.Text;

                //passa os obrigatorios para a tabela
                agen.NOME               = nomeContato;
                agen.TEL_PRINCIPAL      = telPrincipal;
                agen.TIPO_TEL_PRINCIPAL = tipoTelPrincipal;

                //Caso a checkbox de inserir dados de endereço residencial estiver selecionada sera incluido tambem no banco
                if (chkResidencial.Checked)
                {
                    //passa todos os campos de texto para as variaveis
                    string cepResidencial         = txtCepResidencial.Text;
                    string UFResidencial          = txtUFResidencial.Text;
                    string localidadeResidencial  = txtLocalidadeResidencial.Text;
                    string logradouroResidencial  = txtLogradouroResidencial.Text;
                    string numeroResidencial      = txtNumeroResidencial.Text;
                    string complementoResidencial = txtComplementoResidencial.Text;
                    string bairroResidencial      = txtBairroResidencial.Text;

                    //insere os dados na tabela
                    agen.CEP_RESIDENCIAL         = cepResidencial;
                    agen.UF_RESIDENCIAL          = UFResidencial;
                    agen.LOCALIDADE_RESIDENCIAL  = localidadeResidencial;
                    agen.LOGRADOURO_RESIDENCIAL  = logradouroResidencial;
                    agen.NUMERO_RESIDENCIAL      = numeroResidencial;
                    agen.COMPLEMENTO_RESIDENCIAL = complementoResidencial;
                    agen.BAIRRO_RESIDENCIAL      = bairroResidencial;
                }

                //Caso a checkbox de inserir dados de endereço da empresa estiver selecionada sera incluido tambem no banco
                if (chkEmpresa.Checked)
                {
                    //passa todos os campos de texto para as variaveis
                    string nomeEmpresa        = txtNomeEmpresa.Text;
                    string cargo              = txtCargo.Text;
                    string cepEmpresa         = txtCepEmpresa.Text;
                    string UFEmpresa          = txtUFEmpresa.Text;
                    string localidadeEmpresa  = txtLocalidadeEmpresa.Text;
                    string logradouroEmpresa  = txtLogradouroEmpresa.Text;
                    string numeroEmpresa      = txtNumeroEmpresa.Text;
                    string complementoEmpresa = txtComplementoEmpresa.Text;
                    string bairroEmpresa      = txtBairroEmpresa.Text;

                    //insere os dados na tabela
                    agen.NOME_EMPRESA        = nomeEmpresa;
                    agen.CARGO_EMPRESA       = cargo;
                    agen.CEP_EMPRESA         = cepEmpresa;
                    agen.UF_EMPRESA          = UFEmpresa;
                    agen.LOCALIDADE_EMPRESA  = localidadeEmpresa;
                    agen.LOGRADOURO_EMPRESA  = logradouroEmpresa;
                    agen.NUMERO_EMPRESA      = numeroEmpresa;
                    agen.COMPLEMENTO_EMPRESA = complementoEmpresa;
                    agen.BAIRRO_EMPRESA      = bairroEmpresa;
                }
                //adciona na agenda
                db.AGENDA.Add(agen);
                db.SaveChanges();
                //busca o contato que foi cadastrado para relacionar o contato com a lista de telefones e emails opcionais
                var ContatoCadastrado = db.AGENDA.OrderByDescending(s => s.ID_AGENDA).FirstOrDefault();

                //Cria variaveis list para atribuir todos os telefones e emails cadastrados nas listbox
                List <TELEFONE> telOp   = new List <TELEFONE>();
                List <EMAIL>    emailOp = new List <EMAIL>();

                if (listTelOpcional.Items.Count > 0 || ListEmail.Items.Count > 0)
                {
                    //se a lista de telefones Opcionais possui a quantidade de registros maior que 0 sera atribuido a lista
                    if (listTelOpcional.Items.Count > 0)
                    {
                        //percorre pela listbox e insere cada um no banco
                        foreach (var item in listTelOpcional.Items)
                        {
                            //Salva os dados do telefone na string
                            string teletipo = item.ToString();
                            //separa a string de acordo com a quantidade de char que possui
                            string[] teletipoSep = teletipo.Split(new char[1] {
                                ';'
                            });
                            string tipoTel = teletipoSep[0];
                            string Tel     = teletipoSep[1];
                            telOp.Add(new TELEFONE
                            {
                                AGENDA_ID_AGENDA = ContatoCadastrado.ID_AGENDA,
                                TIPO_TEL         = tipoTel,
                                TEL = Tel
                            });
                        }
                        db.TELEFONE.AddRange(telOp);
                    }
                    //Caso se a lista com os emails for > 0 ira entrar no if
                    if (ListEmail.Items.Count > 0)
                    {
                        //percorre toda listBox para inserir na lista de emails
                        foreach (var item in ListEmail.Items)
                        {
                            string   emailTipo    = item.ToString();
                            string[] emailtipoSep = emailTipo.Split(new char[1] {
                                ';'
                            });
                            string tipoEmail = emailtipoSep[0];
                            string email     = emailtipoSep[1];
                            //insere na lista o email
                            emailOp.Add(new EMAIL
                            {
                                AGENDA_ID_AGENDA = ContatoCadastrado.ID_AGENDA,
                                TIPO_EMAIL       = tipoEmail,
                                EMAIL1           = email
                            });
                        }
                        //Adiciona todos os email na tabela de email
                        db.EMAIL.AddRange(emailOp);
                    }
                    //adiciona os telefones e emails opcionais no banco
                    db.SaveChanges();
                }
                MessageBox.Show("Cadastro Efetuado com sucesso", "SUCESSO", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception er)
            {
                MessageBox.Show(er.Message);
            }
            finally
            {
                db.Dispose();
            }
        }