private void EditClient_Load(object sender, EventArgs e) { try { conn = new SqlConnection(connectionString); conn.Open(); string sqlCliente = "SELECT * FROM tb_clientes WHERE codigo = @cod"; SqlCommand comm = new SqlCommand(sqlCliente, conn); comm.Parameters.AddWithValue("@cod", codigo); SqlDataReader read = comm.ExecuteReader(); if (read.HasRows) { if (read.Read()) { txtName.Text = read.GetString(4); txtSurname.Text = read.GetString(5); txtEmail.Text = read.GetString(6); txtBirthday.Text = read.GetString(9); txtPhone.Text = read.GetString(7); txtCellphone.Text = read.GetString(8); txtAge.Text = read.GetInt32(10).ToString(); txtCpf.Text = read.GetString(12); txtCep.Text = read.GetString(15); txtNumber.Text = read.GetInt32(14).ToString(); string cep = txtCep.Text; cep = Basics.limpaCep(cep); string api_url = "https://api.postmon.com.br/v1/cep/" + cep; string request = Basics.httpGet(api_url); Endereco result = JsonConvert.DeserializeObject <Endereco>(request); if (!String.IsNullOrEmpty(result.Logradouro)) { txtAddress.Text = result.Logradouro; txtNeighborhood.Text = result.Bairro; txtCity.Text = result.Cidade; txtState.Text = result.EstadoInfo.Nome; txtAddress.ReadOnly = txtNeighborhood.ReadOnly = txtCity.ReadOnly = txtState.ReadOnly = true; } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private void txtCep_TextChanged(object sender, EventArgs e) { string cepText = txtCep.Text.ToString(); try { int inputSize = txtCep.Text.ToString().Length; if (inputSize == 9) { string cep = cepText; cep = Basics.limpaCep(cep); string api_url = "https://api.postmon.com.br/v1/cep/" + cep; string request = Basics.httpGet(api_url); Endereco result = JsonConvert.DeserializeObject <Endereco>(request); if (!String.IsNullOrEmpty(result.Logradouro)) { txtAddress.Text = result.Logradouro; txtNeighborhood.Text = result.Bairro; txtCity.Text = result.Cidade; txtState.Text = result.EstadoInfo.Nome; txtAddress.ReadOnly = txtNeighborhood.ReadOnly = txtCity.ReadOnly = txtState.ReadOnly = true; } } else { txtAddress.Text = txtNeighborhood.Text = txtCity.Text = txtState.Text = ""; txtAddress.ReadOnly = txtNeighborhood.ReadOnly = txtCity.ReadOnly = txtState.ReadOnly = false; } } catch (Exception ex) { Console.WriteLine("Erro: " + ex.Message); } }