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:
string regionvar = "";
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;
}
}
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 (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
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:
<asp:DropDownList ID="DropDownListRegion" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownListRegion_SelectedIndexChanged">
</asp:DropDownList>
protected void DropDownListRegion_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownListCity.Enabled = true;
regionvar = DropDownListCity.SelectedItem.Text;
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;