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.
Alan
|