Search code examples
c#asp.nettelerikobjectdatasource

Control always getting NullReference in ObjectDataSource


I am trying to use ObjectDataSource for the first time with my code but in its SelectMethod I am always getting the Control as null.

ObjectDataSource

<asp:ObjectDataSource ID="objDataSourceStartAddress" runat="server" SelectMethod="GetSearchStartAddress" TypeName="TransElite.MainApplication.Booking" >
</asp:ObjectDataSource>

Control using ObjectDataSource

<telerik:RadSearchBox runat="server" ID="radtxtSearchStartAddress" EmptyMessage="Search Resolved Address" MinFilterLength="5" OnSearch="radtxtSearchStartAddress_Search" DataTextField="DisplayAddress" DataValueField="Id" DataSourceID="objDataSourceStartAddress" Width="85%">
</telerik:RadSearchBox>

SelectMethod of ObjectDataSource

public List<AddressData> GetSearchStartAddress()
{
    // Assigning collection/list to StartSearchAddress 
    var StartSearchAddress = ClientDataProvider.AddressList(radtxtSearchStartAddress.Text);

    return StartSearchAddress;
}

Kindly suggest how to use the control in SelectMethod.


Solution

  • Thanks guys for your efforts. But i figured out the code which resolved issue with my code. What is concluded is you cannot use control with the method used in SelectMethod. So the other way used is using SelectParameters as following :

    <asp:ObjectDataSource ID="objDataSourceStartAddress" runat="server" SelectMethod="GetSearchStartAddress" TypeName="TransElite.MainApplication.Booking">
            <SelectParameters>
                <asp:ControlParameter Name="startaddress" ControlID="radtxtSearchStartAddress" PropertyName="Text" DefaultValue="BLANKTEXT" />
            </SelectParameters>
        </asp:ObjectDataSource>
    

    and code behind will have following code :

    public List<AddressData> GetSearchStartAddress(string startaddress)
        {
            if (startaddress.Trim() == "BLANKTEXT")
                return null;
    
    
            var StartSearchAddress = ClientDataProvider.AddressListPanelCallback(startaddress);
    
            return StartSearchAddress;
    
        }