gpt4 book ai didi

c# - 将两列绑定(bind)到下拉列表中

转载 作者:行者123 更新时间:2023-11-30 12:12:25 24 4
gpt4 key购买 nike

我想将数据库中的两列绑定(bind)到下拉列表中。这是我的代码:

SqlConnection con=new SqlConnection(CommonRefference.Constr());
string query = "Select Id,Name+' ('+Distribution_name+') 'as Name1 from BR_supervisor ";

SqlCommand cmd = new SqlCommand(query,con);
con.Open();

SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
DropDownList3.DataSource = query;
DropDownList3.DataTextField = "name1";
DropDownList3.DataValueField = "Id";
DropDownList3.DataBind();
}
con.Close();

但是报错如下

DataBinding: 'System.Char' does not contain a property with the name 'name1'

怎么做?非常感谢任何人帮助我

最佳答案

它的 Name1 不是 name1

将此更改为

DropDownList3.DataTextField = "Name1";

编辑:

您正在将字符串(查询)绑定(bind)到此处的数据源

DropDownList3.DataSource = query;

字符串没有名为“name1”的属性,所以出错

解决方案:

引用这个,我已经根据您的要求更改了这个

private void LoadDropDownList()
{
SqlConnection con=new SqlConnection(CommonRefference.Constr());
string query = "Select Id,Name+' ('+Distribution_name+') 'as Name1 from BR_supervisor ";

SqlCommand cmd = new SqlCommand(query,con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();

DropDownList3.DataSource = reader ;

DropDownList3.DataValueField = "Id";
DropDownList3.DataTextField = "Name1";

DropDownList3.DataBind();

DropDownList3.Items.Insert(0, new ListItem("Select One", "0")); // Adds items to the DDL
DropDownList3.Items.Insert(1, new ListItem("All Categories", "-1"));

con.Close();
}

有关更多说明,请参阅此链接 Populate-ASP.NET-dropdownlist

关于c# - 将两列绑定(bind)到下拉列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13564190/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com