Friday, 14 March 2014

IEnumerable and IEnumerator

IEnumerable:


We have create UI, Business Layer and Data Layer. Data Layer return a list, That list do some manipulation (changes)  in BL. BL to UI this list return IEnumerable<>. Because I (BL Developer)  thought UI Developer unable to change the value. Security propose we can use IEnumerable, Because IEnumerable object unable to Add,Remove,ect...

Difference between IEnumerable and List properties 


Example List 


IEnumerable VS IEnumerator
      Ienumerator also unable to Add and remove ect... Values. But here we can't use lambda expression. 

enumerator is created or after the Reset method is called, an enumerator is positioned before the first element of the collection, and the first call to the MoveNextmethod moves the enumerator over the first element of the collection.
If MoveNext passes the end of the collection, the enumerator is positioned after the last element in the collection and MoveNext returns false. When the enumerator is at this position, subsequent calls to MoveNext also return false until Reset is called.
An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and the next call to MoveNext or Reset throws an InvalidOperationException.


Code :

using Generic.Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Collection.Sample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Btn_IList_Click(object sender, EventArgs e)
        {
            CollectionAll obj = new CollectionAll();
            IEnumerable<ProEmp> objreturn = obj.RetIEnumerableValue();
            var tt= objreturn.AsParallel();
            objreturn.Where(n=>n.Id == 103);
            var item1 = (objreturn.Where(n => n.Id == 102)).FirstOrDefault();
            MessageBox.Show("Id = " + item1.Id + "   Name  = " + item1.Name);
        }

        private void btnList_Click(object sender, EventArgs e)
        {
            CollectionAll obj = new CollectionAll();
            List<ProEmp> objreturn = obj.RetListValue();
           
            objreturn.Where(n => n.Id == 103);
            var tt = objreturn.Where(n => n.Id == 102);
        }

        private void btn_IEnumerator_Click(object sender, EventArgs e)
        {
            CollectionAll obj = new CollectionAll();
            IEnumerator<ProEmp> objreturn = obj.RetIEnumeratorValue();
           
            while (objreturn.MoveNext())
            {
                ProEmp item = objreturn.Current;
                MessageBox.Show("Id = " + item.Id + "   Name  = " + item.Name);
                if (item.Id == 103)
                    break;
            }
            MessageBox.Show("While loop End");
            objreturn.MoveNext();
           
            ProEmp item1= objreturn.Current;
            MessageBox.Show("Id = " + item1.Id + "   Name  = " + item1.Name);

          

        }

        private void btnDelegates_Click(object sender, EventArgs e)
        {
            CollectionAll obj = new CollectionAll();
            MessageBox.Show(obj.Calc(15, 8).ToString());
        }
    }
}


Class Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Generic.Code
{
    public class CollectionAll
    {

        List<ProEmp> objemp;
        public IEnumerable<ProEmp> RetIEnumerableValue()
        {
            Setvalue();
            return objemp;
        }

        public List<ProEmp> RetListValue()
        {
            Setvalue();
            return objemp;
        }

        public IEnumerator<ProEmp> RetIEnumeratorValue()
        {
            Setvalue();
            return objemp.GetEnumerator();
        }

        private void Setvalue()
        {
            objemp = new List<ProEmp>();
            ProEmp obj = new ProEmp();
            obj.Id = 101;
            obj.Name = "Sriram";
            obj.Salary = 20000;
            obj.Address = "Chennai";
            objemp.Add(obj);
            obj = new ProEmp();
            obj.Id = 102;
            obj.Name = "Bala";
            obj.Salary = 200000;
            obj.Address = "Chennai";
            objemp.Add(obj);
            obj = new ProEmp();
            obj.Id = 103;
            obj.Name = "Azeez";
            obj.Salary = 150000;
            obj.Address = "Madurai";
            objemp.Add(obj);
            obj = new ProEmp();
            obj.Id = 104;
            obj.Name = "Ratheesh";
            obj.Salary = 200000;
            obj.Address = "Madurai";
            objemp.Add(obj);
            obj = new ProEmp();
            obj.Id = 105;
            obj.Name = "Vikash";
            obj.Salary = 180000;
            obj.Address = "Chennai";
            objemp.Add(obj);
        }

    }
}


namespace Generic.Code
{
    public class ProEmp
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public double Salary { get; set; }
        public string Address { get; set; }
    }
}

Thursday, 13 March 2014

Linq,Lambda and SQL Group by

SQL & Linq Lambda Group

Group By allows us to group rows together and additionally perform aggregate calculation which works on a group of rows. Let me first give you a simple example of what group by does using one field. I will use "SampleTable" table and group all customers by Class in other words I want to show all unique values that exist in Country Field, that is how group by works. See below example code with results (before and after)


