title: How to use PagedDataSource with SQL for paging?
body: <p>I don't understand how you can only get the number of needed rows from a database when you need to know the total amount of rows inorder to get paging to work.</p>
  
  <p>What I mean is:
  I followed this example:
  <a href="http://www.c-sharpcorner.com/uploadfile/rizwan328/datalist-custom-paging-in-Asp-Net-using-C-Sharp/" rel="nofollow">http://www.c-sharpcorner.com/uploadfile/rizwan328/datalist-custom-paging-in-Asp-Net-using-C-Sharp/</a></p>
  
  <p>But instead of using a dataTable I have a database.</p>
  
  <p>I get the news from the database like this, this gets all the news from my news table:</p>
  
  <pre><code>List&lt;News&gt; news = News.GetNews();
  
  public static List&lt;News&gt; GetNews(){
      List&lt;News&gt; news = new List&lt;News&gt;();
      using (SqlConnection conn = new SqlConnection(CONNSTRING)) {
              SqlCommand cmd = new SqlCommand("SELECT * FROM News_news ORDER BY date DESC", conn);
  </code></pre>
  
  <p>That gets all the data from all the news items.</p>
  
  <p>Then I create a PagedDataSource:</p>
  
  <pre><code>PagedDataSource objPds = new PagedDataSource();
  objPds.DataSource = news;
  objPds.AllowPaging = true;
  objPds.PageSize = numRows;
  //Set the current page 
  objPds.CurrentPageIndex = CurrentPage;
  
  //Set the buttons
  </code></pre>
  
  <p>Putt the data in a repeater that will show the first 5 items on the aspx page</p>
  
  <pre><code>repeater1.DataSource = objPds;
  repeater1.DataBind(); 
  </code></pre>
  
  <p>Like this I have a paged front page that shows me the total number of pages and the current page and when I click on the button for the next page I do it all over again.</p>
  
  <p>But I know this isn't correct since I always get all the news and then discared the news I don't need. but please, how do need to do it so that I only get the needed news items?</p>
  
  <p>Do I need to first count the number of rows in my table?</p>