How to do a ZIP search in VB

Asked

Viewed 1,303 times

2

How do I collect address data(Street, City, Neighborhood, Street) of a site, referring to the entry zip code using VB?

I have a similar code on C#

I see two alternatives:

  1. Translate the code to VB
  2. Execute the code of c# from the VB to find the address and collect the results, storing them in textboxes of the address form at VB, for instant display (I don’t know if it’s possible)

Could anyone 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];
        }
    }
}
  • Put this o using System inside all code, formatting in an ideal way

  • The conversion worked ?

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:

  • To declare variable used Dim;
  • Exchange [] for ();
  • To format the String Url was used String.Format;
  • 1

    Thank you very much! It worked! heheheheh despite having some things I found simple to translate, such as variable definition and value assignment the textboxes... I had no idea what the search part would look like in the site’s database... o {0} instead of @. Very useful, it worked cute!

  • 1

    @user7854, here is the explanation of the String.Format of the website microsoft: http://msdn.microsoft.com/pt-br/library/system.string.format(v=vs.110). aspx

2

Hello, I do so:

Here’s the webservice register Byjg is free!

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

 Public CepResultBusca

That 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 result of the search, if you prefer you can do it directly 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 cep is not found the webservice returns a message I treated in the Try Cath Exception with Exit sub to exit here:

 MsgBox(r)
 Exit Sub

Browser other questions tagged

You are not signed in. Login or sign up in order to post.