Sql Table example




DataTable example




Group by example with Aggregate Function

You can group by fields in tables but this is rarely used on its own and very often that is combined with aggregate function. Aggregate function gets access to grouped rows. So if you group 3 rows into 1 then you can perform aggregate calculation like count which in this case would give result 3. You can also use other aggregate functions like SUM, MAX, MIN, AVG and several other ones. See below example that is the same as previous ones but now with Count aggregate function.


select class,COUNT(Class) from SampleTable Group by Class




Linq Output:

 var query = from p in dt.AsEnumerable()
                        group p by p.Field<string>("Class") into g
                        select new { ClassType = g.Key.ToString(), CCount = g.Count() };

Lambda Output:

 var query = dt.AsEnumerable().GroupBy(n => n.Field<string>("Class")).
                Select(y => new
                {
                    ClassType = y.Key.ToString(),
                    CCount = y.Count()
                });



Sunday, 9 March 2014

Permission levels (SharePoint Foundation 2010)

Full Control          - Has full control.
Design                 - Can view, add, update, delete, approve, and customize.
Contribute            - Can view, add, update, and delete list items and documents.
Read                   - Can view pages and list items and download documents.
View Only             - Can view pages, list items, and documents. Document                                     types   with server-side file handlers can be viewed in the                               browser but not downloaded.
Approve                  - Can edit and approve pages, list items, and documents.
Manage Hierarchy  - Can create sites and edit pages, list items, and documents.
Restricted Read      - Can view pages and documents, but cannot view                                          historical  versions or user permissions.

 .



Friday, 7 March 2014

How to transfer data from one form to another form in c# in window application.

I have a datagridview a form which have name "ParentForm".The datagridview have the column name "Name and Age".
Now I want to transfer(retrieve) the column name "id" into the "ChildForm" in window apps. 

There change the value and save it. that value Update in "ParentForm" DataGridview.



Parent Form Design






Parent Form Code


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FormsDemoProject
{
    public partial class ParentForm : Form
    {
        DataTable dtStudentTable;
        string studentName;
        string studentAge;
       
        public string StudentName
        {
            get { return studentName; }
            set { studentName = value; }
        }
        public string StudentAge
        {
            get { return studentAge; }
            set { studentAge = value; }
        }

        public ParentForm()
        {
            InitializeComponent();
            dtStudentTable = new DataTable();
            dtStudentTable.Columns.Add("Student Name");
            dtStudentTable.Columns.Add("Age");
            StudentGridView.DataSource = dtStudentTable;
        }
       
        private void btnAdd_Click(object sender, EventArgs e)
        {
            DataRow drStudentRow = dtStudentTable.NewRow();
            drStudentRow["Student Name"] = txtStudentName.Text;
            drStudentRow["Age"] = txtStudentAge.Text;
            dtStudentTable.Rows.Add(drStudentRow);
            StudentGridView.DataSource = dtStudentTable;
            txtStudentName.Clear();
            txtStudentAge.Clear();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {

            StudentName = StudentGridView.SelectedRows[0].Cells[0].Value.ToString();
            StudentAge = StudentGridView.SelectedRows[0].Cells[1].Value.ToString();
            ChildForm childForm = new ChildForm();
            childForm.StudentName = StudentName;
            childForm.StudentAge = StudentAge;
            childForm.ParentFormReference = this;
            childForm.InitializeValues();
            childForm.ShowDialog();
        }

        public void UpdateGrid()
        {
            StudentGridView.SelectedRows[0].Cells[0].Value = StudentName;
            StudentGridView.SelectedRows[0].Cells[1].Value = StudentAge;
        }
    }
}


ChildForm Design


Child Form Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FormsDemoProject
{
    public partial class ChildForm : Form
    {
        ParentForm parentFormRef;
        string studentName;
        string studentAge;

        public ChildForm()
        {
            InitializeComponent();
        }

        public string StudentName
        {
            get { return studentName; }
            set { studentName = value; }
        }
        public string StudentAge
        {
            get { return studentAge; }
            set { studentAge = value; }
        }

        public ParentForm ParentFormReference
        {
            get { return parentFormRef; }
            set { parentFormRef = value; }
        }

        public void InitializeValues()
        {
            txtStudentName.Text = StudentName;
            txtStudentAge.Text = StudentAge;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            parentFormRef.StudentName = txtStudentName.Text;
            parentFormRef.StudentAge = txtStudentAge.Text;
            parentFormRef.UpdateGrid();
            this.Close();
        }
    }
}


Output :