Thursday, January 30, 2014

How to pass Enum as Method Parameter in C#.net

  private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            CheckForEnum(MyEnum.Male );
        }
        public void CheckForEnum(MyEnum en) {
          int id =  (int)Enum.Parse(typeof(MyEnum), Convert.ToString(en), true);
            MessageBox.Show((id).ToString());
        }

 public enum MyEnum :int{
        Male = 1 ,   Female=2
    }

Sunday, September 15, 2013

How to read XML file

   Create an XML file named StudentData.xml.
Like this
<?xml version="1.0" encoding="utf-8" ?>

<Students>

 <Student>

 <name>Jamal</name>

 <class>six</class>

 </Student>

 <Student>

 <name>Kamal</name>

 <class>Seven</class>

 </Student>

 <Student>

 <name>Salam</name>

 <class>six</class>

 </Student>

 <Student>

 <name>Tomal</name>

 <class>six</class>

 </Student>

 <Student>

 <name>Jalal</name>

 <class>Seven</class>

 </Student>

</Students>
After this
Create a class student like this
public class Student

 
 
{

 
 private string name;

public string Name
 
 

{

 
 get { return name; }

set { name = value; }
 
 

}

 
 private string className;

public string ClassName
 
 

{

 
 get { return className; }

set { className = value; }
 
 

}

}
 
After that write the following line to read the StudentData.xml
 
XmlReaderSettings settings = new XmlReaderSettings();

settings.IgnoreComments = false;

settings.IgnoreWhitespace = true;

XmlTextReader xtr = new XmlTextReader(Server.MapPath("StudentData.xml"));

XmlDocument doc = new XmlDocument();


doc.Load(xtr);


 XmlNodeList nodeList = doc.SelectNodes("//Student");


Student _stu = null;

IList<Student> _studentList = new List<Student>();

foreach (XmlNode n in nodeList)


{


 _stu = new Student();


_stu.Name = n.ChildNodes[0].InnerText;

_stu. ClassName = n.ChildNodes[1].InnerText;

_studentList.Add(_stu);

}

GridView1.DataSource = _studentList;

GridView1.DataBind();



Friday, May 31, 2013

Get String From Collectoin Specific class attribute

Sometimes we need to get a specific atribute values of a collection of any class. In this case we use this generic method. 
//Here Source means collection, valueField means the attribute which we supplied
public static string GetStringFromCollectionAttribute<T>(this ObservableCollection<T> source, string valueField)

{


try

{


string returnString = String.Empty;


if (source.Count > 0)

{


foreach (var obj in source)

{


if (returnString == "") {

returnString = obj.GetType().GetProperty(valueField).GetValue(obj,
null).ToString();

}


else

{

returnString = returnString +

"," + obj.GetType().GetProperty(valueField).GetValue(obj, null);

}

}


if (returnString == String.Empty)

{

returnString = returnString.Substring(0, returnString.Length - 1);

}

}


return returnString;

}


catch (Exception ex)

{


throw ex;

}

}
 

//calling part



ObservableCollection<Student> StudentList = new ObservableCollection<Student>{


new Student {Id=1,Name = "Pria"},


new Student {Id=2,Name = "Smith"},


new Student {Id=3,Name = "Devid"},


new Student {Id=4,Name = "Helen"},


new Student {Id=5,Name = "Nuton"},


new Student {Id=6,Name = "Kafre"},

};


//If I want to get all of the student name from the list as a string I just call the following function


string StudentNames = Extention.GetStringFromCollectionAttribute(StudentList, "Name");

Friday, May 17, 2013

Get Mindate And Max Date froa

 
 public class Student
        {            private int id;            public int Id
            {
                get { return id; }
                set { id = value; }
            }
            private string name;            public string Name
            {
                get { return name; }
                set { name = value; }
            }            private DateTime admissionDate;            public DateTime AdmissionDate
            {
                get { return admissionDate; }
                set { admissionDate = value; }
            }        }

