How to fill DataTable in ado.net, create new data table, data row, column
Ado.Net DataTable is just like a normal html table with DataRow, DataColumn, DataTable is used for pushing, pulling data into database with the help of DataAdapter
Using Ado.Net DataTable you can directly work with DataAdapter, but during application development most of the time DataTable comes under DataSet to deal with data in DataAdapter.
@using System.Data; DataTable _dt = new DataTable();
Before we show you few real-time Ado.net DataTable Example, we want you to read the few often used methods of DataTable.
Copy()
Copy both the structure and data for this DataTable.
Clone()
Copy the structure of the DataTable, but not the data
Clear()
Clear all data of DataTable
NewRow()
Create a new DataRow with the same schema as the table
Merge(DataTable, Boolean)
Merge the specified DataTable with the current DataTable.
Here we have created a DataTable structure with some hardcoded values, so you can have better understanding about DataTable structure during real time application development, will see that soon
DataTable _dt = new DataTable(); // New Instance created
//Adding three Column
_dt.Columns.Add(new DataColumn("Subject"));
_dt.Columns.Add(new DataColumn("TrainingDate"));
_dt.Columns.Add(new DataColumn("Location"));
// cerating a row with same structure using NewRow() method
DataRow _drow= _dt.NewRow();
// Now we are setting up some data in each column of that row
_drow["Subject"]="ADO.Net Training";
_drow["TrainingDate"] = DateTime.Now;
_drow["Location"] = "WebTrainingRoom";
//finally we are adding the row in table
_dt.Rows.Add(_drow);
//now the DataTable _dt is fully loaded with data.
Hope this will make you understand the Ado.Net DataTable structure.
Example of DataTable with DataAdapter and DataSet
You can directly fill your DataTable from DataAdapter.
DataTable getDataTable() { DataTable _dt = null; using (SqlConnection con = new SqlConnection(connectionString)) { SqlDataAdapter sde = new SqlDataAdapter("Select studentName, phone, adderss from tbStudent", con); _dt = new DataTable(); sde.Fill(_dt); } return _dt; }
Or can get the DataTable from DataSet, this method also return the DataTable same as above method.
DataTable getDataTable() { DataTable _dt = null; using (SqlConnection con = new SqlConnection(connectionString)) { SqlDataAdapter sde = new SqlDataAdapter("Select studentName, phone, adderss from tbStudent", con); DataSet ds = new DataSet(); sde.Fill(ds); _dt = ds.Tables[0]; } return _dt; }
How to use Copy, Clone, Clear methods of DataTable object
// copy the DataTable with data DataTable _dt1 = getDataTableDB().Copy(); // copy the DataTable structure without data DataTable _dt2 = getDataTableDB().Clone(); //this will clear all data from above DataTable _dt2.Clear();
In many situations you may need to create DataTable with static data for client demo or presentation etc, here is how you can populate DataTable with static Data
public static DataTable GetDT() { DataTable dt = new DataTable(); // Create columns as per requirement dt.Columns.Add("Name"); dt.Columns.Add("Mobile"); dt.Columns.Add("City"); return dt; }
Add row in DataTable: Now keep creating new empty row using dt.NewRow() method, then set data data in each row ... Same above method continues..
public static DataTable GetDT() { DataRow dr1 = dt.NewRow(); dr1["Name"] = "Arijit"; dr1["Mobile"] = "9820140001"; dr1["City"] = "Kolkata"; DataRow dr2 = dt.NewRow(); dr2["Name"] = "Aratrika"; dr2["Mobile"] = "9825780001"; dr2["City"] = "Durgapur"; DataRow dr3 = dt.NewRow(); dr3["Name"] = "Daniel"; dr3["Mobile"] = "8520140001"; dr3["City"] = "Bangalore"; // finally add each row in datatable object. dt.Rows.Add(dr1); dt.Rows.Add(dr2); dt.Rows.Add(dr3); return dt; }
Above DataTable you can add to any existing dataset also, or store in cache for reusability from different part of application