C# GridView Paging - databasae

4/2/2014·3 min read

This article demostrate how to enable paging, by connect database object source with grid view.

select database

click grid view and select choose object source

select database

 

database for database connection

this database connection will add to webconfig file

select what sql object you want to achieve

you could use regular sql and stored procedure

test your sql

 

result what's UI looks

 

Since you connect you source object to datbase, how code looks like? Is should be bad? lots sql on client? Let's check the actual source

aspx page
  <asp:GridView  ID="GridView1"  runat="server"  AllowPaging="True"
             AutoGenerateColumns="False"  DataSourceID="SqlDataSource1">
             <Columns>
                 <asp:BoundField  DataField="Id"  HeaderText="Id"  SortExpression="Id"  /> 
                 <asp:BoundField  DataField="Name"  HeaderText="Name"  SortExpression="Name"  /> 
                 <asp:BoundField  DataField="Description"  HeaderText="Description"
                     SortExpression="Description"  /> 
                 <asp:BoundField  DataField="ListPrice"  HeaderText="ListPrice"
                     SortExpression="ListPrice"  /> 
             </Columns>
         </asp:GridView>
         <asp:SqlDataSource  ID="SqlDataSource1"  runat="server"
             ConnectionString="  <%  $  ConnectionStrings:ConnectionStringDatabase  %>  "  
             SelectCommand="SELECT * FROM [Product]"></asp:SqlDataSource>
 
C# back-end side
   public  partial  class  GridView_Paging_Database  : System.Web.UI.Page 
     {
         protected  void  Page_Load(object  sender, EventArgs  e)
         {
 
         }
     }
 

So, bad practise for this is, you put your sql to your UI. eventhough you don't see it on your actual HTML, but, if you have layor like, business logic layor and data layor, you should put your sql to data layor, It's not good to build a SOA service based application too, since you put your sql logic on your web application.