//ready list and call
List<Student> stdList = new List<Student> {

new Student{Id=1,Name="A", AdmissionDate = DateTime.Now.AddDays(-100)},

new Student{Id=2,Name="B", AdmissionDate = DateTime.Now.AddDays(-10)},

new Student{Id=3,Name="C", AdmissionDate = DateTime.Now.AddDays(-101)},

new Student{Id=4,Name="D", AdmissionDate = DateTime.Now.AddDays(-102)},

new Student{Id=5,Name="E", AdmissionDate = DateTime.Now.AddDays(-107)},

new Student{Id=6,Name="F", AdmissionDate = DateTime.Now.AddDays(-106)},

new Student{Id=7,Name="G", AdmissionDate = DateTime.Now.AddDays(-11)},

new Student{Id=8,Name="H", AdmissionDate = DateTime.Now.AddDays(-150)},

new Student{Id=9,Name="I", AdmissionDate = DateTime.Now.AddDays(-150)},

new Student{Id=10,Name="J", AdmissionDate = DateTime.Now.AddDays(-170)},

new Student{Id=11,Name="K", AdmissionDate = DateTime.Now.AddDays(-160)},

new Student{Id=12,Name="L", AdmissionDate = DateTime.Now.AddDays(-170)},

new Student{Id=13,Name="M", AdmissionDate = DateTime.Now.AddDays(-130)},

new Student{Id=14,Name="N", AdmissionDate = DateTime.Now.AddDays(-120)},

new Student{Id=15,Name="O", AdmissionDate = DateTime.Now.AddDays(-30)},

new Student{Id=16,Name="P", AdmissionDate = DateTime.Now.AddDays(-120)},

new Student{Id=17,Name="Q", AdmissionDate = DateTime.Now.AddDays(-80)},

new Student{Id=18,Name="R", AdmissionDate = DateTime.Now.AddDays(-104)},

};

DateTime minDate;

DateTime maxDate;

ExtensionCLS.GetMinMaxDate(stdList, "AdmissionDate",out minDate, out maxDate);

string outPutString = "The Mindate Is:" + minDate + " The max date Is:" + maxDate;

Console.WriteLine(outPutString);

Console.ReadLine();

}

      public static void GetMinMaxDate<T>(this List<T> source, string valueField, out DateTime minDate, out DateTime maxDate)
        {            List<T> obsColl = new List<T>();            DateTime _maxDate = DateTime.Now;            DateTime _minDate = DateTime.Now;            if (source.Count > 0)
            {                foreach (T element in source)
                {                    if (element.GetType().GetProperty(valueField).GetValue(element, null) != null)
                    {                        obsColl.Add(element);                    }                }                _minDate = (DateTime)(dynamic)(from obj in obsColl select obj.GetType().GetProperty(valueField).GetValue(obj, null)).Min();                _maxDate = (DateTime)(dynamic)(from obj in obsColl select obj.GetType().GetProperty(valueField).GetValue(obj, null)).Max();            }            minDate = _minDate;            maxDate = _maxDate;        }
 



Output: The Mindate Is:11/29/2012 2:16:11 AM  The max date Is:5/8/2013 2:16:11 AM

Convert Dictionary To String in C#.net


 
public staticclass ExtensionCLS

{

public static string ConvertDictinaryToString<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)

{


return string.Join(",", dictionary.Select(kv => kv.Key.ToString().Trim() + "*" + kv.Value.ToString().Trim()).ToArray());

}

 



}



//Create a dictionary Dictionary<string, string> MyList = new Dictionary<string, string>();

MyList.Add(
"A", "Apple");

MyList.Add(
"B", "Book");

MyList.Add(
"C", "Cat");

MyList.Add(
"D", "Dog");

MyList.Add(
"E", "Egg");


string returnString = "";

// Call the following dictionary to string
returnString =
ExtensionCLS.ConvertDictinaryToString(MyList);


Console.WriteLine(returnString);


Console.ReadLine();


Output: A*Apple,B*Book,C*Cat,D*Dog,E*Egg