ObjectDataSource找不到非泛型方法

时间:2010-12-20 15:03:02

标签: c# asp.net c#-4.0

我有这个ASP.NET代码:

<asp:DropDownList 
    ID="ddlBrokers" 
    runat="server" 
    AutoPostBack="true" 
    DataSourceID="srcBrokers" 
    DataTextField="broker" 
    DataValueField="brokerId"
/>

<asp:ObjectDataSource
    id="srcBrokers" 
    TypeName="DatabaseComponent.DBUtil" 
    SelectMethod="GetBrokers" 
    runat="server">
</asp:ObjectDataSource>

我的DAL代码:

public DataTable GetBrokers(bool? hasImport=null)
{
    SqlCommand cmd = new SqlCommand("usp_GetBrokers");
    if (hasImport.HasValue)
        cmd.Parameters.AddWithValue("@hasImport", hasImport);
    return FillDataTable(cmd, "brokers");
}

当表单加载时,我收到此错误:

ObjectDataSource 'srcBrokers' could not find a non-generic method 'GetBrokers' that has no parameters.

是我的可选参数导致问题吗?我该如何解决这个问题?是否可以使用声明性ASP.NET代码获取可选参数?

2 个答案:

答案 0 :(得分:3)

添加方法:

public DataTable GetBrokers() { 
              return GetBrokers(null);
}

并检查它是否有效?

答案 1 :(得分:0)

您可以像这样使用SelectParameters

<asp:ObjectDataSource id="srcBrokers" TypeName="DatabaseComponent.DBUtil" SelectMethod="GetBrokers" runat="server">
    <SelectParameters>
    <asp:Parameter Name="hasImport" Type="Empty" />
    <SelectParameters>
</asp:ObjectDataSource>