Thursday, 11 April 2013

dataset clone and copy and select query and distinct use dataset table filter and date filter

 -----------------------Short code------------------------
DataTable table6 = ds.Tables[0];
                    if (table6.Rows.Count > 0)
                    {
                        table6.DefaultView.RowFilter = string.Format(CultureInfo.InvariantCulture, "EZone ='" + ds.Tables[3].Rows[i]["Zone"].ToString().Trim() + "' and eemailid='" + ds.Tables[3].Rows[i]["eemailid"].ToString().Trim() + "' and Inv_Dist_Awareness ='2' and  instdate >= #" + moreThan + "# and instdate <= #" + lessThan + "#");
                        table6 = table5.DefaultView.ToTable();
                    }
                  DataRow[] rows;
   rows = table6.Select();

-----------------------------------------------------
 DataSet tempTaskDS = new DataSet();
                DataTable tempTable = ds.Tables[0].Clone();
                foreach (DataRow row in ds.Tables[0].Select("instdate >= #" + Currentmonth_fisrt + "# and instdate <= #" + Currentmonth_last + "#"))
                {
                    tempTable.Rows.Add(row.ItemArray);
                    i++;
                }

                tempTaskDS.Tables.Add(tempTable);
--------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class Datasetclone_datasetclone : System.Web.UI.Page
{

    DataTable GetTable()
    {
        //
        // Here we create a DataTable with four columns.
        //
        DataTable table = new DataTable();
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Patient", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));

        //
        // Here we add five DataRows.
        //
        table.Rows.Add(25, "Indocin", "David", DateTime.Now);
        table.Rows.Add(25, "ajit", "David", DateTime.Now);
        table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
        table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
        table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
        table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
        return table;
    }
    protected void Page_Load(object sender, EventArgs e)
    {

        DataTable ds = new DataTable();
        ds = GetTable();
        DataSet ds1 = new DataSet();
        ds1.Tables.Add(ds);
        _FilterAndInsert(ds1);
        //ds.Tables.Add(employees);
        //ds.Tables.Add(payCheckes);
    }
    public DataSet  _FilterAndInsert(DataSet sourceSet)
    {
         sourceSet.Tables[0].Columns.Add("ppID", typeof(string));
        ///Apply your filter clauses in Select() function parameter
        DataRow[] rows = sourceSet.Tables[0].Select("Dosage >="+25+"");
        ///Get the structure of source table
        DataTable tempTable = sourceSet.Tables[0].Clone();
        ///Add the filtered rows in temp table
        int i = 0;
        foreach (DataRow row in rows)
        {
            tempTable.Rows.Add(row.ItemArray[0], row.ItemArray[1], row.ItemArray[2], row.ItemArray[3], row.ItemArray[4] = (i+1).ToString());
            i++;
        }
        tempTable.Columns.Remove("Date"); 
        tempTable.AcceptChanges();
        ///Create new dataset
        DataSet resultSet = new DataSet();
        ///Add temp table at first index or modify this code sequence as you require
        resultSet.Tables.Add(tempTable);
        for (int index = 1; index < sourceSet.Tables.Count; index++)
        {
            ///Set the copy of source table in local instance
            DataTable tableToAdd = sourceSet.Tables[index].Copy();
            ///Remove from source to avoid any exceptions
            sourceSet.Tables.RemoveAt(index);
            ///Add the copy to result set
            resultSet.Tables.Add(tableToAdd);
        }
        ///Set the copy to source table from result set
        sourceSet = resultSet.Copy();

        //DataTable tbl;
        //DataTable tbl2 = new DataTable();
        //DataSet ds = new DataSet();
        //ds.Tables.Add(new DataTable());
        //ds.Tables[0].Columns.Add("ID", typeof(int));
        //ds.Tables[0].Columns.Add("Tour_Code", typeof(int));
        //ds.Tables[0].Columns.Add("Tour_Date", typeof(string));
        ////ds.Tables[0].Columns.Add("No_Of_Vehicle", typeof(int));
        //ds.Tables[0].Columns.Add("Vehicle", typeof(string));
        //tbl2 = ds.Tables[0].Clone();

        DataTable dt = new DataTable("pramod");
        dt.Columns.Add("Dosage", tempTable.Columns["Dosage"].DataType);

        object LastValue = null;
        foreach (DataRow dr in tempTable.Select("", "Dosage"))
        {
            if (LastValue == null || !(ColumnEqual(LastValue, dr["Dosage"])))
            {
                LastValue = dr["Dosage"];
                dt.Rows.Add(new object[] { LastValue });
            }
        }
       
        return sourceSet;
    }

    private bool ColumnEqual(object A, object B)
    {

        // Compares two values to see if they are equal. Also compares DBNULL.Value.
        // Note: If your DataTable contains object fields, then you must extend this
        // function to handle them in a meaningful way if you intend to group on them.

        if (A == DBNull.Value && B == DBNull.Value) //  both are DBNull.Value
            return true;
        if (A == DBNull.Value || B == DBNull.Value) //  only one is DBNull.Value
            return false;
        return (A.Equals(B));  // value type standard comparison
    }
}

public string _FilterAndInsert(DataSet sourceSet)
    {
        // sourceSet.Tables[0].Columns.Add("ppID", typeof(string));
        ///Apply your filter clauses in Select() function parameter
        string lessThan = DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + "-" + DateTime.Now.Year.ToString();
        string moreThan = DateTime.Now.Month.ToString() + "-" + DateTime.Now.AddDays(-7).Day.ToString() + "-" + DateTime.Now.Year.ToString();

        DataRow[] rows = sourceSet.Tables[0].Select("instdate <= #" + lessThan + "# and instdate > #" + moreThan + "#");//+ "AND instdate >= #" + moreThan + "#");
        ///Get the structure of source table
        DataTable tempTable = sourceSet.Tables[0].Clone();
        ///Add the filtered rows in temp table
        int i = 0;
        string weeklycount = rows.Length.ToString(); ;

        return weeklycount;
    }

No comments:

Post a Comment