Beispiel #1
0
        private void btnConsultar_Click(object sender, EventArgs e)
        {
            if (txtCnpj.Text != string.Empty)
            {
                try
                {
                    string cnpj = txtCnpj.Text.Trim().Replace(".", "").Replace("/", "").Replace("-", "");

                    HttpRequestMessage  request  = new HttpRequestMessage(HttpMethod.Get, uri + cnpj);
                    HttpResponseMessage resposta = httpClient.SendAsync(request).Result;
                    resposta.EnsureSuccessStatusCode();

                    string conteudo = resposta.Content.ReadAsStringAsync().Result;

                    if (conteudo != "")
                    {
                        Empresa.Domain.Entities.Empresa empresa = new Empresa.Domain.Entities.Empresa(cnpj);

                        empresa = JsonConvert.DeserializeObject <Empresa.Domain.Entities.Empresa>(conteudo);
                        {
                            txtId.Text                = (empresa.Id).ToString();
                            txtCnpj.Text              = empresa.Cnpj;
                            txtNomeEmpresarial.Text   = empresa.NomeEmpresarial;
                            txtNomeFantasia.Text      = empresa.NomeFantasia;
                            txtPorte.Text             = empresa.Porte;
                            txtLogradouro.Text        = empresa.Logradouro;
                            txtNumero.Text            = empresa.Numero;
                            txtComplemento.Text       = empresa.Complemento;
                            txtCep.Text               = empresa.Cep;
                            txtBairro.Text            = empresa.Bairro;
                            txtMunicipio.Text         = empresa.Municipio;
                            txtUf.Text                = empresa.Uf;
                            txtEmail.Text             = empresa.Email;
                            txtTelefone.Text          = empresa.Telefone;
                            txtSituacaoCadastral.Text = empresa.SituacaoCadastral;
                        }
                    }
                    else
                    {
                        MessageBox.Show("A empresa consultada não existe no banco de dados.", "ATENÇÃO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Falha ao conectar-se com o banco de dados." + ex.ToString(), "ATENÇÃO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else
            {
                MessageBox.Show("Insira o CNPJ da Empresa para consultar.", "ATENÇÃO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
Beispiel #2
0
        private void btnAtualizar_Click(object sender, EventArgs e)
        {
            if (txtCnpj.Text != string.Empty && txtNomeEmpresarial.Text != string.Empty && txtNomeFantasia.Text != string.Empty && txtPorte.Text != string.Empty &&
                txtLogradouro.Text != string.Empty && txtNumero.Text != string.Empty && txtCep.Text != string.Empty && txtBairro.Text != string.Empty &&
                txtMunicipio.Text != string.Empty && txtTelefone.Text != string.Empty)
            {
                try
                {
                    string cnpj = txtCnpj.Text.Trim().Replace(".", "").Replace("/", "").Replace("-", "");

                    Empresa.Domain.Entities.Empresa empresa = new Empresa.Domain.Entities.Empresa(cnpj)
                    {
                        NomeEmpresarial = txtNomeEmpresarial.Text,
                        NomeFantasia    = txtNomeFantasia.Text,
                        Porte           = txtPorte.Text,
                        Logradouro      = txtLogradouro.Text,
                        Numero          = txtNumero.Text,
                        Complemento     = txtComplemento.Text,
                        Cep             = txtCep.Text,
                        Bairro          = txtBairro.Text,
                        Municipio       = txtMunicipio.Text,
                        Uf       = txtUf.Text,
                        Email    = txtEmail.Text,
                        Telefone = txtTelefone.Text
                    };

                    string conteudo = JsonConvert.SerializeObject(empresa);

                    var stringContent = new StringContent(conteudo, Encoding.UTF8, "application/json");

                    HttpRequestMessage  request  = new HttpRequestMessage(HttpMethod.Put, uri);
                    HttpResponseMessage resposta = httpClient.PutAsync(uri, stringContent).Result;
                    resposta.EnsureSuccessStatusCode();

                    if (resposta.IsSuccessStatusCode)
                    {
                        MessageBox.Show("Empresa atualizada com sucesso.", "ATENÇÃO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Não foi possível atualizar a empresa, verifique conexão." + ex.ToString(), "ATENÇÃO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else
            {
                MessageBox.Show("Campos com asterisco * são obrigatórios.", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }