如何在DropDownList上修复错误? [英] How do I fix my error on my DropDownList?

查看:74
本文介绍了如何在DropDownList上修复错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者所以请耐心等待...



我已经有了这些栏目的桌子

-ID (用于主要用途)

-region(相当于其他国家的州)

-city(在该地区或州内)



我想要发生的是,每当我更改DropDownListRegion时,我想让DropDownListCity只填充该区域内的城市。



这是我的代码:

  string  regionvar =   ;  //  将用于***  

protected void Page_Load( object sender,EventArgs e)
{
if (Page.IsPostBack)
{

}
else
{
selectData();
}
}

受保护 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;

使用(MySqlConnection openconn = conn)
使用(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 = 按ID;从REGIONS顺序中选择不同的区域;

使用(MySqlConnection openconn1 = conn1)
使用(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 = 从REGIONS按城市顺序选择城市,其中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();
}
}

}





我在某处遇到错误where region =RegionName



任何建议都将受到高度赞赏。非常感谢!



---

解决方案编号1之后:

没关系,所以我这样做了

  string  commandText2 =  从REGIONS中选择city = + regionvar +   order by city; 



a出现新错误:

'where子句'中的未知列'REGIONNAME'



---

解决方案编号2和3之后:

所以我这样做了:

< pre lang =xml> < 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; // 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 = 从REGIONS中选择城市,其中region = + regionvar + 按城市排序;

使用(MySqlConnection openconn2 = conn2)
使用(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();
}
}

如果(Page.IsPostBack == true
{

}
}



我在这一行收到错误说:对象引用未设置为对象的实例。

regionvar = DropDownListCity.SelectedItem.Text;

解决方案

尝试以下

 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 =从REGIONS中选择城市,其中region ='+ regionvar +'按城市排序;

使用(MySqlConnection openconn2 = conn2)
使用(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)
{

}
}


 ORDER BY子句 总是在WHERE子句后跟随....请参阅以下代码 参考


使用

string commandText2 = 从中选择城市REGIONS region = + regionvar + 按城市排序; // 使用***值


insted of

string commandText2 = 从REGIONS按城市顺序选择城市,其中region = + regionvar; // 使用***值

使DropDownListRegion的自动回发属性为true并使用选定的索引更改事件。因此,当您更改区域下拉列表的索引时,它将调用服务器端事件(已更改选定的索引)。在此事件代码中获取区域下拉列表的选定值。获取此值后,从数据库中检索相关数据。


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 = ""; //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

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; //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;

解决方案

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)
            {
 
            }
        }


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 ***


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 .


这篇关于如何在DropDownList上修复错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