# Web Development > ASP.NET Countries Capital DropdownList

## ushalakshmi

Hi I am a beginner to asp.net.
Could any one please help me ...
I have a database table with two colums ( column one has all the countries and column two has all the capitols of that particular country.)

In my .aspx page I have a dropdown list and a text box.
Drop downlist will display all the countries and if I select a country then its capitol should be displayed in the textbox.

Thanks in Advance...
Your help is much appreciated....

----------


## vikasvaidya

> Hi I am a beginner to asp.net.
> Could any one please help me ...
> I have a database table with two colums ( column one has all the countries and column two has all the capitols of that particular country.)
> 
> In my .aspx page I have a dropdown list and a text box.
> Drop downlist will display all the countries and if I select a country then its capitol should be displayed in the textbox.
> 
> Thanks in Advance...
> Your help is much appreciated....


Hi Usha,

Bind the dropdown list with DataTextField as the countryName and DataValueField as capital

then in onSelectedIndex_Change event of dropdown write code like
TextBox1.Text = DropDownList1.SelectedValue


---V V---
Vikas Vaidya

_[Please mark the post as Thank if u find the answer useful]_

----------


## kadhar

You have told that you have two clomns. 1.dropdownlist,2.text box and second one should nit be a textbox. It should be a another dropdownlist. If you are using sql server initially you have to create table for country. Then you have to create nexttable for capitals. For example create table capitals(capital nvarchar(10)) insert into capitals (capital)values(delhi) totally you have to create 3 loaddrop() 1.edit 2.to bind datatextfield ofcapitals 3.if u click any countries to show capitals as well as u have to write in your home page like if(!pageispostback) { loaddrop(); }

----------


## raji1

For displaying country private sub binddropdown() try dim da as new sqldataadapter("select * from countries order by country", myconnection) dim ds as new dataset myconnection.open() da.fill(ds) myconnection.close() dropdownlist1.datatextfield = "country_name" dropdownlist1.datavaluefield = "country_id" dropdownlist1.datasource = ds dropdownlist1.databind() catch ex as exception end try end sub then you have to write code on dropdownlist1_selectedindexchanged cmd = new sqlcommand("sp_capital", myconnection) ' here i am using stored procedue or u can use the sqlquery directly /* create procedure sp_capital @countryid as integer as begin select ----------- where dbo.city.country_id=@countryid end go*/ cmd.commandtype = commandtype.storedprocedure cmd.parameters.add("@countryid", sqldbtype.int) cmd.parameters("@countryid").value = dropdownlist1.selectedvalue.trim() da = new sqldataadapter(cmd) da.fill(ds) myconnection.open() cmd.executenonquery() myconnection.close() end sub hope this will find u useful

----------


## rajesh.karnatakam

> Hi I am a beginner to asp.net.
> Could any one please help me ...
> I have a database table with two colums ( column one has all the countries and column two has all the capitols of that particular country.)
> 
> In my .aspx page I have a dropdown list and a text box.
> Drop downlist will display all the countries and if I select a country then its capitol should be displayed in the textbox.
> 
> Thanks in Advance...
> Your help is much appreciated....




First u connect to the database,retrieve all the countries and bind it to the dropdownlist,u have to bind it if it is not postback ie:firsttime 
if(!postback)
Dropdown list is having selected_Index_changed 

on that event u take the selected country value into the variable

pass the variable as parameter to the stored procedure and take the capital of that country and bind it to the textbox control

----------

