0

I have the following code:

<asp:DropDownList ID="ddlStudyYear" runat="server" SelectedValue='<%# Bind("StudyYearID") %>' DataSourceID="sdsStudyYears" DataTextField="StudyYearID" DataValueField="StudyYearID"></asp:DropDownList>
<asp:SqlDataSource ID="sdsStudyYears" runat="server" ConnectionString="<%$ ConnectionStrings:ATCNTV1ConnectionString %>" SelectCommand="SELECT StudyYearID FROM tblStudyYears ORDER BY StudyYearID"></asp:SqlDataSource>

But it does not work because the Bind("StudyYearID") comes from another SqlDataSource further down the code, which returns a VARCHAR(3). However, the datasource above (sdsStudyYears) returns a value as a CHAR(3).

So when I run this, I get the following error:

'ddlStudyYear' has a SelectedValue which is invalid because it does
not exist in the list of items.

I was not able to find it on the net either, but there must be a way in the Bind() function to tell it to CAST the varchar to char. Or perhaps a function surrounding the SelectedValue property to do the casting?

Any help appreciated.

Thank you

4

1 回答 1

0

找到解决方案!

我从查询的角度解决了它。

我将查询更改为...

SELECT RTRIM(CAST(CASE WHEN StudyYearID = '0' THEN ' ' ELSE StudyYearID END AS VARCHAR(3))) AS StudyYearID FROM tblStudyYears

我知道 CAST() 是多余的,但 RTRIM() 是这样做的,但强制返回值与 gridview 数据源中的 VARCHAR() 一样,现在两者都比较完美。

很高兴知道我是如何完全从 bind() 函数中做到这一点的。

于 2013-05-08T01:28:04.943 回答