"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Showing posts with label Datatable. Show all posts
Showing posts with label Datatable. Show all posts

Wednesday, October 30, 2013

linq to loop through datatable c#

Iterating through a data table using linq in c#

i faced a criteria to trim a row value with some criteria,

Before trimming

 



After trimming















C# Code





If u had any trouble just ask, Happy to help u :)
Stay Tune...
Have a nice day... 'N happy Coding :)

Wednesday, January 30, 2013

Remove Duplicate Row from datatable in c#

As i was also unaware of this feature until i read as answer in fourms.asp.net (link). Previously i was achieved by using lambda expression.Now no need to those just one line code :)


DataTable dt = new DataTable
        {
            Columns ={
                {"Myid",typeof(int)},
                {"Name"},
                {"Address"}
                }
        };
        dt.Rows.Add(1, "Arun Aravind", "Kollam");
        dt.Rows.Add(1, "Arun Aravind", "Kollam");
        dt.Rows.Add(2, "Arun Aravind", "Kollam");
        dt.Rows.Add(2, "Arun Aravind", "Kollam");
        dt.Rows.Add(3, "Arun Aravind", "Kollam");

        dt = dt.DefaultView.ToTable(false, "Address", "Name", "Myid");

Output




If u had any trouble just ask, Happy to help u :)
Stay Tune... Have a nice day... 'N happy Coding :)

Tuesday, January 22, 2013

Convert a datatable to Json c#

Hi *.*,
I'm so happy today to introduce this.Last day by noon i got a crazy idea to create own json converter. i don't have a plan or an architecture how it goes, where to start. all what i had is a json string :). I finished that by 2 hr 50 min. :)

For the reusability of the code i created that by extension method. and i named the class JsonExtensionMethod class goes below.


public static class JsonExtensionMethod
{

Tuesday, January 15, 2013

join two DataTables where both having a common column

Join two DataTables where both having a common column


      DataTable dt1 = new DataTable("Table1");

        DataTable dt2 = new DataTable("Table2");

        DataSet ds = new DataSet("DataSet");


Sunday, December 2, 2012

Count of duplicate record in dataset or datatable



Count of duplicate record in dataset or datatable in C# 


   DataTable dtList = dtduplicate();

        DataTable dt = ds.Tables[0];
      
        var result = from c in dt.AsEnumerable()
                     group c by new
                     {
                         ClientID = c.Field<string>("ClientID"),
                         CurrentServicerLoanNo = c.Field<string>("CurrentServicerLoanNo"),
                         CurrentLoanServicer=c.Field<string>("CurrentLoanServicer"),
                         NameOfOriginatingLender=c.Field<string>("NameOfOriginatingLender"),
                         OriginalLoanNumber=c.Field<string>("OriginalLoanNumber")
                     } into g
                     where g.Count() > 1
                     select new
                     {
                        
                         g.Key.ClientID,
                         g.Key.CurrentServicerLoanNo,
                         g.Key.CurrentLoanServicer,
                         g.Key.NameOfOriginatingLender,
                         g.Key.OriginalLoanNumber,
                         Noofrec = g.Count()
                     };

for (int i = 0; i < result.ToList().Count; i++)
        {
            var item = result.ToList()[i];
            //   resultTable.Rows.Add(item.ProdId, item.Pin, item.Noofrec);
            dtList.Rows.Add(null,item.ClientID, item.CurrentLoanServicer, item.CurrentServicerLoanNo, item.NameOfOriginatingLender, item.Noofrec, item.OriginalLoanNumber);
        }

   public static DataTable dtduplicate()
    {
        DataTable dtDuplicatelist = new DataTable();

        DataColumn auto = new DataColumn("AutoID", typeof(System.Int32));
        dtDuplicatelist.Columns.Add(auto);
        auto.AutoIncrement = true;
        auto.AutoIncrementSeed = 1;
        auto.ReadOnly = true;
        auto.Unique = true;


        dtDuplicatelist.Columns.Add("ClientID", typeof(string));
        dtDuplicatelist.Columns.Add("CurrentLoanServicer", typeof(string));
        dtDuplicatelist.Columns.Add("CurrentServicerLoanNo", typeof(string));
        dtDuplicatelist.Columns.Add("NameOfOriginatingLender", typeof(string));
        dtDuplicatelist.Columns.Add("Noofrec", typeof(string));
        dtDuplicatelist.Columns.Add("OriginalLoanNumber", typeof(string));

        dtDuplicatelist.PrimaryKey = new DataColumn[] { auto }; // Setting as primary key Then only it will add autoincrement to the Column
        return dtDuplicatelist;
    }

 



If u had any trouble just ask, Happy to help u :)
Stay Tune...
Have a nice day... 'N happy Coding :)

Saturday, December 1, 2012

Dynamically add a new column to dataset or data table in c#



Dynamically add a new column to dataset or data table in c#
 

System.Data.DataTable dtIncremented = ds.Tables[0];
        DataColumn auto = new DataColumn("AutoID", typeof(System.Int32));
        dtIncremented.Columns.Add(auto);
        //auto.AutoIncrement = true;
        //auto.AutoIncrementSeed = 1;
        //auto.ReadOnly = true;
        //auto.Unique = true;

       // dtIncremented.Columns.Add(auto);
        //dtIncremented.PrimaryKey = new DataColumn[] { auto };

        dtIncremented.BeginLoadData();

        DataTableReader dtReader = new DataTableReader(ds.Tables[0]);
        dtIncremented.Load(dtReader);

        dtIncremented.EndLoadData();

//foreach (DataRow dr in dtIncremented.Rows)
       // {
       //     dr["AutoID"] = dtIncremented.Rows.IndexOf(dr);
     //   }


If u had any trouble just ask, Happy to help u :)
Stay Tune...
Have a nice day... 'N happy Coding :)