Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi!

I have public SQL server and i want to resolve, user fill the postal code and leave textbox then execute one sqlquery to what autofill the correct city for postal code

What I have tried:

C#
private void txt_tanul_irszam_Leave(object sender, System.EventArgs e)
{
    if (txt_tanul_irszam.Text!=null)
    {
        string connectionstring = null;
        SqlConnection con;
        connectionstring = "Data Source ="";
        con = new SqlConnection(connectionstring);
    }
}
Posted
Updated 16-Jun-18 15:18pm
v2
Comments
Eric Lynch 14-Jun-18 17:47pm    
Postal codes are a tricky country-by-country thing. You have three issues to solve: 1) finding the postal code in a free-form address (hopefully you have a fixed field address) 2) a good source of postal code information and 3) a very easy lookup. For the second, check out GeoNames.org. For the third, its really easy to code. Once you know the postal code and have a source of information, simply describe the schema (format of the database) and anyone here can provide a simple solution.
Member 13873270 15-Jun-18 2:35am    
The country is fix. I have one country's postal codes with city names
Member 13873270 15-Jun-18 2:38am    
Postalcode table(Postal code city column)
Eric Lynch 15-Jun-18 6:27am    
You're reply is a bit terse (and unclear)...not exactly a schema. So, I'm forced to make some assumptions. I'll assume the following: you know your connection string, your table name is "Postalcode", your column names are "postalcode" and "city", both columns are compatible with string (e.g. varchar, nvarchar, etc.), the table is (minimally) indexed by postal code (for performance), you want to use ADO.NET (instead of LINQ to SQL or LINQ to Entities), and your question is how to get the name of a city given a postal code.


If this is all true (a huge IF), the first thing you should know is that most SQL classes in C# are IDisposable, so you should make use of the "using" statement.


Your solution should be approximately as follows. Note, I'm typing it by hand here, so there may be some slight syntax errors.

using (var connection = new SqlConnection(connectionString))
{
connection.Open();

using (var command = new SqlCommand("SELECT city FROM Postalcodes WHERE postalcode=@PostalCode", connection))
{
command.Parameters.AddWithValue("postalcode", "01010");

using (var reader = command.ExecuteReader())
{
string city = reader.Read() ?
reader[0] as string : "Unknown";

if (reader.Read())
city = "Ambiguous";
}
}
}
Member 13873270 15-Jun-18 6:55am    
This program will student registartion form. the user typing the correct postal code to postal code textbox when user leave (leave event) the textbox then select city from postalcode where postalcode = what user given. That result print out to city textbox

1 solution

You want an SQL Server "scalar" query.

But, frankly, considering the lack of effort you've put out relative to all the people who have been trying to help, I would say you need to do "more work" before you get anymore hand outs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900