“System.Data.DataRowView”在下拉列表中
我无法将数据绑定到下拉列表
..任何人都可以解释我为什么吗? 错误是:“System.Data.DataRowView”不包含名称为“_DeptID”的属性。
我的代码是:
public class ClassDataManagement
{
public DataTable BindDropDownList(string Sql, DropDownList DropDownList)
{
SqlConnection cn = new SqlConnection(@"Data Source=ABID-PC;Initial Catalog=_uniManagement;Integrated Security=True");
SqlCommand cmd = new SqlCommand(Sql, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count == 0)
{
}
else
{
DropDownList.DataTextField = "Name";
DropDownList.DataValueField = "_DeptID";
DropDownList.DataSource = dt.DefaultView;
DropDownList.DataBind();
}
return dt;
}
}
protected void Page_Load(object sender, EventArgs e)
{
ClassDataManagement dm = new ClassDataManagement();
dm.BindDropDownList("select _Program.Name from _program,_Department where _program._Deptid = _department._DeptId", DropDownListProgram);
}
I cannot bind data to a Dropdown List
.. Can any one explain me why?
Error is : 'System.Data.DataRowView' does not contain a property with the name '_DeptID'.
my code is:
public class ClassDataManagement
{
public DataTable BindDropDownList(string Sql, DropDownList DropDownList)
{
SqlConnection cn = new SqlConnection(@"Data Source=ABID-PC;Initial Catalog=_uniManagement;Integrated Security=True");
SqlCommand cmd = new SqlCommand(Sql, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count == 0)
{
}
else
{
DropDownList.DataTextField = "Name";
DropDownList.DataValueField = "_DeptID";
DropDownList.DataSource = dt.DefaultView;
DropDownList.DataBind();
}
return dt;
}
}
protected void Page_Load(object sender, EventArgs e)
{
ClassDataManagement dm = new ClassDataManagement();
dm.BindDropDownList("select _Program.Name from _program,_Department where _program._Deptid = _department._DeptId", DropDownListProgram);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您没有在 SQL 语句中返回名为
_Deptid
的列。您的 SQL 语句应为:
正如 Stackoverflow 用户所提到的,通过使用
using
语句,这将自动处理您的对象。另一件事,你正在做:但里面没有代码。无论您是否在实际代码中执行此操作,但如果您打算将其留空,建议执行以下操作:
You aren't returning a column called
_Deptid
in your SQL statement.Your SQL statement should read:
As Stackoverflow User has mentioned, by using the
using
statement, this will automatically dispose of your objects. Another thing, you are doing:but you have no code within it. Whether you do in your actual code or not, but if you are intending on leaving it blank, it would be advisable to do:
缺少对象的处置。所以你可以通过两种方式执行它
(a) 手动处置
(b) 使用语句
EDIT - 1
以及为SqlDataAdapter添加Using语句
Disposing of the objects is missing. So you can perform it in two ways
(a) Manually disposing
(b) Using Statement
EDIT - 1
Adding the Using Statement for SqlDataAdapter as well
将 SQL 更改为:
Change your SQL to:
尝试将您的代码修改为此,您缺少
_DeptID
Try modifing your code to this, you are missing
_DeptID