Search code examples
c#asp.netsqldatasource

ASP.NET SqlDataSource.Select() not selecting


I have the following SqlDataSource:

<asp:SqlDataSource ID="DataSourcePFEP" runat="server" OnSelecting="DataSourcePFEP_Selecting" OnSelected="DataSourcePFEP_Selected"
ConnectionString="<%$ ConnectionStrings:DALI %>" ProviderName="Oracle.ManagedDataAccess.Client" CancelSelectOnNullParameter="false">
    <SelectParameters>
        <asp:SessionParameter Name="PlantID" SessionField="PlantID" />
        <asp:QueryStringParameter Name="ProductionLine" QueryStringField="Line" ConvertEmptyStringToNull="true" />
        <asp:SessionParameter Name="ProductionLine" SessionField="ProductionLine" />
    </SelectParameters>
</asp:SqlDataSource>

It is the data source of a GridView and it populates it without issues. But when I try to use it to get the same data by calling the .Select() method in the code behind, it does not return anything.

DataRow Part = ((DataView)DataSourcePFEP.Select(new DataSourceSelectArguments())).ToTable().Select("Material = '" + CommandArguments[0] + "' AND SupplierID = '" + CommandArguments[1] + "'")[0];

As you can see, it takes the result of the .Select(), converts this into a DataTable by .ToTable() to then select the row I need using another .Select of the DataTable. But it will throw a null reference error right when trying to convert the results of the DataSourcePFEP.Select().

The issue is that even though I call the .Select(), no select actually seems to happen. I added OnSelecting and OnSelected method for test and they don't fire at all (even though they do during regular Page_Load when it is populating the GridView.

So why could it be that the SqlDataSource.Select() does not fire at all?


Solution

  • Alright, after a bit of experimenting, it looks like I need to set SelectCommand for the SqlDataSource again before calling the Select() on it.

    I don't understand why, as it is already set during Page_Load. Does the SelectCommand get cleared for some reason after the SqlDataSource populates the GridView?

    I don't understand. Anyway, this way it seems to work. I just needed to set the SelectCommand again.