How to do a zip search in VB

2

How do I collect address data (Street, City, Neighborhood, Street) for a site, referring to the input Cep using VB ?

I have a similar code in C#

I see two alternatives:

  • Translate the code to VB
  • Run the c# code from VB to find the address and collect the results, storing them in textboxes of address fill form in VB , for instant visualization (do not know if it is possible)
  • Someone could help with some of these alternatives above.

    Code in C# :

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string xml = "http://cep.republicavirtual.com.br/web_cep.phpcep=@cep&formato=xml"
               .Replace("@cep",maskedTextBox1.Text); 
    
            DataSet ds = new DataSet();
    
            ds.ReadXml(xml);
    
            label1.Text = ds.Tables[0].Rows[0][1].ToString();
            label3.Text= ds.Tables[0].Rows[0][5].ToString();
    
            textBox2.Text = ds.Tables[0].Rows[0][6].ToString();
            textBox3.Text = ds.Tables[0].Rows[0][4].ToString();
            textBox4.Text = ds.Tables[0].Rows[0][3].ToString();
            textBox5.Text = ds.Tables[0].Rows[0][2].ToString();
    
            dataGridView1.DataSource = ds.Tables[0];
            }
        }
    }
    
        
    asked by anonymous 21.04.2014 / 19:41

    2 answers

    3

    Converting C # to VB.NET

    Dim xml As String = String.Format("http://cep.republicavirtual.com.br/web_cep.php?cep={0}&formato=xml", maskedTextBox1.Text)
    
    Dim ds As New DataSet()
    ds.ReadXml(xml)
    
    label1.Text = ds.Tables(0).Rows(0)(1).ToString()
    label3.Text = ds.Tables(0).Rows(0)(5).ToString()
    
    textBox2.Text = ds.Tables(0).Rows(0)(6).ToString()
    textBox3.Text = ds.Tables(0).Rows(0)(4).ToString()
    textBox4.Text = ds.Tables(0).Rows(0)(3).ToString()
    textBox5.Text = ds.Tables(0).Rows(0)(2).ToString()
    

    Changes:

    21.04.2014 / 19:57
    2

    Hello, I do this:

    Here's the webservice subscribe ByJG is free!

    After adding the reference, as I use in several forms I created in the Module a global variable and the function that receives the parameters:

     Public CepResultBusca
    

    This is the function:

     Public Sub BuscarCep(cep As String)
        Dim connWebCep As New br.com.byjg.www.CEPService
    
        //verificação simples apenas para saber se o campo cep esta preenchido
        If Trim(cep) = Nothing Then
            Exit Sub
        End If
        //no meu caso faço a busca apenas por numero do cep, que é passado por parametro
        //.obterLogradouroAuth vai da sua necessidade!
        CepResultBusca = connWebCep.obterLogradouroAuth(cep, "login", "senha")
    End Sub
    

    Create in the KeyDown event of TextBox cep the treatment for the search result, if you prefer you can do it right in the module and return already formatted:

     Private Sub cep_cobranca_KeyDown(sender As Object, e As KeyEventArgs) Handles cobranca_cep.KeyDown
        If e.KeyCode = 13 Then
            BuscarCep(cobranca_cep.Text)
            Dim r As String = CepResultBusca
            Trim(r)
            Try
                Dim rua = Split(r, ",")(0)
                Dim bairro = Split(r, ",")(1)
                Dim cidade = Split(r, ",")(2)
                Dim uf = Split(r, ",")(3)
                cobranca_endereco.Text = Trim(rua)
                cobranca_bairro.Text = Trim(bairro)
                cobranca_cidade.Text = Trim(cidade)
                cobranca_uf.Text = Trim(uf)
            Catch ex As Exception
                MsgBox(r)
                Exit Sub
            End Try
        End If
    End Sub
    

    In case the zip is not found the webservice returns a message that I treated in the exception of try cath with exit sub to exit here:

     MsgBox(r)
     Exit Sub
    
        
    12.05.2015 / 20:04