473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

populate two drop downlist and textbox

hi
i want to populate country's name in 1st dropdownlist and their states should be placed in the 2nd dropdownlist, if we select any state it should place the name of the capital in the textbox besides that and so on.....
If possible send me code

thank you in advance
raghu
Nov 27 '07 #1
12 1631
dip_developer
648 Expert 512MB
hi
i want to populate country's name in 1st dropdownlist and their states should be placed in the 2nd dropdownlist, if we select any state it should place the name of the capital in the textbox besides that and so on.....
If possible send me code

thank you in advance
raghu
you should keep a good table structure for your work....I recommend four(4) tables to create...

1.tblCountry_Master
------------------------------
CountryId(PrimaryKey),CountryName

2.tblSate_Master
-----------------------
StateID(PK),StateName

3. tblStateCountryRelation
-------------------------------------
CountryId,StateId

4.tblStateCapital
-------------------------------
StateId(PK),Capital_Name

populate your country dropdown first with sql like...........
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM tblCountry_Master
make a dataset.....set the datasource of the dropdown to the Dataset.
if you are making a windows application then set ValueMember of the dropdownlist to "CountryId" and DisplayMember to "CountryName"[ you have not mentioned about your application.......In web application these are DataValueField and DataTextField...........]

Now in the SelectedIndexChanged event of your country dropdown choose the valuefield(Here it is CountryId)

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim country_id As String 
  3. country_id = myDropdown.SelectedValue 
  4.  
Now after getting the country_id you can easily fetch the corresponding statename with a simple query like....

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT StateID,StateName FROM tblSate_Master S INNER JOIN 
  3. tblStateCountryRelation SC ON S.StateID=SC.StateId WHERE 
  4. CountryId='" & country_id & "'
again create a dataset with these records and bind it to State dropdownlist

Repeat the above mechanism for state capital name::
--------------------------------------------------------------------------------
In the SelectedIndexChanged event of your state dropdown choose the valuefield(Here it is StateId)..........and choose the capital from tblStateCapital with sql statement

Expand|Select|Wrap|Line Numbers
  1.  SELECT Capital_Name FROM tblStateCapital WHERE 
  2. StateId='" & state_id & "'
  3.  
Hope you understand.........I have not given you the full code ......try yourself....I think you can do it now........
Nov 27 '07 #2
you should keep a good table structure for your work....I recommend four(4) tables to create...

1.tblCountry_Master
------------------------------
CountryId(PrimaryKey),CountryName

2.tblSate_Master
-----------------------
StateID(PK),StateName

3. tblStateCountryRelation
-------------------------------------
CountryId,StateId

4.tblStateCapital
-------------------------------
StateId(PK),Capital_Name

populate your country dropdown first with sql like...........
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM tblCountry_Master
make a dataset.....set the datasource of the dropdown to the Dataset.
if you are making a windows application then set ValueMember of the dropdownlist to "CountryId" and DisplayMember to "CountryName"[ you have not mentioned about your application.......In web application these are DataValueField and DataTextField...........]

Now in the SelectedIndexChanged event of your country dropdown choose the valuefield(Here it is CountryId)

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim country_id As String 
  3. country_id = myDropdown.SelectedValue 
  4.  
Now after getting the country_id you can easily fetch the corresponding statename with a simple query like....

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT StateID,StateName FROM tblSate_Master S INNER JOIN 
  3. tblStateCountryRelation SC ON S.StateID=SC.StateId WHERE 
  4. CountryId='" & country_id & "'
again create a dataset with these records and bind it to State dropdownlist

Repeat the above mechanism for state capital name::
--------------------------------------------------------------------------------
In the SelectedIndexChanged event of your state dropdown choose the valuefield(Here it is StateId)..........and choose the capital from tblStateCapital with sql statement

Expand|Select|Wrap|Line Numbers
  1.  SELECT Capital_Name FROM tblStateCapital WHERE 
  2. StateId='" & state_id & "'
  3.  
Hope you understand.........I have not given you the full code ......try yourself....I think you can do it now........


thank you for giving me such a good detail, i will try to sought my problem
Nov 28 '07 #3
Hi,
I feel 2 tables are enough

1. Country Table (Country_ID(Primary Key),Country_Name)
2. State Table (State_ID(Primary Key), State_Name,Country_Id(Foreign Key), Capital_City)

Each State will have one Capital City so i feel one more table is not necessary for Capital City

In the SelectedIndexChanged event of the Country DropDown write the below query

Select State_Id,State_Name From State Where Country_Id=ddlCountry.selectedValue ( Country dropDownList Name)


In the SelectedIndexChanged event of the State DropDown write the below query

Select Capital_City From State Where State_ID=ddlState.selectedValue ( StatedropDownList Name)


I hope this will work
Nov 28 '07 #4
i am developing web application using c#,,
i got an idea how to populate but were shoould i write coding for state_master coding(select * from state_master......)
Nov 29 '07 #5
dip_developer
648 Expert 512MB
Hi,
I feel 2 tables are enough

1. Country Table (Country_ID(Primary Key),Country_Name)
2. State Table (State_ID(Primary Key), State_Name,Country_Id(Foreign Key), Capital_City)

Each State will have one Capital City so i feel one more table is not necessary for Capital City

In the SelectedIndexChanged event of the Country DropDown write the below query

Select State_Id,State_Name From State Where Country_Id=ddlCountry.selectedValue ( Country dropDownList Name)


