Pages

Showing posts with label create datatable. Show all posts
Showing posts with label create datatable. Show all posts

Tuesday, September 24, 2013

Copy unique rows of a column from one Datatable to other Datatable - LINQ



Here , We have to copy the unique rows of column 'CategoryName' from DataTable 'dt_old' to the 'Category' column of DataTable 'dt_Categories' using LINQ.


 DataTable dt_Categories = new DataTable();
 dt_Categories.Columns.Add("Category", Type.GetType("System.String"));

DataTable dt_old = new DataTable();
dt_old = (DataTable)Session["dt"];


 dt_Categories = dt_old.AsEnumerable().Select(row =>
            {
                DataRow newRow = dt_Categories.NewRow();
                newRow["Category"] = row.Field<string>("CategoryName");
                return newRow;
            }).Distinct(DataRowComparer.Default).CopyToDataTable();






Monday, August 19, 2013

Create Datatable manually-ASP.Net

Add Red Code to Create DataTable, change the columns name and Datatype according to your needs.

  // Initialise Datatable

        DataTable dt = new DataTable( );      

// add columns with their Datatypes

        dt.Columns.Add("Id", Type.GetType("System.Decimal"));                    // decimal type
        dt.Columns.Add("Country_ID", Type.GetType("System.String"));          // string type
        dt.Columns.Add("Name", Type.GetType("System.String"));
        dt.Columns.Add("Description", Type.GetType("System.String"));
        dt.Columns.Add("Url", Type.GetType("System.String"));
        dt.Columns.Add("Unique Url Name", Type.GetType("System.String"));
        dt.Columns.Add("Meta Title", Type.GetType("System.String"));
        dt.Columns.Add("Meta Keyword", Type.GetType("System.String"));
        dt.Columns.Add("Meta Description", Type.GetType("System.String"));



Details:

The below line is used to add a column with the Datatype:
dt.Columns.Add("Column_name", Type.GetType("System.DataType_name"));