Wrox Programmer Forums
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 11th, 2006, 09:51 PM
Registered User
Join Date: Dec 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQLDataSource row count

I am a newbie to ASP.NET 2.0, and until I 'find my feet' with this new stuff I am using Visual Web Developer, with C# as my scripting language. I hope this is the most suitable forum for this question, but if not feel free to move it to a more appropriate forum.

I have a gridview bound to a SQLDataSource (let's call it MyData), which connects to a SQL-server back-end. If MyData contains rows I want the grid to display the rows, but if MyData contains no rows, I want to display a message. Something like this...

<% if (MyData.Tables(0).rows.count>0) {%>
    <asp:Gridview id="MyGrid" DataSourceID="MyData" ...
<% } else {%>
    <asp:Label id="Label1" runat="server" Text="Sorry, no records selected"></asp:Label>
<% }%>

When I run the code I get the following
 Compiler Error Message: CS0117: 'System.Web.UI.WebControls.SqlDataSource' does not contain a definition for 'Tables'.

On further reading it appears that MyData.Tables(0).rows.count only works if MyData is a dataset.
Q. How do I select records into a dataset instead of a SQLDataSource? (Is this the same as ADO.net?)
Q. How do I bind a DataSet to the grid?
Q. What would be the equivalent solution for determining a row count using a SQLDataSource?

How can I get the numer of rows in the datasource? I think I can use MyGrid.Rows.Count to obtain the number of rows in the grid, but I want to refer to the datasource elsewhere in the page, so I would rather know how to get the row-count from the datasource itself.

As I get into ASP.net more, and to make my code more modular, I can see myself wanting to get data data via C# functions instead of asp:SqlDataSource commands (with filter parameters passed as function parameters). Is SQLDataSource the best way to go in this case, or would datasets be more efficient and/or easier to program?

Any help would be appreciated.


Old December 27th, 2006, 03:23 PM
Registered User
Join Date: Dec 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

The recommendation is to use the Event Handler for the Selected Event for SqlDataSource.

Protected Sub SqlDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEvent Args) Handles

  Label1.Text = String.Format("rows count: {0}", e.AffectedRows)

End Sub

I am looking answer for the same question.

I found it on:


Posted by: Daniel TIZON, MCP - MCSD.NET - MCT

Hope it helps.

Old December 29th, 2006, 08:20 PM
Registered User
Join Date: Dec 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

Hi CodingUnknowingly,

Thanks for your reply.


Old June 5th, 2010, 06:02 AM
Registered User
Join Date: Jun 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Thanks

Thanks for your reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
Row Count in each table ayazhoda Access VBA 0 April 15th, 2008 04:31 PM
advance count (2 counts in a row) avishni SQL Language 3 November 23rd, 2006 05:04 AM
Row count Discrepancy bph SQL Server 2000 9 January 26th, 2006 02:04 PM
SELECT COUNT as a ROW?? t0ny SQL Language 3 December 8th, 2004 05:31 AM
Row Count Walden SQL Server 2000 6 June 9th, 2003 12:47 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.