How can I check if SqlDataSource returned data?

I have an asp.net page that has several SqlDataSourcesthat define feed data for some graphs. The graphic product does not handle gracefully "without data" and throws an exception. I would like this to handle the situation - so I need to check if the data was returned SqlDataSourcebefore the graph was displayed (and if not, just post the message “No data” or something else).

Is there an easy way to check if the data source returned data and do it if / or without a bunch of code?

+5
source share
2 answers

The following is taken from devcurry: this is pretty much what you are looking for.

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName],
    [ContactTitle], [Address] FROM [Customers]"
    onselected="SqlDataSource1_Selected">
</asp:SqlDataSource>

And in the code behind:

Protected Sub SqlDataSource1_Selected(ByVal sender As Object, ByVal e As SqlDataSourceStatusEventArgs)

    If e.AffectedRows < 1 Then

        ' perform action

    End If

End Sub
+14
source

All Articles