Friday, May 1, 2015

asp.net convert file to base64 string

public static string FileToBase64(string filePath)
        {
            byte[] data = null;

            try
            {
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                long numBytes = new FileInfo(filePath).Length;
                //logger.LogInfo("Library.Functions.FileToBase64(): Path:" + filePath + " Bytes:" + numBytes);
                data = br.ReadBytes((int)numBytes);
                br.Close();
                fs.Close();
            }
            catch (BaseException ex)
            {
                logger.LogError("Library.Functions.FileToBase64(): Path:" + filePath + "; Exception: " + ex.ToString(), ex);
                return "";
            }

            if (data == null)
            {
                return "";
            }
            else
            {
                return Convert.ToBase64String(data);
            }
        }

asp.net convert byte to base64

  public static string ByteToBase64(byte[] byteData)
        {
            if(byteData == null)
            {
                return "" ;
            }
            else
            {
                 return Convert.ToBase64String(byteData);
            }

        }

asp.net convert to base 64

    public static byte[] FromBase64(string base64)
        {
            if (base64 == null) //Then Throw New ArgumentNullException("base64")
            {
                return new byte[0];
            }
            else
            {
                return Convert.FromBase64String(base64);
            }
        }

asp.net convert base64 to byte

  public static byte[] Base64ToByte(string base64)
        {
            if (base64 == null) //Then Throw New ArgumentNullException("base64")
            {
                return new byte[0];
            }
            else
            {
                return Convert.FromBase64String(base64);
            }
        }

asp.net create datatable

 public static DataTable CreateTable<T>()
        {
            Type entityType = typeof(T);
            DataTable table = new DataTable(entityType.Name);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

            foreach (PropertyDescriptor prop in properties)
            {
                table.Columns.Add(prop.Name, prop.PropertyType);
            }

            return table;
        }

asp.net convert To DataTable

  public static DataTable ToDataTable<T>(IList<T> list)
        {
            DataTable table = CreateTable<T>();
            Type entityType = typeof(T);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

            foreach (T item in list)
            {
                DataRow row = table.NewRow();

                foreach (PropertyDescriptor prop in properties)
                {
                    row[prop.Name] = prop.GetValue(item);
                }

                table.Rows.Add(row);
            }

            return table;
        }

asp.net Get Date Difference In Months

    public static Int32 GetDateDiffInMonths(string CurrentDate, string targetDate)
        {
            DateTime startDate = System.Convert.ToDateTime(CurrentDate);
            DateTime endDate = System.Convert.ToDateTime(targetDate);
           
            int monthsApart = 12 * (startDate.Year - endDate.Year) + startDate.Month - endDate.Month;
            return Math.Abs(monthsApart);
        }