Monday, September 15, 2014

Linq with Datatable : Asp.net C#

1. Show all data
********************************
 protected void Button1_Click(object sender, EventArgs e)
    {
        string srt = @"select e.EmployeeId,d.Designation,u.username,u.userpwd from Employees e 
                    inner join DesignationMaster d on d.Designationid=e.DesignationID
                    inner join userdet u on u.EmpId=e.EmployeeId";
        SqlDataAdapter adt = new SqlDataAdapter(srt, con);
        DataTable dt = new DataTable();
        adt.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

Output
********************************
Image1


2. Show all Asst.Professor
********************************
 protected void Button2_Click(object sender, EventArgs e)
    {
        string srt = @"select e.EmployeeId,d.Designation,u.username,u.userpwd from Employees e 
                    inner join DesignationMaster d on d.Designationid=e.DesignationID
                    inner join userdet u on u.EmpId=e.EmployeeId";
        SqlDataAdapter adt = new SqlDataAdapter(srt, con);
        DataTable dt = new DataTable();
        adt.Fill(dt);
       


        EnumerableRowCollection<DataRow> filter = from row in dt.AsEnumerable() where row.Field<string>("Designation") == "Asst.Professor" select row;
        DataView DV = new DataView();
        DV = filter.AsDataView();
        GridView1.DataSource = DV;
        GridView1.DataBind();
    }

Output
********************************
Image2


3. Filer based on user name
********************************
 protected void Button3_Click(object sender, EventArgs e)
    {
        string srt = @"select e.EmployeeId,d.Designation,u.username,u.userpwd from Employees e 
                    inner join DesignationMaster d on d.Designationid=e.DesignationID
                    inner join userdet u on u.EmpId=e.EmployeeId";
        SqlDataAdapter adt = new SqlDataAdapter(srt, con);
        DataTable dt = new DataTable();
        adt.Fill(dt);

        EnumerableRowCollection<DataRow> filter = from row in dt.AsEnumerable() where row.Field<string>("username").Contains(TextBox1.Text) select row;

        DataView dv = new DataView();
        dv = filter.AsDataView();
        GridView1.DataSource = dv;
        GridView1.DataBind();

    }

Output
********************************
Image3

No comments:

Post a Comment