using Business;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
namespace WebApp.LIBS
{
public static class Common
{
public static void BindControl(Control CT, IEnumerable collection)
{
PropertyInfo propertyInfo = CT.GetType().GetProperty("DataSource");
propertyInfo.SetValue(CT, collection, null);
CT.DataBind();
}
public static DateTime DateConvertAcordingExcel(string str, string Format)
{
try
{
int day = 0;
int month = 0;
int year = 0;
var Datee = str.Split('/');
var fromats = Format.Split('/');
if (fromats[0] == "dd")
{
day = Datee[0].ConvertInt();
}
else if (fromats[1] == "dd")
{
day = Datee[1].ConvertInt();
}
else if (fromats[2] == "dd")
{
day = Datee[2].ConvertInt();
}
if (fromats[0] == "MM")
{
month = Datee[0].ConvertInt();
}
else if (fromats[1] == "MM")
{
month = Datee[1].ConvertInt();
}
else if (fromats[2] == "MM")
{
month = Datee[2].ConvertInt();
}
if (fromats[0] == "yyyy")
{
year = Datee[0].ConvertInt();
}
else if (fromats[1] == "yyyy")
{
year = Datee[1].ConvertInt();
}
else if (fromats[2] == "yyyy")
{
year = Datee[2].ConvertInt();
}
return new DateTime(year, month, day);
}
catch
{
return DateTime.ParseExact("dd/MM/yy", Format, CultureInfo.InvariantCulture);
}
}
public static string SeduleType(int @SeduleType)
{
string @ReturnValue="";
if (@SeduleType == 1)
{
@ReturnValue = "Monthly";
}
else if (@SeduleType == 2)
{
@ReturnValue = "Quarterly";
}
else if (@SeduleType == 3)
{
@ReturnValue = "HalfYearly";
}
else if (@SeduleType == 4)
{
@ReturnValue = "Yearly";
}
else if (@SeduleType == 5)
{
@ReturnValue = "Hourly";
}
return @ReturnValue;
}
public static decimal TruncateDecimal(decimal value, int decimalPlaces)
{
decimal integralValue = Math.Truncate(value);
decimal fraction = value - integralValue;
decimal factor = (decimal)Math.Pow(10, decimalPlaces);
decimal truncatedFraction = Math.Truncate(fraction * factor) / factor;
decimal result = integralValue + truncatedFraction;
return result;
}
public static void BindControldt(Control CT, DataTable dt)
{
PropertyInfo propertyInfo = CT.GetType().GetProperty("DataSource");
propertyInfo.SetValue(CT, dt, null);
CT.DataBind();
}
public static bool DeletePath(string path)
{
try
{
FileInfo finfo = new FileInfo(path);
if (finfo.Attributes == FileAttributes.Directory)
{
//recursively delete directory
Directory.Delete(path, true);
return true;
}
else if (finfo.Attributes == FileAttributes.Normal)
{
File.Delete(path);
return true;
}
else
{
return true;
}
}
catch (Exception ex)
{
return false;
}
}
public static string PopulateBody(string userName, string title, string url, string description)
{
string body = string.Empty;
using (StreamReader reader = new StreamReader(HttpContext.Current.Server.MapPath("~/admin/emailtemplate.html")))
{
body = reader.ReadToEnd();
}
body = body.Replace("{UserName}", userName);
body = body.Replace("{Title}", title);
body = body.Replace("{Description}", description);
return body;
}
public static DateTime DateConvert(string str)
{
return DateTime.ParseExact(str, "dd-MM-yyyy", CultureInfo.InvariantCulture);
}
public static DateTime DateConvertMultimatch(string str)
{
if (str.Contains("-"))
return DateTime.ParseExact(str, "dd-MM-yyyy", CultureInfo.InvariantCulture);
else if (str.Contains("/"))
return DateTime.ParseExact(str, "dd/MM/yyyy", CultureInfo.InvariantCulture);
else
return DateTime.ParseExact(str, "dd.MM.yyyy", CultureInfo.InvariantCulture);
}
public static DateTime DateConvert(string str, string Format)
{
return DateTime.ParseExact(str, Format, CultureInfo.InvariantCulture);
}
public static DataTable ToDataTable(List items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
//Get all the properties
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
dataTable.Columns.Add(prop.Name);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
//inserting property values to datatable rows
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
//put a breakpoint here and check datatable
return dataTable;
}
public static List IntArrayWithSize(int Main, int Max, int Count)
{
Random rand = new Random();
List result = new List();
HashSet check = new HashSet();
for (Int32 i = 0; i < Count; i++)
{
int curValue = rand.Next(Main, Max);
while (check.Contains(curValue))
{
curValue = rand.Next(Main, Max);
}
result.Add(curValue);
check.Add(curValue);
}
return result;
}
public static void BindControl(Control CT, IEnumerable collection, string DataTextField, string DataValueField, string DefaultValue)
{
PropertyInfo propertyInfo = CT.GetType().GetProperty("DataSource");
propertyInfo.SetValue(CT, collection, null);
propertyInfo = CT.GetType().GetProperty("DataTextField");
if (propertyInfo != null) { propertyInfo.SetValue(CT, DataTextField, null); }
propertyInfo = CT.GetType().GetProperty("DataValueField");
if (propertyInfo != null) { propertyInfo.SetValue(CT, DataValueField, null); }
CT.DataBind();
if (!String.IsNullOrEmpty(DefaultValue))
{
propertyInfo = CT.GetType().GetProperty("Items");
MethodInfo methodInfo = propertyInfo.PropertyType.GetMethod("Insert", new Type[] { typeof(int), typeof(string) });
methodInfo.Invoke(propertyInfo.GetValue(CT, null), new object[] { 0, DefaultValue });
}
}
public static void ASPxComboBoxReadOnlyProperty(DevExpress.Web.ASPxEditors.ASPxComboBox CT)
{
PropertyInfo propertyInfo = CT.GetType().GetProperty("ReadOnly");
propertyInfo.SetValue(CT, true, null);
propertyInfo = CT.GetType().GetProperty("BackColor");
propertyInfo.SetValue(CT, System.Drawing.Color.WhiteSmoke, null);
CT.DropDownButton.Visible = false;
}
public static void ASPxComboBoxunReadOnlyProperty(DevExpress.Web.ASPxEditors.ASPxComboBox CT)
{
PropertyInfo propertyInfo = CT.GetType().GetProperty("ReadOnly");
propertyInfo.SetValue(CT, false, null);
propertyInfo = CT.GetType().GetProperty("BackColor");
propertyInfo.SetValue(CT, System.Drawing.Color.White, null);
CT.DropDownButton.Visible = true;
}
public static void DeveXpressBindControl(DevExpress.Web.ASPxEditors.ASPxComboBox CT, IEnumerable collection, string DataTextField, string DataValueField, string DefaultValue)
{
PropertyInfo propertyInfo = CT.GetType().GetProperty("DataSource");
propertyInfo.SetValue(CT, collection, null);
propertyInfo = CT.GetType().GetProperty("TextField");
if (propertyInfo != null) { propertyInfo.SetValue(CT, DataTextField, null); }
propertyInfo = CT.GetType().GetProperty("ValueField");
if (propertyInfo != null) { propertyInfo.SetValue(CT, DataValueField, null); }
CT.DataBind();
if (!String.IsNullOrEmpty(DefaultValue))
{
CT.Items.Insert(0, new DevExpress.Web.ASPxEditors.ListEditItem(DefaultValue, DefaultValue));
CT.Value = DefaultValue;
}
}
public static void ResetForm(Control Container)
{
foreach (Control CT in Container.Controls)
{
PropertyInfo propertyInfo = null;
if (CT.GetType().Name == "TextBox" || CT.GetType().Name == "UserControl")
{
propertyInfo = CT.GetType().GetProperty("Text");
if (propertyInfo != null) { propertyInfo.SetValue(CT, String.Empty, null); }
}
propertyInfo = CT.GetType().GetProperty("SelectedIndex");
if (propertyInfo != null) { propertyInfo.SetValue(CT, 0, null); }
propertyInfo = CT.GetType().GetProperty("Checked");
if (propertyInfo != null) { propertyInfo.SetValue(CT, false, null); }
}
}
public static DateTime DateTimeNow()
{
TimeZoneInfo INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
DateTime indianTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, INDIAN_ZONE);
return indianTime;
}
public static void AllResetForm(Control[] objects)
{
foreach (var Container in objects)
{
foreach (Control CT in Container.Controls)
{
PropertyInfo propertyInfo = null;
if (CT.GetType().Name == "TextBox" || CT.GetType().Name == "UserControl")
{
propertyInfo = CT.GetType().GetProperty("Text");
if (propertyInfo != null) { propertyInfo.SetValue(CT, String.Empty, null); }
}
propertyInfo = CT.GetType().GetProperty("SelectedIndex");
if (propertyInfo != null) { propertyInfo.SetValue(CT, 0, null); }
propertyInfo = CT.GetType().GetProperty("Checked");
if (propertyInfo != null) { propertyInfo.SetValue(CT, false, null); }
}
}
}
public static string StripTagsCharArray(string source)
{
char[] array = new char[source.Length];
int arrayIndex = 0;
bool inside = false;
for (int i = 0; i < source.Length; i++)
{
char let = source[i];
if (let == '<')
{
inside = true;
continue;
}
if (let == '>')
{
inside = false;
continue;
}
if (!inside)
{
array[arrayIndex] = let;
arrayIndex++;
}
}
return new string(array, 0, arrayIndex);
}
public static string CreateSefUrl(string inputStr)
{
string outputStr = "";
outputStr = inputStr.Trim().Replace(":", "").Replace("&", "").Replace(" ", "-").Replace("'", "").Replace(",", "").Replace("(", "").Replace(")", "").Replace("--", "").Replace(".", "");
return Regex.Replace(outputStr.Trim().ToLower().Replace("--", ""), "[^a-zA-Z0-9_-]+", "", RegexOptions.Compiled);
}
public static string UrlBuilder(params string[] _Url)
{
if (_Url.Length > 0)
return String.Join("/", _Url);
else
return String.Empty;
}
public static string GetFirstParagraph(string _data)
{
if (String.IsNullOrEmpty(_data) == false)
{
Match m = Regex.Match(_data, @"\s*(.+?)\s*
");
if (m.Success)
return m.Groups[1].Value;
else
return GetDataByLength(_data, 200);
}
else
{
return String.Empty;
}
}
public static string GetDataByLength(string _data, int _length)
{
return (_data.Length < _length ? _data : _data.Substring(0, _length) + "....");
}
public static string GetExactDataByLength(string _data, int _length)
{
return (_data.Length < _length ? _data : _data.Substring(0, _length));
}
public static string Otp()
{
string _numbers = "0123456789";
Random random = new Random();
StringBuilder builder = new StringBuilder(6);
string numberAsString = "";
for (var i = 0; i < 6; i++)
{
builder.Append(_numbers[random.Next(0, _numbers.Length)]);
}
numberAsString = builder.ToString();
return int.Parse(numberAsString).ToString();
}
public static string RandomString(int size)
{
StringBuilder builder = new StringBuilder();
Random random = new Random((int)Common.DateTimeNow().Ticks);
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
return builder.ToString();
}
public static string NumbersToWords(int inputNumber)
{
int inputNo = inputNumber;
if (inputNo == 0)
return "Zero";
int[] numbers = new int[4];
int first = 0;
int u, h, t;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
if (inputNo < 0)
{
sb.Append("Minus ");
inputNo = -inputNo;
}
string[] words0 = {"" ,"One ", "Two ", "Three ", "Four ",
"Five " ,"Six ", "Seven ", "Eight ", "Nine "};
string[] words1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ",
"Fifteen ","Sixteen ","Seventeen ","Eighteen ", "Nineteen "};
string[] words2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ",
"Seventy ","Eighty ", "Ninety "};
string[] words3 = { "Thousand ", "Lakh ", "Crore " };
numbers[0] = inputNo % 1000; // units
numbers[1] = inputNo / 1000;
numbers[2] = inputNo / 100000;
numbers[1] = numbers[1] - 100 * numbers[2]; // thousands
numbers[3] = inputNo / 10000000; // crores
numbers[2] = numbers[2] - 100 * numbers[3]; // lakhs
for (int i = 3; i > 0; i--)
{
if (numbers[i] != 0)
{
first = i;
break;
}
}
for (int i = first; i >= 0; i--)
{
if (numbers[i] == 0) continue;
u = numbers[i] % 10; // ones
t = numbers[i] / 10;
h = numbers[i] / 100; // hundreds
t = t - 10 * h; // tens
if (h > 0) sb.Append(words0[h] + "Hundred ");
if (u > 0 || t > 0)
{
if (h > 0 || i == 0) sb.Append("and ");
if (t == 0)
sb.Append(words0[u]);
else if (t == 1)
sb.Append(words1[u]);
else
sb.Append(words2[t - 2] + words0[u]);
}
if (i != 0) sb.Append(words3[i - 1]);
}
return sb.ToString().TrimEnd() + " Only";
}
public static string GetMonthName(int Month)
{
return System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(Month);
}
public static int RankNodeCount(int rank)
{
int total = 0;
for (int i = rank; i >= 1; i--)
{
total += i;
}
return total;
}
public static int GetPercentRange(decimal range)
{
if (range >= 100)
return 100;
else if (range >= 75)
return 75;
else if (range >= 50)
return 50;
else if (range >= 25)
return 25;
else
return 0;
}
public static int CalculateAge(DateTime bday)
{
DateTime today = DateTime.Today;
int age = today.Year - bday.Year;
if (bday > today.AddYears(-age)) age--;
return age;
}
public static int ConvertInt(object value)
{
try
{
Convert.ToInt32(value);
}
catch { value = 0; }
return Convert.ToInt32(value);
}
public static byte Convertbyte(object value)
{
try
{
Convert.ToByte(value);
}
catch { value = 0; }
return Convert.ToByte(value);
}
public static short ConvertInt16(object value)
{
try
{
Convert.ToInt16(value);
}
catch { value = 0; }
return Convert.ToInt16(value);
}
public static string Convertstring(object value)
{
string retunvalu;
try
{
retunvalu = value.ToString();
}
catch { retunvalu = ""; }
return retunvalu;
}
public static decimal ConvertDecimal(object value)
{
try
{
Convert.ToDecimal(value);
}
catch { value = 0; }
return Convert.ToDecimal(value);
}
public static bool ConvertBool(object value)
{
try
{
Convert.ToBoolean(value);
}
catch { value = false; }
return Convert.ToBoolean(value);
}
//private void CerateTree(int userID)
//{
// var obj = Global.Context.UserLogins.SingleOrDefault(c => c.UserID == userID);
// List obmakelist = new List();
// if (SiteSession.RoleId ==(int) SiteKey.UserRoleEnum.BDO)
// {
// var objList = Global.Context.UserLogins.Where(C => C.SateID == obj.SateID && obj.DesignationId == (int)SiteKey.UserRoleEnum.BDO && C.DistrictID == obj.DistrictID).ToList();
// }
//}
}
}
public partial class TreeMake
{
public int DesignationId { get; set; }
public int UserId { get; set; }
}