简体   繁体   中英

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

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)
END

Notice 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?

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 (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:

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.

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:

dsGroupsPerFestival.Clone()

Thanks everyone for helping me out!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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