In the SelectedIndexChanged event of the State DropDown write the below query

Select Capital_City From State Where State_ID=ddlState.selectedValue ( StatedropDownList Name)


I hope this will work
dear Usharva,

with due respect I have to tell you that your table structure is not smart enough to do the required work.......its true that the whole thing is going good with your provided code....but think of a situation where you have one country with one state[though it has a fair chance of ocurrence] only and I have to DELETE that record from your State Table...then what happened......the only country ID will be deleted.....and you have to update the Country Table.....

your table structure is not Normalized....it will have Redundant data...as well as it has not met the criteria of Functional dependancy

So Normalize your database.........

I have created 4 tables after taking into consideration all of the possibilities.........
Nov 29 '07 #6
hi
i populated dropdownlisst of country by writing this code

public partial class _Default : System.Web.UI.Page
{


protected void Page_Load(object sender, EventArgs e)
{

if (Page.IsPostBack == false)
{
SqlConnection con=new SqlConnection("user id=sa;password=vubrain;database=raghu;data source=vubrain4");
SqlDataAdapter da = new SqlDataAdapter("select countryid,countryname from country", con);
DataSet ds = new DataSet();
da.Fill(ds, "country");
dd1.DataSource = ds.Tables["country"];
dd1.DataTextField = "countryname";
dd1.DataValueField = "countryid";
dd1.DataBind();

}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string capitalid;
capitalid = dd1.SelectedValue;


}
}




after this how to populate next dropdownlist and textbox
Nov 29 '07 #7
dip_developer
648 Expert 512MB
I have told you in detail...please read it carefully...you will get the answer.......otherwise I have to write the same code.....
Nov 29 '07 #8
I have told you in detail...please read it carefully...you will get the answer.......otherwise I have to write the same code.....

shell i write coding to populate dropdownlist2 in DropDownList1_SelectedIndexChanged

i mean to say where should i write sql querry
Nov 29 '07 #9
I have told you in detail...please read it carefully...you will get the answer.......otherwise I have to write the same code.....

its very urgent i been fighting for this from 3 days
Nov 29 '07 #10
dip_developer
648 Expert 512MB
shell i write coding to populate dropdownlist2 in DropDownList1_SelectedIndexChanged

i mean to say where should i write sql querry
in the DropDownList1_SelectedIndexChanged event you have to write the sql query and fetch the states......
Nov 29 '07 #11
i wrote code in dropdownlist_selectedindexchanged....

protected void dd1_SelectedIndexChanged(object sender, EventArgs e)
{
string countryid;
countryid = dd1.SelectedValue;
SqlConnection con = new SqlConnection("user id=sa;password=vubrain;database=raghu;data source=vubrain4");
SqlDataAdapter da1 = new SqlDataAdapter("SELECT State_id,statename FROM state_master S INNER JOIN state_country SC ON S.state_id=SC.stateId8 WHERE countyr_id='" + countryid + "'");
DataSet ds1 = new DataSet();
da1.Fill(ds1, "state_master");
dd2.DataSource = ds1.Tables["state_master"];
dd2.DataTextField = "statename";
dd2.DataValueField = "State_id";
dd2.DataBind();


}
protected void dd2_SelectedIndexChanged(object sender, EventArgs e)
{
string stateid;
stateid = dd2.SelectedValue;

}

but it showing two error

1.Argument '1': cannot convert from 'string'to 'System.Data.SqlClient.SqlCommand'

2.The best overloaded method match for 'System.Data.SqlClient.SqlDataAdapter.SqlDataAdapt er

please sought this problem
Nov 30 '07 #12
hi dip i am trying the same you said but i am geting the same error in the dropdownlist1_selectedindexedchanged as i earlieri mentioned you
please solve my problem
thank you
Dec 4 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: C. David Rossen | last post by:
Hello: I have a registration form for classes. Each class has a fee. I have a drop down box whereby the user chooses his class. There is a textbox with the associated fee. I would like to...
2
by: Tom Gao | last post by:
hi guys I'm wondering how do I pass a key-value pair to a drop downlist ? since I can not use hashtable as there is nor order.. and I can not use sortedlist as the order which I supply to the...
0
by: Nithin | last post by:
My code as an txt attachment. I have 2 drop down list boxes that on selection populate text boxes from my database table. I am able to display the correct values in these text boxes. I have 2...
7
by: Markus McGee | last post by:
Hi all, I have a quick question...I believe. On my web page, call it page A, I have a drop downlist with runat server enabled. When the drop downlist change event occurs it repopulates a...
4
by: Rudy | last post by:
Hello! I have 4 diffrent drop downlist. I want a user to select a value from a drop down list, and place it in a SELECT statement. How would I put that value in the select statement. And if the...
3
by: Yi Chen | last post by:
We have a drop down list on a PHP page, with several product names, and when people click one item, we will refresh the same page with the product name as parameter, and in turn we want to include...
2
by: myquestion | last post by:
how to hide/show a text box using drop downlist in php
4
by: pskvenkat | last post by:
Hi this is venkat, i have a drop downlist for country, i composed a code in javascript for that and its working in HTML page but i dont know how to call the same javascript function in asp.net(C#)...
2
by: Hugh | last post by:
The PyGUI website specified this place as the place for general discussion about it, so here goes.... First off - thanks for something that is so straightforward to get running on OSX... I've...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.