Help on dropdown list [Resolved]

Posted by Vijayar under ASP.NET on 5/12/2011 | Points: 10 | Views : 1385 | Status : [Member] | Replies : 13
Hi
I have one dropdown list box,i am binding values from the database from 2 tables states and cities ,in this format
Country1 ex: India

state1 AP
state2 Karnataka
Country2 USA
state1 Mexico
state2
When i select one state for a country i.e Mexico for Usa in dropdown i should get Usa-Mexico .i.e Country name and statename .please help me in this issue it's very urgent.

vijaya


Responses

Posted by: Ndebata on: 5/12/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi
Try to set AutoPostBack="true" for dropdownlist ,then register OnSelectedIndexChanged event.
in code behind

void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ListItem cntry=new ListItem();
string _resultformat=null;
if (DropDownList1.SelectedItem != null)
{
foreach (ListItem item in DropDownList1.Items)
{
if (item.Value.StartsWith("C", StringComparison.OrdinalIgnoreCase))
cntry = item;
else if (item.Value.Equals(DropDownList1.SelectedItem.Value) && item.Value.StartsWith("S", StringComparison.OrdinalIgnoreCase))
{
_resultformat=string.Format("{0}-{1}", cntry.Text, DropDownList1.SelectedItem.Text);
break;
}
}
}
}

The other way you can go to database and get both country and state for the selected state. :)
Thanks,
Debata

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijayar on: 5/16/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
Please help me in getting the state id and country id for the state id for the selected state through query and inserting those id's in to a table .it's very urgent

vijaya

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 5/16/2011 [Member] Starter | Points: 25

Up
0
Down
Hi

From the DropDownList1.SelectedItem.Value you have to remove the "S" , then you will get the StateID , which you need to pass to to the database to get the country id.
Declare @CountryID int
Select @CountryID =CountryID from tblState where StateID=@StateID (which is passed from asp.net application)
Now use can insert/save this @SateID and @CountryID to any table you want.

Thanks,
Debata



Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijayar on: 5/16/2011 [Member] Starter | Points: 25

Up
0
Down
SqlDataAdapter da = new SqlDataAdapter(" Select stateid from states where statename='" + DropDownList1.SelectedItem.Text + "'", conn);
DataSet ds = new DataSet();
da.Fill(ds);
int stateid = Convert.ToInt32(ds.Tables[0].Rows[0]["stateid"]);

SqlDataAdapter da1 = new SqlDataAdapter(" Select countryid from states where statename='" + DropDownList1.SelectedItem.Text + "'", conn);
DataSet ds1 = new DataSet();
da.Fill(ds1);
int countryid = Convert.ToInt32(ds.Tables[0].Rows[0]["CountryId"]);
Is this correct way

But i am ,getting Exception as Column 'CountryId' does not belong to Table.When i write the same query in database i am getting country id what is the mistake.please Tell me


vijaya

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijayar on: 5/16/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
I have one dropdown list box,i am binding values from the database from 2 tables states and cities ,in this format

When i select one state for a country i.e Mexico for Usa in dropdown i should get Usa-Mexico .i.e Country name and statename in dropdown but not in a label and insert their id's in to table .please help me in this issue
vijaya

vijaya

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 5/16/2011 [Member] Starter | Points: 25

Up
0
Down
C# is case sensitive.
in your select query you are selecting "countryid" but from datatable you are specifing column name as "CountryId".
Thats why you are getting error, change it first , it will work.

You can use a single Statement instead of two.
Thanks,
Debata

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijayar on: 5/16/2011 [Member] Starter | Points: 25

Up
0
Down

Hi
I Have checked with the database ,but not working,in single statement,can we write in this way

SqlDataAdapter da1 = new SqlDataAdapter(" Select countryid,stateid from states where statename='" + DropDownList1.SelectedItem.Text + "'", conn);
DataSet ds1 = new DataSet();
da.Fill(ds1);
int countryid = Convert.ToInt32(ds.Tables[0].Rows[0]["CountryId"]);
int stateid = Convert.ToInt32(ds.Tables[0].Rows[0]["StateId"]);

vijaya

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 5/16/2011 [Member] Starter | Points: 25

Up
0
Down
@Please change the column name as highlighted bellow, As i could see the Select list Name are in lower case and the column you try to access is different.

Hi
I Have checked with the database ,but not working,in single statement,can we write in this way

SqlDataAdapter da1 = new SqlDataAdapter(" Select countryid,stateid from states where statename='" + DropDownList1.SelectedItem.Text + "'", conn);
DataSet ds1 = new DataSet();
da.Fill(ds1);
int countryid = Convert.ToInt32(ds.Tables[0].Rows[0]["countryid
"]);
int stateid = Convert.ToInt32(ds.Tables[0].Rows[0]["stateid
"]);

vijaya


Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijayar on: 5/16/2011 [Member] Starter | Points: 25

Up
0
Down
Hi

I have one dropdown list box,i am binding values from the database from 2 tables states and cities ,in this format
Country1 ex: India

state1 AP
state2 Karnataka
Country2 USA
state1 Mexico
state2
When i select one state for a country i.e Mexico for Usa in dropdown i should get Usa-Mexico .i.e Country name and statename .please help me in this issue it's very urgent..Instead of showing combination of country and state in label
can we display it i n dropdown itself,then how to get state d and country id
vijaya

vijaya

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijayar on: 5/16/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
i am getting the country id and state id also

vijaya

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijayar on: 5/16/2011 [Member] Starter | Points: 25

Up
0
Down
HI


Thanks alot for ur suggestions


vijaya

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijayar on: 5/18/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
I have one dropdown list box,i am binding values from the database from 2 tables states and cities ,in this format

When i select one state for a country i.e Mexico for Usa in dropdown i should get Usa-Mexico .i.e Country name and statename in dropdown but not in a label and insert their id's in to table .please help me in this issue.when i display the combination of state and country in label i am able to get their id's.But when i dispaly their combination in drop down ,i am getting the valueof DropDownList1.SelectedItem as Usa-Mexico ,so that i am unable their id's .Help me in this issue to get their id's
vijaya

vijaya

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijayar on: 5/18/2011 [Member] Starter | Points: 25

Up
0
Down
Hi


Is there any other way to the same task .If u know please help me with an attachment

vijaya

Vijayar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response