I am developing a lookup page i.e when i provide a zip code in the textbox and submit the button it should display the details like city,state etc. I've created two tables in the database,

one with zip code and other with details city,state etc. how can i display the data in the same page when zip code is entered in the textbox . Can someopne give me a sample code or an idea to develop this page. i am new to programming so please someone help me.

7 Years
Discussion Span
Last Post by anetbegin

I've done something like that in an application i built sometime ago.

firstly, you dont need two seperate tables. Just have one table with the zip code, city, state etc and everything you need.

You can create a button next to the text box and on the click event, just write the code for connecting to the database and running a simple query like
SELECT * from zip_table where zip = <<zip code from textbox>>
Get the result and display in the relevant text boxes..


what is the code to display them in relevant text boxes in the same page? i mean can you give me an idea


i have used the code given above select * from ziptable where zip = textbox1.text;
but i am getting an error.


Try this:

SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = cn; // This is your connection string
            cmd.CommandText = "SELECT * from zip_table where zip = " + textZip.Text;

            SqlDataReader dr = cmd.ExecuteReader();
            textCity.Text = dr["City"].ToString();
            textState.Text = dr["State"].ToString();

Im assuming you have your connection string sorted out (cn).
I would personally use stored procedures instead of a direct query, but this one will work just as well.

Edited by jatin24: n/a


hey jatin,

this code is working. thank you very much for helping me

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.