3
votes

I have this ASP.NET code:

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

My DAL code:

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

When the form loads I get this error:

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

Is it my optional parameter that is causing the problem? How can I workaround this? Is it possible to have optional parameters with declarative ASP.NET code?

2
add methods public DataTable GetBrokers() { return GetBrokers(null); } and check is it works? - Andrei Andrushkevich
@galimy: that worked great! Maybe send this comment as an answer, so I can accept it? - Mark Allison

2 Answers

3
votes

add method:

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

and check is it works?

0
votes

You can use SelectParameters like this:

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