Posts

Showing posts with the label DataTable

Jquery serer side datatables in asp.net

In this post we will learn how we can implement jquery   server side datatables in asp.net mvc application. We need to add datatables library to our page, we can add references of libray from datatables <a href="https://datatables.net/" target="_blank">website</a>. We need to create an html table on which we will apply datatable. <table class="table table-striped table-bordered table-hover table-checkable order-column dataTable" id="tblInventoryItems">   <thead>     <tr>       <th>Item Name</th>       <th>Item Price</th>       <th>Item Quantity</th>       <th></th>     </tr>   </thead>   <tbody></tbody> </table> Then we need the code which will apply the datatable to above html table. Below is the code to apply datatable $("yourtableid").DataTable({         "p...

Reading / looping through datatable C#

DataTable dt = new DataTable (); dt.Columns.Add( "ID" ); dt.Columns.Add( "Name" ); dt.Columns.Add( "Address" ); DataRow dr = dt.NewRow(); dr[ "ID" ] = 1; dr[ "Name" ] = "John" ; dr[ "Address" ] = "Street 10" ; dt.Rows.Add(dr); DataRow dr1 = dt.NewRow(); dr[ "ID" ] = 2; dr[ "Name" ] = "Smith" ; dr[ "Address" ] = "Street 10" ; dt.Rows.Add(dr1); //Looping through datatable foreach ( DataRow row in dt.Rows) {     Console .WriteLine(row[ "ID" ]);     Console .WriteLine(row[ "Name" ]);     Console .WriteLine(row[ "Address" ]); }

Load data from SqlDataReader into DataTable

SqlDatareader is used in .Net to read data from sql server database. We may have a situation where we need to fill data into DataTable from SqlDataReader . DatatTable has a Load method which accepts SqlDataReader object and fills DataTable with data. DataTable dt = new DataTable (); using ( SqlConnection con = new SqlConnection ( ConfigurationManager .ConnectionStrings[ "DefaultConnection" ].ConnectionString)) {    using ( SqlCommand cmd = new SqlCommand ())    {       con.Open();       cmd.CommandText = "Select * from tblUsers" ;       cmd.Connection = con;       cmd.CommandType = CommandType .Text;        SqlDataReader reader = null ;       reader = cmd.ExecuteReader();       dt.Load(reader);                ...

Filter DataTable in C#

Datatable is used to store data in table form. We may face a situation where we need to filter datatable in C# code. Here is how we can load data in datatable. Below is a table which we will load in datatable in C#. ID Name Address 1 Mairaj Street 1 2 Ahmad Street 10 3 Minhas Street 12 We will filter the table using the ID column where ID value is 1. DataRow []FilteredData = dt.Select( "ID = 1" ); foreach ( DataRow row in FilteredData) {   Console .WriteLine(FilteredData[0][ "ID" ]); } The result retuned by Select method is an array of DataRow. We can loop through array to get value from each row.

Load data in datatable from SQL Server database

Loading data from database is very basic part of every application which uses database as data store. Let's see how we can load data in  DataTable  from database using  ADO.NET  from a  SQL server database. DataTable  dt =  new   DataTable (); using  ( SqlConnection  con =  new   SqlConnection ( ConfigurationManager .ConnectionStrings[ "MyConnectionString" ].ConnectionString)) {     using  ( SqlCommand  cmd =  new   SqlCommand ())    {       cmd.CommandText =  "Select * from tblUsers" ;       cmd.Connection = con;       cmd.CommandType =  CommandType .Text;        using  ( SqlDataAdapter  adapter =  new   SqlDataAdapter (cmd))       {          adapter.Fill(dt);     ...

Load data in datatable from SQL Server database

Loading data from database is very basic part of every application which uses database as data store. Let's see how we can load data in DataTable from database using ADO.NET from a SQL server database. DataTable dt = new DataTable (); using ( SqlConnection con = new SqlConnection ( ConfigurationManager .ConnectionStrings[ "MyConnectionString" ].ConnectionString)) {    using ( SqlCommand cmd = new SqlCommand ())    {       cmd.CommandText = "Select * from tblUsers" ;       cmd.Connection = con;       cmd.CommandType = CommandType .Text;       using ( SqlDataAdapter adapter = new SqlDataAdapter (cmd))       {          adapter.Fill(dt);         }     }  }