0

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?

4
  • Asp:Sqldatasource includes a selectcommand property that the above seems to be missing. learn.microsoft.com/en-us/dotnet/api/… Commented Apr 4, 2024 at 13:13
  • It gets set during Page_Load in this case (sorry, I didn't include that part in my example). I assume if it is set during Page_Load, then it should stay the same and be already set in my button click event as well, no? Or do I have to set it again? Commented Apr 5, 2024 at 16:06
  • Are you receiving and exception? Are you calling data bind? Where are you setting the datasource for the grid view? You clearly need to include more source code. Commented Apr 8, 2024 at 18:16
  • No exception at all during the Select() call. The Select() simply returns null for some reason. As mentioned, OnSelecting and OnSelected does not fire at all. The GridView populates just fine, this issue occurs when I want to use the same data for something else in code behind - so I call the Select() method and want to put the results into a DataView. But despite the selectCommand and the parameters being set, the Select() call does not seem to actually do anything, it returns null. Commented Apr 12, 2024 at 13:07

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.