简体   繁体   English

DropDownList.DataTextField不能绑定到数据集?

[英]DropDownList.DataTextField can't be bound to a dataset?

I'm trying to bind a dataset to a dropdownlist , which throws a HttpException 我正在尝试将dataset绑定到dropdownlist ,该dropdownlist会抛出HttpException

Here's what I'm trying to do 这就是我想要做的

if (dsGroupsNotOnFestival.Tables[0].Rows.Count > 0)
{
    //Bind dataset to new group selection
    ddlNewGroup.DataSource = dsGroupsNotOnFestival.Tables[0].DefaultView;
    ddlNewGroup.DataValueField = "band_id";
    ddlNewGroup.DataTextField = "band_naam";
    ddlNewGroup.DataBind();
}
else
{
    ddlNewGroup.Items.Add(new ListItem("No groups to add"));
}

The exception message: 异常消息:

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'band_naam'.

I'm absolutely positive that the dataset contains a column with the title band_naam. 我绝对肯定dataset包含一个标题为band_naam的列。 As you can see, the exception is thrown when binding the DataTextField , meaning that the DataValueField is bound correctly, right? 如您所见,绑定DataTextField时抛出异常,这意味着DataValueField绑定正确,对吧?

EDIT: 编辑:

This is the stored procedure: 这是存储过程:

USE [groep2_festivals]
GO
/****** Object:  StoredProcedure [dbo].[GetGroupsOfFestival]    Script Date: 14-05-13 12:31:18 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      Robbie Vercammen
-- Create date: 2013-05-09
-- Description: Getting all groups for a defined festival
-- =============================================
ALTER PROCEDURE [dbo].[GetGroupsOfFestival]
    (
    @festId  nvarchar(4)
    )
AS
BEGIN
    DECLARE @query varchar(255)

    SELECT b.*, p.* 
    FROM bands b 
    JOIN bandsperfestival bpf ON b.band_id = bpf.band_id 
    JOIN podia p ON p.pod_id = bpf.pod_id WHERE fest_id LIKE @festId
    EXEC(@query)
END

Notice the b.* and bands b ? 注意b.*bands b Now here that table: 现在这个表:

在此输入图像描述

This stored procedure has been used on multiple occasions, so I'm pretty sure that it works correctly. 这个存储过程已经多次使用,所以我很确定它能正常工作。 But here's perhaps the most important code that I forgot to mention: 但这里可能是我忘记提及的最重要的代码:

//Filling dataset with groups not on this festival
dsGroupsNotOnFestival.Tables.Add(new DataTable());
foreach (DataRow row in dsGroupsAll.Tables[0].Rows)
{
    if (!dsGroupsPerFestival.Tables[0].Rows.Equals(row))
    {
        dsGroupsNotOnFestival.Tables[0].ImportRow(row);
    }
}

And once again, the DataSet dsGroupsAll has been used before so the data is present :) 再一次, DataSet dsGroupsAll之前已经使用过,所以数据存在:)

@manish mishra dsGroupsAll is filled with the same stored procedure above. @manish mishra dsGroupsAll充满了上面相同的存储过程。 To prove that there is actually data in there: 为了证明那里有实际的数据:

在此输入图像描述

For those still following, I've confirmed that there are 14 rows in dsGroupsNotOnFestival . 对于那些仍然关注的人,我已经确认dsGroupsNotOnFestival有14行。 When i'm trying to get a value in a simple way, for example: 当我试图以简单的方式获取值时,例如:

String strResult = dsGroupsNotOnFestival.Tables[0].Rows[0][0].ToString();

I get an exception saying that column 0 could not be found... why is that? 我得到一个例外,说找不到第0列......为什么会这样?

I'm absolutely positive that the dataset contains a column with the title band_naam. 我绝对肯定数据集包含一个标题为band_naam的列。

I understand how you feel, but I'm absolutely positive that there isn't one because that error is extremely explicit: 我理解你的感受,但我绝对肯定没有一个,因为这个错误是非常明确的:

does not contain a property with the name 'band_naam' 不包含名为'band_naam'的属性

Have another look at the code that fills that DataSet . 再看一下填充DataSet的代码。 The DefaultView is a perfect image of the DataTable , which of course is the result set of your query, so the query you're using to fill this DataTable is missing this field. DefaultViewDataTable的完美图像,当然它是查询的结果集,因此您用来填充此DataTable的查询缺少此字段。

Maybe it's a calculated field and you forgot the AS keyword to give it a column name. 也许它是一个计算字段,你忘了AS关键字给它一个列名。


EDIT 编辑

Based on your feedback (ie screenshots of the queries and schema), I'm going to say Habib probably hit it right on the head in the comments, change this line: 根据您的反馈(即查询和架构的屏幕截图),我要说Habib可能会在评论中直接点击它,更改此行:

ddlNewGroup.DataSource = dsGroupsNotOnFestival.Tables[0].DefaultView;

to this line: 到这一行:

ddlNewGroup.DataSource = dsGroupsNotOnFestival.Tables[0];

I personally have never had a problem binding to a DefaultView but I don't feel there are any other possibilities left. 我个人从未遇到过绑定到DefaultView的问题,但我觉得还没有其他可能性。

Not sure if I am allowed to approve my own answers, but I found the solution to my nightmare. 不确定我是否被允许批准我自己的答案,但我找到了解决我梦魇的方法。

//Filling dataset with groups not on this festival
dsGroupsNotOnFestival = dsGroupsPerFestival.Clone();
foreach (DataRow row in dsGroupsAll.Tables[0].Rows)
{
    if (!dsGroupsPerFestival.Tables[0].Rows.Equals(row))
    {
        dsGroupsNotOnFestival.Tables[0].ImportRow(row);
    }
}

The difference is, I'm cloning the original DataSet preserving the structure, constraints, etc using: 区别在于,我正在使用以下方法克隆原始DataSet保留结构,约束等:

dsGroupsPerFestival.Clone()

Thanks everyone for helping me out! 谢谢大家帮帮我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM