Display data on page using datatable, object datasource and detailsview

Declare Object data source in page

<asp:ObjectDataSource  
  ID="ObjectDataSource1"  
  runat="server"  
  TypeName="testship" 
   SelectMethod="GetAllShippers"> 
</asp:ObjectDataSource>

Class to return data:

public class testship
{ 
    private static string _cnnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString(); 
     public static DataTable GetAllShippers()  
    { 
        SqlDataAdapter adp = new SqlDataAdapter( 
            "SELECT * FROM shippers", _cnnString); 
 
        DataSet ds = new DataSet("shippers"); 
        adp.Fill(ds, "shippers"); 
 
        return ds.Tables["shippers"];     
    } 
}

Details view on page to display data:

<asp:DetailsView  
  ID="DetailsView1"  
  runat="server"  
  DataSourceID="ObjectDataSource1" 
  AllowPaging="true"> 
</asp:DetailsView>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.