Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 将数据库数据绑定到dropdownlist_Asp.net_Sql_Linq_Drop Down Menu - Fatal编程技术网

Asp.net 将数据库数据绑定到dropdownlist

Asp.net 将数据库数据绑定到dropdownlist,asp.net,sql,linq,drop-down-menu,Asp.net,Sql,Linq,Drop Down Menu,我已经尝试了很长一段时间来修复我在项目中遇到的DropDownList错误。目前,我正在使用此数据获取数据,它运行良好: using (InfoEntities ie = new InfoEntities()) { var sqlddl = (from query in ie.Table_Customers from q in ie.Accounting_PriceType

我已经尝试了很长一段时间来修复我在项目中遇到的DropDownList错误。目前,我正在使用此数据获取数据,它运行良好:

 using (InfoEntities ie = new InfoEntities())
        {
            var sqlddl = (from query in ie.Table_Customers
                          from q in ie.Accounting_PriceType
                          where query.TypeID == 1 && q.ID == 1
                          orderby query.Customers
                          select query).Distinct().ToList();
            DropDownListCust.DataTextField = "Customers";
            DropDownListCust.DataValueField = "ID";
            DropDownListCust.DataSource = sqlddl;
            DropDownListCust.DataBind();
        }
现在,当用户保存数据并再次打开网站时,我需要先前检索的dropdownlist上选择的保存值。这也很好,但问题是我得到了重复。不管怎样,我是这样做的,我很确定我做错了:

在页面加载中,我加载我的dropdownlist以获取所有项目,再加上以下内容以获取保存的值:

 DropDownListCust.SelectedItem.Text = sql.Customers;
这使得我的DDL非常有缺陷,有些项目不相似,有时还会重复值。有人能帮忙吗?我正在使用LINQ,但我可以使用其他一些方法,只要它是固定的


干杯

我这样解决了我的问题:

DropDownList1.ClearSelection();
DropDownList1.Items.FindByText(sql.Customer).Selected = true;