Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Visual Studio 2008 (Visual Basic OR C#) -- Lookup Table w/ TextBoxes

Posted on 2010-01-09
Medium Priority
Last Modified: 2013-11-26
Hi Experts!  <Visual Basic 2008 Newbie here> !

I want to create a very simple (1 page VB2008 form), which connects to an external table, "looks up" values based on a Zip Code ... and populates three(3) textboxes on my form.  Note:  I purchased a TABLE (ZipInfo) from a commercial carrier, which is ALREADY POPULATED with every valid zipcode, city, state and county in the country.

So.. to begin:
1)  I want to create a VB 2008 Form, which will have only four (4) text boxes, labeled as follows: txtZipcode,  txtCity,   txtState,   and   txtCounty.  

2)  I want the user to enter in a 'Zip Code' in the textbox labeled:   'txtZipcode'.  After the
     user presses 'ENTER',

3)  I want my VB2008 form to then execute some code (???) which will:
     a) lookup the matching city, state and county field values, from the Zipcode (ZipInfo) table;
     b) populate each of the three textboxes (labeled txtCity, txtState, txtCounty) on my
         VB2008 form, with the corresponding information from the Zipcode table.

I have ATTACHED a picture of the VB2008 form, I've created so far.

I don't know anything, other then to drag the textboxes from the VB2008  toolbox to a blank form.  I don't understand ADO, or coding or where to put the code to begin with.  I don't know how to reference the connection to the ZipInfo table.  I'm not even certain how to connect properly to the data source.

When I asked this question before, I was given a VB 6 solution, which doesn't appear to work with VB 2008.  

I would really like to know a VB2008 solution and a C# solution, ideally ... so that I can try and understand how making data connections, connecting to the data and populating forms works in Visual Studio 2008.

In conclusion,

I need a 'STEP-BY-STEP' answer, so that I can learn easily.  I'm guessing that there are likely many blog readers who would benefit by your answer(s).

Thanks Everyone!

Question by:DelphiNewb
  • 3
  • 2
LVL 41

Expert Comment

ID: 26274455
what database would be that table in?
here is are some basic tutorials on using Ado.net with C#:

Author Comment

ID: 26275984
Hello Guru_Sami --

Thank you very much Guru_Sami for the tutorial links... I am studying them now...

To answer your question, the database I was considering was either SQL Server Express 2008, or...

Thank you again for the information ... this might provide me enough information.  Reviewing now.

If you come across any additional information, I would appreciate it. --- Thomas
LVL 83

Accepted Solution

CodeCruiser earned 2000 total points
ID: 26305173
Here is some example code in VB.NET.
Private Sub txtZipCode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtZipCode.KeyPress
        If Asc(e.KeyChar) = 13 Then
            Dim dbcon As New SqlConnection(connectionstring)
            Dim dbcmd As New SqlCommand
            dbcmd.Connection = dbcon
            dbcmd.CommandText = "Select City, State, County from tblAddresses where zipcode=" & txtZipCode.Text
            Dim dbreader As SqlDataReader = dbcmd.ExecuteReader
            If dbreader.HasRows Then
                txtCity.Text = dbreader.Item("City")
                txtState.Text = dbreader.Item("State")
                txtCounty.Text = dbreader.Item("County")
            End If
        End If
    End Sub

Open in new window

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.


Author Comment

ID: 26333404
I want to thank CodeCruiser very much ... your example was right on, and will work great!
Thank you so much for your clear and complete answer!

CodeCruiser ... you rock!

Thanks --- DelphiNewb

Author Closing Comment

ID: 31675018
CodeCruiser's answer was very very helpful.

I am now his or her (CodeCruiser's) fan!
LVL 83

Expert Comment

ID: 26334342
Glad to help :-)

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question