“System.Data.DataRowView”在下拉列表中

发布于 2025-01-03 03:19:27 字数 1113 浏览 0 评论 0原文

我无法将数据绑定到下拉列表..任何人都可以解释我为什么吗? 错误是:“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

滴情不沾 2025-01-10 03:19:27

您没有在 SQL 语句中返回名为 _Deptid 的列。

您的 SQL 语句应为:

select _program.Name, _program._Deptid 
  from _program,_Department 
 where _program._Deptid = _Department._DeptId

正如 Stackoverflow 用户所提到的,通过使用 using 语句,这将自动处理您的对象。另一件事,你正在做:

if (dt.Rows.Count == 0)

但里面没有代码。无论您是否在实际代码中执行此操作,但如果您打算将其留空,建议执行以下操作:

if (dt.Rows.Count > 0)
{
    DropDownList.DataTextField = "Name";    
    DropDownList.DataValueField = "_DeptID";    
    DropDownList.DataSource = dt.DefaultView;    
    DropDownList.DataBind();
}

You aren't returning a column called _Deptid in your SQL statement.

Your SQL statement should read:

select _program.Name, _program._Deptid 
  from _program,_Department 
 where _program._Deptid = _Department._DeptId

As Stackoverflow User has mentioned, by using the using statement, this will automatically dispose of your objects. Another thing, you are doing:

if (dt.Rows.Count == 0)

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:

if (dt.Rows.Count > 0)
{
    DropDownList.DataTextField = "Name";    
    DropDownList.DataValueField = "_DeptID";    
    DropDownList.DataSource = dt.DefaultView;    
    DropDownList.DataBind();
}
若无相欠,怎会相见 2025-01-10 03:19:27
  1. select 语句中缺少 _Dept 列
  2. 缺少对象的处置。所以你可以通过两种方式执行它

    (a) 手动处置

    (b) 使用语句


public class ClassDataManagement
{
    public DataTable BindDropDownList(string Sql, DropDownList DropDownList)
    {
        using (SqlConnection cn = new SqlConnection(@"Data Source=ABID-PC;Initial Catalog=_uniManagement;Integrated Security=True"))
        {
            using (SqlCommand cmd = new SqlCommand(Sql, cn))
            {
                using(SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    using (DataTable dt = new DataTable())
                    {
                        da.Fill(dt);
                        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, _program._Deptid from _program,_Department "
        + "where _program._Deptid = _department._DeptId", DropDownListProgram);

}

EDIT - 1

以及为SqlDataAdapter添加Using语句

  1. The column _Dept is missing in your select statement
  2. Disposing of the objects is missing. So you can perform it in two ways

    (a) Manually disposing

    (b) Using Statement


public class ClassDataManagement
{
    public DataTable BindDropDownList(string Sql, DropDownList DropDownList)
    {
        using (SqlConnection cn = new SqlConnection(@"Data Source=ABID-PC;Initial Catalog=_uniManagement;Integrated Security=True"))
        {
            using (SqlCommand cmd = new SqlCommand(Sql, cn))
            {
                using(SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    using (DataTable dt = new DataTable())
                    {
                        da.Fill(dt);
                        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, _program._Deptid from _program,_Department "
        + "where _program._Deptid = _department._DeptId", DropDownListProgram);

}

EDIT - 1

Adding the Using Statement for SqlDataAdapter as well

再浓的妆也掩不了殇 2025-01-10 03:19:27

将 SQL 更改为:

select _Program.Name, _Program._DeptId from _program,_Department where _program._Deptid = _department._DeptId

Change your SQL to:

select _Program.Name, _Program._DeptId from _program,_Department where _program._Deptid = _department._DeptId
梦途 2025-01-10 03:19:27

尝试将您的代码修改为此,您缺少 _DeptID

   protected void Page_Load(object sender, EventArgs e)
    {
        ClassDataManagement dm = new ClassDataManagement();
        dm.BindDropDownList("select _Program.Name,_DeptID from _program,_Department where _program._Deptid = _department._DeptId", DropDownListProgram);

    }

Try modifing your code to this, you are missing _DeptID

   protected void Page_Load(object sender, EventArgs e)
    {
        ClassDataManagement dm = new ClassDataManagement();
        dm.BindDropDownList("select _Program.Name,_DeptID from _program,_Department where _program._Deptid = _department._DeptId", DropDownListProgram);

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文