Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm a beginner so please bear with me...

I already have a table with these columns
-ID (for primary key purposes)
-region (equivalent to a state in other countries)
-city (within the region or state)

What I want to happen is this, whenever I change DropDownListRegion, I want to have the DropDownListCity populated with only the cities inside that region.

Here's my code:
C#
string regionvar = ""; //to be used on ***

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                
            }
            else
            {
                selectData();
            }
        }

        protected void selectData()
        {
            string myConnectionString = "server=localhost;uid=root;" + "pwd=xxxxx;database=xxxxx;";
            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);

            DataTable dt = new DataTable();

            string commandText = "select * from table";

            using (MySqlConnection openconn = conn)
            using (MySqlDataAdapter adap = new MySqlDataAdapter(commandText, openconn))
            {
                adap.Fill(dt);
                GridViewUserInfoTable.DataSource = dt;
                GridViewUserInfoTable.DataBind();
            }

            MySql.Data.MySqlClient.MySqlConnection conn1 = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);

            DataTable dt1 = new DataTable();

            string commandText1 = "select distinct region from REGIONS order by ID";

            using (MySqlConnection openconn1 = conn1)
            using (MySqlDataAdapter adap1 = new MySqlDataAdapter(commandText1, openconn1))
            {
                adap1.Fill(dt1);
                if (dt1.Rows.Count > 0)
                {
                    DropDownListRegion.DataSource = dt1;
                    DropDownListRegion.DataTextField = "region";
                    DropDownListRegion.DataValueField = "region";
                    DropDownListRegion.DataBind();
                    regionvar = DropDownListRegion.SelectedItem.Text; //setting value for ***
                }
                
            }

            MySql.Data.MySqlClient.MySqlConnection conn2 = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);

            DataTable dt2 = new DataTable();

            string commandText2 = "select city from REGIONS order by city where region = " + regionvar; //using value of ***

            using (MySqlConnection openconn2 = conn2)
            using (MySqlDataAdapter adap2 = new MySqlDataAdapter(commandText2, openconn2))
            {
                adap2.Fill(dt2);
                if (dt2.Rows.Count > 0)
                {
                    DropDownListCity.DataSource = dt2;
                    DropDownListCity.DataTextField = "city";
                    DropDownListCity.DataValueField = "city";
                    DropDownListCity.DataBind();
                }
            }
            
        }


I'm getting the error somewhere in where region = "RegionName"

Any suggestions will be highly appreciated. Thank you very much!

---
After solution number1:
okay, so i did this
C#
string commandText2 = "select city from REGIONS where region=" + regionvar + " order by city";

a new error appears:
Unknown column 'REGIONNAME' in 'where clause'

---
After solution number 2 and 3:
so I made this:
XML
<asp:DropDownList ID="DropDownListRegion" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownListRegion_SelectedIndexChanged">
        </asp:DropDownList>


C#
protected void DropDownListRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownListCity.Enabled = true;
            regionvar = DropDownListCity.SelectedItem.Text; //ERROR HERE
            
            string myConnectionString = "server=localhost;uid=root;" + "pwd=XXXXX;database=XXXXX;";
            MySql.Data.MySqlClient.MySqlConnection conn2 = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);

            DataTable dt2 = new DataTable();

            string commandText2 = "select city from REGIONS where region=" + regionvar + " order by city";

            using (MySqlConnection openconn2 = conn2)
            using (MySqlDataAdapter adap2 = new MySqlDataAdapter(commandText2, openconn2))
            {
                adap2.Fill(dt2);
                if (dt2.Rows.Count > 0)
                {
                    DropDownListCity.DataSource = dt2;
                    DropDownListCity.DataTextField = "city";
                    DropDownListCity.DataValueField = "city";
                    DropDownListCity.DataBind();
                }
            }

            if (Page.IsPostBack == true)
            {

            }
        }

I get an error on this line saying: Object reference not set to an instance of an object.
regionvar = DropDownListCity.SelectedItem.Text;
Posted
Updated 5-Mar-14 1:53am
v4
Comments
Ramug10 5-Mar-14 7:09am    
are you getting value for regionvar did you check with break point?
Raajkumar.b 5-Mar-14 7:12am    
refer these links once u get an idea
http://www.aspsnippets.com/Green/Articles/Cascading-DropDownList-for-CountryStateCity-in-ASPNet.aspx

http://www.c-sharpcorner.com/uploadfile/rohatash/dropdownlist-with-country-state-and-city-in-asp-net/

Try below
protected void DropDownListRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownListCity.Enabled = true;
            regionvar = DropDownListRegion.SelectedItem.Text; //ERROR HERE
            
            string myConnectionString = "server=localhost;uid=root;" + "pwd=XXXXX;database=XXXXX;";
            MySql.Data.MySqlClient.MySqlConnection conn2 = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);
 
            DataTable dt2 = new DataTable();
 
            string commandText2 = "select city from REGIONS where region='" + regionvar + "' order by city";
 
            using (MySqlConnection openconn2 = conn2)
            using (MySqlDataAdapter adap2 = new MySqlDataAdapter(commandText2, openconn2))
            {
                adap2.Fill(dt2);
                if (dt2.Rows.Count &gt; 0)
                {
                    DropDownListCity.DataSource = dt2;
                    DropDownListCity.DataTextField = "city";
                    DropDownListCity.DataValueField = "city";
                    DropDownListCity.DataBind();
                }
            }
 
            if (Page.IsPostBack == true)
            {
 
            }
        }
 
Share this answer
 
Comments
[no name] 6-Mar-14 2:00am    
"select city from REGIONS where region='" + regionvar + "' order by city";
this line solve my problem... Many Thanks...
although... (dt2.Rows.Count > 0)... > i think was a typo or something...
Ramug10 6-Mar-14 2:08am    
You most welcome :)
yaa it is > only.

-RG
C#
ORDER BY clause is always followed after the WHERE clause.... See below code for reference


use this 

string commandText2 = "select city from REGIONS where region = " + regionvar + " order by city"; //using value of ***


insted of

string commandText2 = "select city from REGIONS order by city where region = " + regionvar; //using value of ***
 
Share this answer
 
v2
Comments
Ramug10 5-Mar-14 7:13am    
regionvar is a string so you need to put it in single codes.
[no name] 5-Mar-14 7:22am    
what do yo mean? can you please elaborate? Thanks!
Ramug10 5-Mar-14 7:27am    
is the above statement working for you?
PJ003 5-Mar-14 7:25am    
Yes, we can but by using nested query... here is an example for nested query
SELECT *
FROM (
SELECT *
FROM myTable
ORDER BY TOP10) T
WHERE ROWNUM <=10
Make auto postback property of DropDownListRegion true & use selected index changed event. So that when you will change index of region dropdown it will call server side event(Selected index changed). In this event code for getting selected value of region dropdown. After getting this value retrive related data from database .
 
Share this answer
 
Here Immediate binding of region dropdown, you comparing the city with region, but you don't select any region since you just binded the region dropdown.

what you need to do is, just split your code. Keep your code up to region dropdown binding, after that cut the code and paste the code in region dropdown SelectedIndexChange event. There you need to get the selected region and pass it to query. make sure that autopostback of region dropdown is true.


Thanks,
-RG
 
Share this answer
 
first you bind Regiondropdown and then Citydropdown in page load.Bind CityDrop down again in RegionDropdown_selected_ index changed event. Auto post back of Regionddl =true
 
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