我正在尝试将dataset绑定到dropdownlist ,该dropdownlist会抛出HttpException
这就是我想要做的
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")); }异常消息:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'band_naam'.我绝对肯定dataset包含一个标题为band_naam的列。 如您所见,绑定DataTextField时抛出异常,这意味着DataValueField绑定正确,对吧?
编辑:
这是存储过程:
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注意b.*和bands b ? 现在这个表:
这个存储过程已经多次使用,所以我很确定它能正常工作。 但这里可能是我忘记提及的最重要的代码:
//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); } }再一次, DataSet dsGroupsAll之前已经使用过,所以数据存在:)
@manish mishra dsGroupsAll充满了上面相同的存储过程。 为了证明那里有实际的数据:
对于那些仍然关注的人,我已经确认dsGroupsNotOnFestival有14行。 当我试图以一种简单的方式获取值时,例如:
String strResult = dsGroupsNotOnFestival.Tables[0].Rows[0][0].ToString();我得到一个例外,说无法找到第0列......为什么会这样?
I'm trying to bind a dataset to a dropdownlist, which throws a 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. As you can see, the exception is thrown when binding the DataTextField, meaning that the DataValueField is bound correctly, right?
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) ENDNotice the b.* and 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 :)
@manish mishra dsGroupsAll is filled with the same stored procedure above. To prove that there is actually data in there:
For those still following, I've confirmed that there are 14 rows in dsGroupsNotOnFestival. 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?
最满意答案
我绝对肯定数据集包含一个标题为band_naam的列。
我理解你的感受,但我绝对肯定没有一个,因为这个错误是非常明确的:
不包含名为'band_naam'的属性
再看一下填充DataSet的代码。 DefaultView是DataTable的完美图像,当然它是查询的结果集,因此您用来填充此DataTable的查询缺少此字段。
也许它是一个计算字段,你忘了AS关键字给它一个列名。
编辑
根据您的反馈(即查询和架构的屏幕截图),我会说Habib可能会在评论中直接点击它,更改此行:
ddlNewGroup.DataSource = dsGroupsNotOnFestival.Tables[0].DefaultView;到这一行:
ddlNewGroup.DataSource = dsGroupsNotOnFestival.Tables[0];我个人从未遇到过绑定到DefaultView的问题,但我觉得还没有任何其他可能性。
I'm absolutely positive that the dataset contains a column with the title 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'
Have another look at the code that fills that 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.
Maybe it's a calculated field and you forgot the AS keyword to give it a column name.
EDIT
Based on your feedback (i.e. 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:
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.
更多推荐
发布评论