Wednesday, September 7, 2016

Custom Enum attributes in C#

public static class EnumExtensions
    {
        public static TAttribute GetAttribute(this Enum value)
            where TAttribute : Attribute
        {
            var type = value.GetType();
            var name = Enum.GetName(type, value);
            return type.GetField(name) // I prefer to get attributes this way
                .GetCustomAttributes(false)
                .OfType()
                .SingleOrDefault();
        }
    }

    public class AliasInfoAttribute : Attribute
    {
        internal AliasInfoAttribute(string alias)
        {
            this.Alias = alias;
        }
        public string Alias { get; private set; }
    }

    public enum EnumType
    {
        [AliasInfo("Alias1")]
        Emp=1, 


        [AliasInfo("Alias2")]
        Mngr= 2
    }


To Get Attributes

GetAlias(EnumType.Emp)


 public static string GetAlias(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());

            AliasInfoAttribute[] attributes =
                (AliasInfoAttribute[])fi.GetCustomAttributes(
                typeof(AliasInfoAttribute),
                false);

            string desc = string.Empty;

            if (attributes != null &&
                attributes.Length > 0)
                return attributes[0].Alias;
            else
                return value.ToString();

        }


Happy Coding

Monday, August 1, 2016

List to CSV

List of Int to CSV 
 
 
List<int> list = new List<int>() {1,2,3,4,5};
Console.WriteLine(list.Select(f => f.ToString()).Aggregate((x, y) => x+","+y));
 
Output 
 
1,2,3,4,5 
 
 
 List of string to CSV using string.join
 
 List list = new List() {"1","2","3","4","5"};
 string joined =  string.Join("\",\"", list) ; 
 
Output  1,2,3,4,5  


 List of string to CSV using string.join  and string.Format
  
List list = new List() {"1","2","3","4","5"};
 string joined =  string.Format("\"{0}\"", string.Join("\",\"", list)); 
 
 Output  "1","2","3","4","5"  

Wednesday, June 22, 2016

Validation for Checkbox list(at least one check box selected)















Add Checkbox list

 
           


 Add custom validators
 
                 ForeColor="Red" ClientValidationFunction="ValidateCheckBoxList" runat="server" />


Add script 


Thursday, May 19, 2016

Single Selection in Radio button in GridView/RadGrid


We need to call Javascropt function on radio button click which select the current radio button and remove selection from already selected radio button


                            ID="RadGrid1" runat="server" Width="100%" EnableLinqExpressions="false" Height="680px"


                               
                                   
                                        
                                       
                                            "RadioCheck(this);"
>

                                   
 

                           








Happy Coding

Thursday, May 12, 2016

Find row in datatable linq using C#






  • Add a reference to the System.Data.DataSetExtensions.This is usually added by default.
  • Add the namespace System.Linq 

Create Datatable


        public static DataTable GetTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name", typeof(string));

            dt.Rows.Add("C#", 1);
            dt.Rows.Add("Asp.net", 2);
            dt.Rows.Add("SqlServer", 3);

            return dt;
        }


Find in datatable

  DataTable dt = GetTable();
var dataRow = dt.AsEnumerable().Where(x => x.Field("Name") == "C#").FirstOrDefault();

                    if (dataRow != null)
                    {
                        int Id = Convert.ToInt32(dataRow["Id"]);
                    }