using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// /// Summary description for Utilities /// public class Utilities { public Utilities() { // // TODO: Add constructor logic here // } // Returns a date/time stamp in the format YYYYMMDD_HHMMSS public static string GetTimeStamp() { DateTime dt = DateTime.Now; return string.Format("{0}{1}{2}_{3}{4}{5}", dt.Year, dt.Month.ToString().PadLeft(2, Char.Parse("0")), dt.Day.ToString().PadLeft(2, Char.Parse("0")), dt.Hour.ToString().PadLeft(2, Char.Parse("0")), dt.Minute.ToString().PadLeft(2, Char.Parse("0")), dt.Second.ToString().PadLeft(2, Char.Parse("0"))); } // Returns the month text for a month number public static string GetMonthText(int monthNumber, bool? getMonthAbbrev) { string monthName; switch (monthNumber) { case 1: monthName = "January"; break; case 2: monthName = "February"; break; case 3: monthName = "March"; break; case 4: monthName = "April"; break; case 5: monthName = "May"; break; case 6: monthName = "June"; break; case 7: monthName = "July"; break; case 8: monthName = "August"; break; case 9: monthName = "September"; break; case 10: monthName = "October"; break; case 11: monthName = "November"; break; case 12: monthName = "December"; break; default: monthName = "-1"; break; } if(getMonthAbbrev == true && monthNumber > 0 && monthNumber <= 12) { monthName = monthName.Substring(0, 3); } return monthName; } // Returns a date/time in the format MMMM DD, YYYY HH:MM AM/PM public static string GetFormattedDateTime() { DateTime dt = DateTime.Now; return string.Format("{0}{1}{2}", dt.ToLongDateString(), " ", dt.ToShortTimeString()); } // Returns a date in the format MMM DD, YYY public static string GetFormattedDate() { DateTime dt = DateTime.Now; return string.Format("{0}", dt.ToString("MMM dd yyyy")); } // Put a space after every comma public static string putSpaceAfterChar(string inputString, string charToPutSpaceAfter) { inputString = inputString.Replace(",", ", "); return inputString; } // Replaces spaces with hyphens. public static string replaceSpaces(string a) { a = a.Replace(' ', '-'); return a; } // Replace char1 with char2 in the string public static string replaceChar(string a, char charToReplace, char charToReplaceWith) { a = a.Replace(charToReplace, charToReplaceWith); return a; } // Replace str1 with str2 in the string public static string replaceString(string a, string stringToReplace, string stringToReplaceWith) { a = a.Replace(stringToReplace, stringToReplaceWith); return a; } // Replaces newline with a space. private string FixCrLf(string value) { if (String.IsNullOrEmpty(value)) { return String.Empty; } return value.Replace(Environment.NewLine, " "); } // Remove spaces. public static string removeSpaces(string a) { string temp = ""; int i; for (i = 0; i < a.Length; i++) if (a[i] != ' ') temp += a[i]; a = temp; return a; } // Remove illegal filename chars. public static string removeIllegalChars(string a) { string temp = ""; int i; for (i = 0; i < a.Length; i++) { switch (a[i]) { case ' ': break; case '\\': break; case '/': break; case '*': break; case ':': break; case '?': break; case '\"': break; case '<': break; case '>': break; case '|': break; default: temp += a[i]; break; } } a = temp; return a; } // Reverse a string. public static string reverse(string a) { string temp = ""; int i, j; for (j = 0, i = a.Length - 1; i >= 0; i--, j++) temp += a[i]; a = temp; return a; } //Function to count no.of occurences of Substring in Main string public static int CharCount(String strSource, String strToCount) { int iCount = 0; int iPos = strSource.IndexOf(strToCount); while (iPos != -1) { iCount++; strSource = strSource.Substring(iPos + 1); iPos = strSource.IndexOf(strToCount); } return iCount; } // Return part of string after specified character public static string GetStringPartAfterChar(String str, String chr) { int startPos; string subStr; startPos = str.IndexOf(chr) + 1; subStr = str.Substring(startPos); return subStr; } // Return part of string before specified character public static string GetStringPartBeforeChar(String str, String chr) { int lastPos; string subStr; lastPos = str.LastIndexOf(chr); if (lastPos >= 0) { subStr = str.Substring(0, lastPos); } else { subStr = str; } return subStr; } // Disable controls public static void DisableControls(Control c) { if (c is WebControl) { if (!(c is Label)) { ((WebControl)c).Enabled = false; } } foreach (Control child in c.Controls) { if (!(child is Label)) { DisableControls(child); } } } // Get logged-on userID public static string GetLoggedInUserID(string UserIdentityName) { String[] userNames; String userName; userNames = UserIdentityName.Split('\\'); userName = userNames[1]; return userName; } // Methods to populate lists and combo boxes // Overloaded method public static void populateCombo(SqlDataSource ds, DropDownList comboName, string spName, string dataValueField, string dataTextField) { ds.SelectCommandType = SqlDataSourceCommandType.StoredProcedure; ds.SelectCommand = spName; comboName.DataSource = ds; comboName.DataValueField = dataValueField; comboName.DataTextField = dataTextField; comboName.DataBind(); comboName.Items.Insert(0, "Select One"); } // Overloaded method public static void populateCombo(SqlDataSource ds, DropDownList comboName, string spName, string dataValueField, string dataTextField, string paramName, int paramValue) { ds.SelectCommandType = SqlDataSourceCommandType.StoredProcedure; ds.SelectCommand = spName; ds.SelectParameters.Clear(); // Declare a parameter & add it to the datasource //Parameter spParameter = new Parameter(paramName, TypeCode.Int32); //spParameter.DefaultValue = paramValue.ToString(); //ds.SelectParameters.Add(spParameter); // OR add the parameter directly ds.SelectParameters.Add(paramName, paramValue.ToString()); ds.SelectParameters[0].Direction = ParameterDirection.Input; comboName.DataSource = ds; comboName.DataValueField = dataValueField; comboName.DataTextField = dataTextField; comboName.DataBind(); comboName.Items.Insert(0, "Select One"); } public static void populateList(SqlDataSource ds, ListBox listBoxName, string spName, string dataValueField, string dataTextField) { ds.SelectCommandType = SqlDataSourceCommandType.StoredProcedure; ds.SelectCommand = spName; listBoxName.DataSource = ds; listBoxName.DataValueField = dataValueField; listBoxName.DataTextField = dataTextField; listBoxName.DataBind(); listBoxName.Items.Insert(0, "None"); listBoxName.Items[0].Selected = true; } }