Asp.Net DataTable Crud Operations

In the .Net developer’s life it is very common to come across the datatable manipulation. So here i have decided to explain the datatable manipulation like adding ,editing , updating or deleting records from datatable based on the condition and apply sorting datatable based on columns.

DataTable:

It represents one table of in-memory relational data, can be created and used independently, or can be used by other .NET Framework objects, most commonly as a member of a DataSet.

Creating DataTable & Adding Records:

Now i have created a new console application to explain the datatable manipulation. Datatable have different types of constructors. Based on our requirement we can use the specific constructor to create the datatable object. Below are the datatable constructors available.

DataTable() – This constructor is used to create new object of datatable with no arguments.
DataTable(String Tablename) – This constructor is used to create new object of the datatable with specified string as table name.
DataTable(String Tablename,String Namespace) – This constructor is used to create new object of the datatable with specified string as table name and specified namespace.

Now before creating instance of the datatable include the below namespaces if it is not already included.

using System;
using System.Data;
And now i have created the instance of the datatable with the table name as “Users” and inserted some sample records. And using the foreach loop i have just printed the dattable records.Below is the code.

// Initializes a new instance of the DataTable With Name
DataTable dtUser= new DataTable(“Users”);
 
// Add columns with datatype in users datatable
dtUser.Columns.Add(“UserId”, typeof(int));
dtUser.Columns.Add(“Name”, typeof(string));
dtUser.Columns.Add(“Age”, typeof(int));
// Add new Row to users datatable
DataRow newRow = dtUser.NewRow();
newRow[“UserId”] = 1;
newRow[“Name”] = “Pawan”;
newRow[“Age”] = “20”;
dtUser.Rows.Add(newRow);
 
// Add new Row to users datatable
newRow = dtUser.NewRow();
newRow[“UserId”] = 2;
newRow[“Name”] = “Diwakar”;
newRow[“Age”] = “18”;
dtUser.Rows.Add(newRow);
 
// Add new Row to users datatable
newRow = dtUser.NewRow();
newRow[“UserId”] = 3;
newRow[“Name”] = “Rahul”;
newRow[“Age”] = “25”;
 
// Add new Row to users datatable
newRow = dtUser.NewRow();
newRow[“UserId”] = 4;
newRow[“Name”] = “Pushpraj”;
newRow[“Age”] = “28”;
dtUser.Rows.Add(newRow);
 
//Table Initial Data
Console.WriteLine(“”);
Console.WriteLine(“Table Initial Data”);
Console.WriteLine(“=========================”);
foreach (DataRow dRow in dtUser.Rows)
{
 Console.WriteLine(“Id : “ + dRow[“UserId”].ToString() + “, Name : “+ dRow[“Name”].ToString() + “, Age : “ + dRow[“Age”].ToString());
}

Sorting Datatable Records:

We cann’t sort the datatable directly since we don’t have any predefined method to sort. So what we can do is create a new dataview from your datatable and apply the sorting you want to apply and then create a new datatable from the dataview using DataView.ToTable() method.

Below is the code i have used to sort the datatable.

//Sorting Datatable
DataView dv = dtUser.DefaultView;
dv.Sort = “Age asc”;
DataTable sortedTable = dv.ToTable();
Console.WriteLine(“Sorted Data Table By Age”);
Console.WriteLine(“=========================”);
foreach (DataRow dRow in sortedTable.Rows)
{
 Console.WriteLine(“Rank : “ + dRow[“StudentRank”].ToString()+“, Id : “ + dRow[“UserId”].ToString() + “, Name : “ + dRow[“Name”].ToString() + “, Age : “ + dRow[“Age”].ToString());
}
Updating & Deleting Datatable Records:

Below code i have used to update and delete the records in the datatable.

// Updating Student Record With Id 4
foreach (DataRow dRow in dtUser.Rows)
{
 if (dRow[“UserId”].ToString() == “4”)
 {

dRow[“Name”] = “Pawan Shukla”;
 dRow[“Age”] = 50;
 
 Console.WriteLine(“Modified Data”);
 Console.WriteLine(“=========================”);
 Console.WriteLine(“Id : “ + dRow[“UserId”].ToString() + “, Name : “ + dRow[“Name”].ToString() + “, Age : “ + dRow[“Age”].ToString());
 }
}
Console.WriteLine(“”);
 
// Deleting Data With Id 2 & 3
for (int i = 0; i < dtUser.Rows.Count; i++)
{
 if (dtUser.Rows[i][“UserId”].ToString() == “2” || dtUser.Rows[i][“UserId”].ToString() == “3”)
 {
 dtUser.Rows[i].Delete();
 }
}
 }

Leave a Reply

Your email address will not be published. Required fields are marked *