jQuery and ASP.NET

February 13, 2009

How do you check if SQLDataSource returns empty data




A user asked me a simple way to determine if the SQLDataSource returns empty data.

Here's a simple way to do so: Use the OnSelected event of the SQLDataSource


<asp:SqlDataSource ID="SqlDataSource1" runat="server"


ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"


SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName],


[ContactTitle], [Address] FROM [Customers]"


onselected="SqlDataSource1_Selected">


</asp:SqlDataSource>




C#


protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)


{


if (e.AffectedRows < 1)


{


// perform action


}


}




VB.NET


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


If e.AffectedRows < 1 Then


' perform action


End If


End Sub



Note: As pointed out by a reader, this tip will not work if SQLDataSource caching is enabled.

'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

4 Responses to "How do you check if SQLDataSource returns empty data"
  1. Shilpa Jathar said...
    February 19, 2010 9:30 PM

    Thank you for posting this solution. Really helpful.

  2. Anonymous said...
    July 15, 2010 3:23 PM

    It helps me too, thank you!

  3. Anonymous said...
    March 17, 2011 11:20 AM

    This is useful but it fails when SQLdatasource Caching is enabled, as 0 is returned.

  4. Suprotim Agarwal said...
    March 17, 2011 5:38 PM

    Good point! I have added it to the post.

 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions