Search This Blog

2009-04-17

Some common general important method for .NET

some general utility class for .NET.I have created some common general utility method in a class named as "GeneralUtility.cs".Most of the important function like Convert To Double Array,Convert To one dimensional Array,Empty All Control,Empty All Textbox Control,Create String From Array,convert Excel To CSV,Reverse a number or Reverse a string,Refresh Parent Window,Number to word etc.

Create a class file GeneralUtility.cs and paste the following lines


using System;
using System.Data;
using System.Data.SqlClient;
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;
using System.Globalization;
using System.IO;
using System.Collections;
using System.Security.Cryptography;
using System.Data.OleDb;
using System.Text;
/// <summary>
/// Summary description for GeneralUtility
/// </summary>
public class GeneralUtility
{
//GeneralUtility//GeneralUtility
public GeneralUtility()
{
//
// TODO: Add constructor logic here
//
}
public static double[,] ConvertTo2DblArray(DataSet ds)
{
double[,] dblArray = new double[ds.Tables[0].Columns.Count, ds.Tables[0].Rows.Count];
//ArrayList List=new ArrayList(ds.Tables[0].Rows.Count);

//foreach(DataRow rowItem in dt.Rows)
//{

// List.Add(row(0));
//}
//for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
//{
// for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
// {
// //dblArray[i][j] = Convert.ToDouble(ds.Tables[0].Rows[i][0].ToString());

// //dblArray[i,j]=Convert.ToDouble(ds.Tables[0].Rows[i].ItemArray.GetValue(j).ToString());

// }

//}

for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
{
//dblArray[i][j] = Convert.ToDouble(ds.Tables[0].Rows[i][0].ToString());

dblArray[i, j] = Convert.ToDouble(ds.Tables[0].Rows[j][i].ToString());

}

}
return dblArray;


}
public static double[,] ConvertTo2DblArrayRowWise(DataTable dt)
{
//double[,] dblArray = new double[ds.Tables[0].Columns.Count, ds.Tables[0].Rows.Count];
double[,] dblArray = new double[dt.Rows.Count, dt.Columns.Count - 1];

for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count - 1; j++)
{
//dblArray[i][j] = Convert.ToDouble(ds.Tables[0].Rows[i][0].ToString());

//dblArray[i, j] = Convert.ToDouble(ds.Tables[0].Rows[i][j].ToString());
dblArray[i, j] = Convert.ToDouble(dt.Rows[i][j + 1]);
}

}
return dblArray;


}
public static double[] ConvertTo1DblArray(DataSet ds)
{
double[] dblArray = new double[ds.Tables[0].Rows.Count];

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dblArray[i] = Convert.ToDouble(ds.Tables[0].Rows[i][0].ToString());
}
return dblArray;
}
public static double[] ConvertTo1DblArray(DataTable dt)
{
double[] dblArray = new double[dt.Rows.Count];

for (int i = 0; i < dt.Rows.Count; i++)
{
dblArray[i] = Convert.ToDouble(dt.Rows[i][0]);
}
return dblArray;
}
public static string CreateStringFromArray(double[,] arrData)
{
string strArrayData = string.Empty;
for (int i = 0; i < (arrData.Length / 2); i++)
{
strArrayData = strArrayData + arrData[i, 1].ToString() + ",";
}
strArrayData = strArrayData.Substring(0, strArrayData.Length - 1);
return strArrayData;
}
public static void EmptyAllControl(Control parent, TextBox tb)
{
foreach (Control c in parent.Controls)
{
if (c.GetType() == typeof(TextBox))
{
((TextBox)(c)).Text = string.Empty;
tb.Focus();
}
else if (c.GetType() == typeof(DropDownList))
{
try
{
((DropDownList)(c)).SelectedIndex = 0;
}
catch { }
}
else if (c.GetType() == typeof(CheckBox))
{
((CheckBox)(c)).Checked = false;
}
if (c.HasControls())
{
EmptyAllControl(c, tb);
}
}
}
public static void EmptyTextBoxControl(Control parent, TextBox tb)
{
foreach (Control c in parent.Controls)
{
if (c.GetType() == typeof(TextBox))
{
((TextBox)(c)).Text = string.Empty;
tb.Focus();
}
if (c.HasControls())
{
EmptyTextBoxControl(c, tb);
}
}
}

//------------------

public static void convertExcelToCSV(string sourceFile, string worksheetName, string targetFile, string skipHeader)
{

//string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
// "Data Source=" + sourceFile + ";" +
// "Extended Properties=Excel 8.0;";
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data Source=" + sourceFile + ";" + @"Extended Properties=""Excel 8.0;HDR=" + skipHeader + @";IMEX=1″";
OleDbConnection conn = null;
StreamWriter wrtr = null;
OleDbCommand cmd = null;
OleDbDataAdapter da = null;

try
{

conn = new OleDbConnection(strConn);
conn.Open();

cmd = new OleDbCommand("SELECT * FROM [" + worksheetName + "$]", conn);
cmd.CommandType = CommandType.Text;
wrtr = new StreamWriter(targetFile);

da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

for (int x = 0; x < dt.Rows.Count; x++)
{
string rowString = "";
for (int y = 0; y < dt.Columns.Count; y++)
{
rowString += "\"" + dt.Rows[x][y].ToString() + "\",";
}
wrtr.WriteLine(rowString);

}

Console.WriteLine("File converted successfully.");

}
catch
{
// Console.WriteLine(exc.ToString());
Console.WriteLine("USAGE: XlsToCsv [XLS File] [CSV File] [Skip Header? Yes/No]");
Console.ReadLine();
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
conn.Dispose();
cmd.Dispose();
da.Dispose();
wrtr.Close();
wrtr.Dispose();
}
}

//--------------------

public static void ConvertDatasetToCSV(DataSet ds,string strFilePath,string seperator)
{
// Create the CSV file to which grid data will be exported.
//StreamWriter sw = new StreamWriter("D:\\BACOE2\\Reports\\detailedreports\\CSVReports\\" + strFilePath);
StreamWriter sw = new StreamWriter(strFilePath);
// First we will write the headers.
DataTable dt = ds.Tables[0];
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns[i]);
if (i < iColCount - 1)
{
//sw.Write(",");
sw.Write(seperator);
}
}
sw.Write(sw.NewLine);
// Now write all the rows.
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
sw.Write(dr[i].ToString());
}
if (i < iColCount - 1)
{
//sw.Write(",");
sw.Write(seperator);
}
}
sw.Write(sw.NewLine);
}
sw.Close();
}
public static DataTable csvToDataTable(string file, bool isRowOneHeader, char seperator)
{

DataTable csvDataTable = new DataTable();

//no try/catch - add these in yourselfs or let exception happen
//String[] csvData = File.ReadAllLines(HttpContext.Current.Server.MapPath(file));

String[] csvData = File.ReadAllLines(file);

//if no data in file ‘manually’ throw an exception
if (csvData.Length == 0)
{
throw new Exception("CSV File Appears to be Empty");
}

String[] headings = csvData[0].Split(seperator);
int index = 0; //will be zero or one depending on isRowOneHeader

if (isRowOneHeader) //if first record lists headers
{
index = 1; //so we won’t take headings as data

//for each heading
for (int i = 0; i < headings.Length; i++)
{
//replace spaces with underscores for column names
headings[i] = headings[i].Replace(" ", "_");

//add a column for each heading
csvDataTable.Columns.Add(headings[i], typeof(string));
}
}
else //if no headers just go for col1, col2 etc.
{
for (int i = 0; i < headings.Length; i++)
{
//create arbitary column names
csvDataTable.Columns.Add("col" + (i + 1).ToString(), typeof(string));
}
}

//populate the DataTable
for (int i = index; i < csvData.Length; i++)
{
//create new rows
DataRow row = csvDataTable.NewRow();

for (int j = 0; j < headings.Length; j++)
{
//fill them

row[j] = csvData[i].Split(seperator)[j];

}

//add rows to over DataTable
csvDataTable.Rows.Add(row);
}

//return the CSV DataTable
return csvDataTable;

}
public static string[] CreateFileListFromafolder(string ftpDirectory)
{
string[] fileList = Directory.GetFiles(ftpDirectory);
return fileList;

}
//------------------------------
public static bool IsDecimal(string inputString)
{
try
{
Convert.ToDouble(inputString);
return true;
}
catch
{
return false;
}
}

public static bool IsInteger(string inputString)
{
try
{
Convert.ToInt32(inputString);
return true;
}
catch
{
return false;
}
}

public static string Right(string s, int n)
{
string returnValue = "";
int p = s.Length;
p = p - n;
try
{
returnValue = s.Substring(p, n);
}
catch (Exception ex)
{
returnValue = "";
}
return returnValue;
}

public static string Left(string s, int n)
{
string returnValue = "";

try
{
returnValue = s.Substring(0, n);
}
catch (Exception ex)
{
returnValue = "";
}
return returnValue;
}

public static string Reverse(string inputString)
/**
* Reverses any given string
*/
{
StringBuilder returnString = new StringBuilder(15);
char[] inputCharArray = new char[15];
char c;

inputCharArray = inputString.ToCharArray();

for (int i = inputString.Length - 1; i >= 0; i--)
{
c = inputCharArray[i];
returnString.Append(c);
}

return returnString.ToString();
}

public static string GetFirstNChars(string s, int n)
{
/**
* Gets the First N Chars of a string
*/
if (s.Length > n - 1)
return (s.Substring(0, n));
return "";
}

//Function Name: Replicate.
//Parameters: string,integer.
//Purpose: Repliate a string n no. of times

public static string Replicate(int n)
{
int i;
string temp = " ";
for (i = 0; i < n; i++)
temp += " ";
return temp;
}

public static string Replicate(string s, int n)
{
int i;
string temp = "";
for (i = 0; i < n; i++)
temp += s;
return temp;
}

public static string Replicate(string first, int n, string second)
{
int i;
string temp = "";
for (i = 0; i < n; i++)
temp += second;
return first + temp;
}

//Expand width.

public static int ExpandWidth(string s)
{
int temp = 0;
temp = s.Length * 12;
return temp;
}

public static int ExpandLargeWidth(string s)
{
int temp = 0;
temp = s.Length * 140;
return temp;
}

public static int ExpandWidth(string s, int i)
{
int temp = 0;
temp = s.Length * i;
return temp;
}

//Conversion Functions:--

public static int Asc(string String)
{
//return Convert.ToInt32(String);
return String.Substring(0, 1).ToCharArray()[0];
}

public static char Chr(int num) //Overloaded
{
return Convert.ToChar(num);
}

public static char Chr(string num) //Overloaded
{
return Convert.ToChar(Convert.ToInt16(num));
}

public static string ReplaceQuotes(string s)
{
return s.Replace("'", "''");
}

public static string FormatValue(string StrVal, int Size)
{
int i;
string temp = "";
try
{
for (i = 0; i < (Size - StrVal.Length); i++)
{
temp = temp + " ";
if (i % 3 == 0)
{
temp = temp + " ";
}
}
return temp + StrVal;
}
catch (NullReferenceException e)
{
return temp;
}

}



public static string GetDateInSqlFormat(string inputStringWithSlashes)
{
/*string year = "";
string month = "";
string day = "";

if(inputStringWithSlashes.Trim().Length== 0)
return "";

month = inputStringWithSlashes.Substring(0,2);
day = inputStringWithSlashes.Substring(3,2);
year = inputStringWithSlashes.Substring(6,4);

return year + "-" + month + "-" + day ; */
if (inputStringWithSlashes != "")
{
string currentDate = inputStringWithSlashes;
int firstindex = currentDate.IndexOf("/");

string month = currentDate.Substring(0, firstindex);
string rest = currentDate.Substring(firstindex + 1, (currentDate.Length - month.Length - 1));
if (month.Length == 1)
{
month = "0" + month;
}
int nextindex = rest.IndexOf("/");
string day = rest.Substring(0, nextindex);
string year = rest.Substring(nextindex + 1, (rest.Length - day.Length - 1));
if (day.Length == 1)
{
day = "0" + day;
}

//string month = currentDate.Substring(0,1);
//string day = currentDate.Substring(2,1);
//string year = currentDate.Substring(4,4);
//if(month.Length==1){month="0"+month;}
// if(day.Length==1){day="0"+day;}
string returnDate = year.Substring(0, 4) + "-" + month + "-" + day;

return returnDate;
}
else
{
string returnDate = "";

return returnDate;
}
//return month+firstindex+rest;
//return time.ToString("d");
}

public static string GetDateFromCombo(string inputs)
{
string currentDate = inputs;
int firstindex = currentDate.IndexOf("/");

string month = currentDate.Substring(0, firstindex);
string rest = currentDate.Substring(firstindex + 1, (currentDate.Length - month.Length - 1));
if (month.Length == 1)
{
month = "0" + month;
}
int nextindex = rest.IndexOf("/");
string day = rest.Substring(0, nextindex);
string year = rest.Substring(nextindex + 1, (rest.Length - day.Length - 1));
if (day.Length == 1)
{
day = "0" + day;
}

//string month = currentDate.Substring(0,1);
//string day = currentDate.Substring(2,1);
//string year = currentDate.Substring(4,4);
//if(month.Length==1){month="0"+month;}
// if(day.Length==1){day="0"+day;}
string returnDate = year.Substring(0, 4) + "-" + month + "-" + day;

return returnDate;
//return month+firstindex+rest;
//return time.ToString("d");
}

public static string GetDateInScreenFormat(string inputDateStringWithHyphens)
{
string year = "";
string month = "";
string day = "";
int m, d;
inputDateStringWithHyphens = inputDateStringWithHyphens.Trim();
if (inputDateStringWithHyphens.Trim().Length == 0) // if empty string, then return
return "";
m = inputDateStringWithHyphens.IndexOf("/", 0, inputDateStringWithHyphens.Length);
month = inputDateStringWithHyphens.Substring(0, m);
d = inputDateStringWithHyphens.IndexOf("/", m + 1, 3);
day = inputDateStringWithHyphens.Substring(m + 1, d - m - 1);
year = inputDateStringWithHyphens.Substring(d + 1, 4);

//month = inputDateStringWithHyphens.Substring(0,2);
//day = inputDateStringWithHyphens.Substring(3,2);
//year = inputDateStringWithHyphens.Substring(5,4);

if (month.Length == 1)
month = "0" + month;
if (day.Length == 1)
day = "0" + day;
//if((month.Substring(1,1)=="/")(month.Substring(1,1)=="-"))
//month="0"+month.Substring(0,1);
//if(day.Substring(1,1)=="/")
//day="0"+day.Substring(0,1);

if (month.Equals("00") && day.Equals("00") && year.Equals("0000"))
return ""; // if the DB returns a "0000-00-00" value, then return blank
return (month + "/" + day + "/" + year);
}

//Function: GreaterTime.
//Return Type: bool
//Purpose: Returns true if the first time is greater than the second.

public static bool GreaterTime(string firstTime, string secondTime)
{
int firstTimehrs = int.Parse(firstTime.Substring(0, 2));
int firstTimeminutes = int.Parse(firstTime.Substring(3, 2));
int firstTimeInMinutes = firstTimehrs * 60 + firstTimeminutes;
int secondTimehrs = int.Parse(secondTime.Substring(0, 2));
int secondTimeminutes = int.Parse(secondTime.Substring(3, 2));
int secondTimeInMinutes = secondTimehrs * 60 + secondTimeminutes;
return (firstTimeInMinutes > secondTimeInMinutes ? true : false);
}

public static string IncrementID(string inputString, int incrementValue)
{
/**
* Increments any given ID by a specified amount
* param String inputString
* param int incrementValue
* return String returnString - incremented string
* Assumes the ID field comprises of a String part
* and an Integer part. The ID string first is read from right to left
* and divided into the Integer Part and then the String part.
* The Integer part is reversed and then incremented and the final result
* string is formed by concatenating the Integer and String parts
* reversed (since they were read from right to left at first).
*/
StringBuilder integerPart = new StringBuilder(12);
StringBuilder stringPart = new StringBuilder(12);
bool stillIntegerPart = true;
char[] inputCharArray = new char[15];
string returnString = "";

inputCharArray = inputString.ToCharArray();

for (int i = inputCharArray.Length - 1; i >= 0; i--)
{
char c = (char)inputCharArray.GetValue(i);
if (c >= '0' && c <= '9' && stillIntegerPart)
integerPart.Append(c);
else
{
stringPart.Append(c);
stillIntegerPart = false;
}
}
int intValue = Int32.Parse(Reverse(integerPart.ToString()));
intValue += incrementValue;
returnString = Reverse(stringPart.ToString()) + intValue;

return returnString;
}

public static string GetStringAfterNullCheck(string inputString)
{
/**
* Gets "" or the string value after a null check
*/
if (inputString != null)
return inputString;
else
return "";
}

public static string GetMonth()
{
return DateTime.Now.Date.Month.ToString().Trim();
}

public static string GetDay()
{
return DateTime.Now.Date.Day.ToString().Trim();
}

public static string GetYear()
{
return DateTime.Now.Date.Year.ToString().Trim();
}

public static string GetCurrentDate(int m)
{
/**
* Gets Date String in mm-dd-yyyy format
*/
DateTime time = DateTime.Now.AddMonths(m);
string currentDate = time.ToString("d");
int firstindex = currentDate.IndexOf("/");

string month = currentDate.Substring(0, firstindex);
string rest = currentDate.Substring(firstindex + 1, (currentDate.Length - month.Length - 1));
if (month.Length == 1)
{
month = "0" + month;
}
int nextindex = rest.IndexOf("/");
string day = rest.Substring(0, nextindex);
string year = rest.Substring(nextindex + 1, (rest.Length - day.Length - 1));
if (day.Length == 1)
{
day = "0" + day;
}

//string month = currentDate.Substring(0,1);
//string day = currentDate.Substring(2,1);
//string year = currentDate.Substring(4,4);
//if(month.Length==1){month="0"+month;}
// if(day.Length==1){day="0"+day;}
string returnDate = month + "/" + day + "/" + year;

return returnDate;
//return month+firstindex+rest;
//return time.ToString("d");
}

public static string GetCurrentDate()
{
/**
* Gets Date String in mm-dd-yyyy format
*/
DateTime time = DateTime.Now;
string currentDate = time.ToString("d");
int firstindex = currentDate.IndexOf("/");

string month = currentDate.Substring(0, firstindex);
string rest = currentDate.Substring(firstindex + 1, (currentDate.Length - month.Length - 1));
if (month.Length == 1)
{
month = "0" + month;
}
int nextindex = rest.IndexOf("/");
string day = rest.Substring(0, nextindex);
string year = rest.Substring(nextindex + 1, (rest.Length - day.Length - 1));
if (day.Length == 1)
{
day = "0" + day;
}

//string month = currentDate.Substring(0,1);
//string day = currentDate.Substring(2,1);
//string year = currentDate.Substring(4,4);
//if(month.Length==1){month="0"+month;}
// if(day.Length==1){day="0"+day;}
string returnDate = month + "/" + day + "/" + year;

return returnDate;
//return month+firstindex+rest;
//return time.ToString("d");
}
public static string GetOneMonthBackDate()
{
DateTime time = DateTime.Now;
string currentDate = time.ToString("d");
int firstindex = currentDate.IndexOf("/");

string month = currentDate.Substring(0, firstindex);
string rest = currentDate.Substring(firstindex + 1, (currentDate.Length - month.Length - 1));
if (month.Length == 1)
{
month = "0" + month;
}
int nextindex = rest.IndexOf("/");
string day = rest.Substring(0, nextindex);
string year = rest.Substring(nextindex + 1, (rest.Length - day.Length - 1));
if (day.Length == 1)
{
day = "0" + day;
}
int backM, backY;
if (month != "01")
{
backM = (int.Parse(month) - 1);
month = backM.ToString();
}
else
{
month = "12";
backY = (int.Parse(year) - 1);
year = backY.ToString();
}
if (month.Length == 1)
{
month = "0" + month;
}
string returnDate = month + "/" + day + "/" + year;
return returnDate;
}

public static string GetCurrentTimeWithoutSeconds()
{
/**
* Gets Time String in HH:MM format
*/
DateTime time = DateTime.Now;
string currentTime = time.ToString("t");
//string mode=currentTime.Substring(currentTime.Length-2,2);
//return mode;
return currentTime;
}

public static string GetCurrentTimeWithSeconds()
{
/**
* Gets Time String in HH:MM:SS format
*/
DateTime time = DateTime.Now;

return time.ToString("T");
}

public static string Get24HRSTimeFromAMPM(string s)
{
s = s.Trim();
if (s.Length > 2)
{
string p = s.Substring(0, 2);
string q = s.Substring(3, 2);
string r = s.Substring(6, 2);
if (r.Equals("PM"))
if (!(p.Equals("12")))
p = IncrementID(p, 12);

if (r.Equals("AM"))
if (p.Equals("12"))
p = IncrementID(p, 12);
return (p.Trim() == "0" ? "00" : p.Trim() + ":" + q.Trim());
}
else
{
s = GetCurrentTimeWithoutSeconds();
return s;
}

}

public static string GetAMPMFrom24HrsTime(string t)
{
string mode = "AM";
string p = t.Substring(0, 2);
string q = t.Substring(3, 2);
string s = "";
int r = Int32.Parse(p);
if (r > 12)
{
mode = "PM";
r -= 12;
}
s = r.ToString().Trim();
return ((s.Length == 1 ? "0" + s : s) + ":" + q + " " + mode);
}

public static string Set24HrsTime(string starttime)
{
string timeStart = "";
string min = "";

if (starttime != "")
{
int strTimeHr = 0;
int strTimeMin = 0;
string AMPM = "";

if (starttime.Length == 8)
{
strTimeHr = Int32.Parse(starttime.Substring(0, 2));
strTimeMin = Int32.Parse(starttime.Substring(3, 2));
AMPM = (starttime.Substring(6, 2));
}
else
{
if (starttime.Length == 5)
{
strTimeHr = Int32.Parse(starttime.Substring(0, 2));
strTimeMin = Int32.Parse(starttime.Substring(3, 2));
;
//AMPM=(starttime.Substring(3,2));
}

else
{
strTimeHr = Int32.Parse(starttime.Substring(0, 1));
strTimeMin = Int32.Parse(starttime.Substring(2, 2));
;
//AMPM=(starttime.Substring(2,2));

}

}

if (strTimeMin == 0)
{
min = ":00";
}
else
{
if (strTimeMin <= 9)
min = (":0" + strTimeMin.ToString());
else
min = (":" + strTimeMin.ToString());
}

if ((strTimeHr < 12) && (AMPM == "PM"))
{
strTimeHr = strTimeHr + 12;
timeStart = (strTimeHr.ToString() + min + " PM");
}

if ((strTimeHr == 12) && (AMPM == "PM"))
{
timeStart = (strTimeHr.ToString() + min + " PM");
}
if ((strTimeHr < 12) && (AMPM == "AM"))
{
if (strTimeHr <= 9)
timeStart = ("0" + strTimeHr.ToString() + min + " AM");
else
timeStart = (strTimeHr.ToString() + min + " AM");

}
if ((strTimeHr == 12) && (AMPM == "AM"))
{
//strTimeHr=strTimeHr-12;
timeStart = (strTimeHr.ToString() + min + " AM");
}

if (strTimeHr > 12)
{
strTimeHr = strTimeHr - 12;
if (strTimeHr <= 9)
timeStart = ("0" + strTimeHr.ToString() + min + " PM");
else
timeStart = (strTimeHr.ToString() + min + " PM");

}

/*if(strTimeHr<12)
{

if(strTimeHr<=9)
timeStart=("0"+strTimeHr.ToString() +min+" AM");
else
timeStart=(strTimeHr.ToString() +min+" AM");

}
if(strTimeHr==12)
{

timeStart=(strTimeHr.ToString() +min+" PM");

} */

}

else
{
timeStart = "";
}
return timeStart;

}

public static string GetAMPMFrom24Hrs(string starttime)
{
string timeStart = "";
string min = "";

if (starttime != "")
{
int strTimeHr = 0;
int strTimeMin = 0;
//string AMPM="";

if (starttime.Length == 5)
{
strTimeHr = Int32.Parse(starttime.Substring(0, 2));
strTimeMin = Int32.Parse(starttime.Substring(3, 2));
//AMPM=(starttime.Substring(6,2));

if (strTimeMin == 0)
{
min = ":00";
}
else
{
min = (":" + starttime.Substring(3, 2));
//min=(":"+strTimeMin.ToString());
}

if (strTimeHr < 12)
{
//strTimeHr=strTimeHr+12;
if (strTimeHr <= 9)
timeStart = ("0" + strTimeHr.ToString() + min);
else
timeStart = (strTimeHr.ToString() + min);
}

if (strTimeHr == 12)
{
timeStart = (strTimeHr.ToString() + min);
}

if (strTimeHr == 0)
{
strTimeHr = strTimeHr + 12;
timeStart = (strTimeHr.ToString() + min);
}

if (strTimeHr > 12)
{
strTimeHr = strTimeHr - 12;
if (strTimeHr <= 9)
timeStart = ("0" + strTimeHr.ToString() + min);
else
timeStart = (strTimeHr.ToString() + min);

}

}

if (starttime.Length == 7)
{
strTimeHr = Int32.Parse(starttime.Substring(0, 2));
strTimeMin = Int32.Parse(starttime.Substring(3, 2));
//AMPM=(starttime.Substring(6,2));

if (strTimeMin == 0)
{
min = ":00";
}
else
{
min = (":" + starttime.Substring(3, 2));
//min=(":"+strTimeMin.ToString());
}

if (strTimeHr < 12)
{
//strTimeHr=strTimeHr+12;
if (strTimeHr <= 9)
timeStart = ("0" + strTimeHr.ToString() + min);
else
timeStart = (strTimeHr.ToString() + min);
}

if (strTimeHr == 12)
{
timeStart = (strTimeHr.ToString() + min);
}

if (strTimeHr == 0)
{
strTimeHr = strTimeHr + 12;
timeStart = (strTimeHr.ToString() + min);
}
if (strTimeHr > 12)
{
strTimeHr = strTimeHr - 12;
if (strTimeHr <= 9)
timeStart = ("0" + strTimeHr.ToString() + min);
else
timeStart = (strTimeHr.ToString() + min);

}

}

if (starttime.Length == 8)
{
strTimeHr = Int32.Parse(starttime.Substring(0, 2));
strTimeMin = Int32.Parse(starttime.Substring(3, 2));
//AMPM=(starttime.Substring(6,2));

if (strTimeMin == 0)
{
min = ":00";
}
else
{
min = (":" + starttime.Substring(3, 2));
//min=(":"+strTimeMin.ToString());
}

if (strTimeHr < 12)
{
//strTimeHr=strTimeHr+12;
if (strTimeHr <= 9)
timeStart = ("0" + strTimeHr.ToString() + min);
else
timeStart = (strTimeHr.ToString() + min);
}

if (strTimeHr == 12)
{
timeStart = (strTimeHr.ToString() + min);
}

if (strTimeHr == 0)
{
strTimeHr = strTimeHr + 12;
timeStart = (strTimeHr.ToString() + min);
}
if (strTimeHr > 12)
{
strTimeHr = strTimeHr - 12;
if (strTimeHr <= 9)
timeStart = ("0" + strTimeHr.ToString() + min);
else
timeStart = (strTimeHr.ToString() + min);

}

}

}

else
{
timeStart = "";
}
return timeStart;

}

public static string ReturnAMPM(string starttime)
{
string mode = "";

if (starttime.Length == 8)
{
string ampm = starttime.Substring(6, 2);
if (ampm == "PM")
mode = "PM";
else
mode = "AM";
}

if (starttime.Length == 7)
{
string ampm = starttime.Substring(5, 2);
if (ampm == "PM")
mode = "PM";
else
mode = "AM";
}
if (starttime.Length == 5)
{
int hour = Int32.Parse(starttime.Substring(0, 2));
if (hour >= 12)
mode = "PM";
else
mode = "AM";
}

return mode;
}

public static string GetDayOfWeek(string dt)
{
return "";
}

public static void LockControls(Page Page)
{
foreach (Control ctrl in Page.Controls)
{
if (ctrl is TextBox)
{
if (((TextBox)ctrl).Visible == true)
((TextBox)ctrl).Enabled = false;
}

if (ctrl is Label)
{
if (((Label)ctrl).Visible == true)
((Label)ctrl).Enabled = false;
}

}

}

public static void ShowConcurrencyDialog(Page Page)
{
string m = "<script>alert('Concurrency Violation ... record could not be updated. ');<";
m += "/";
m += "script>";
if (!Page.IsStartupScriptRegistered("alert"))
Page.RegisterStartupScript("alert", m);

}

/*public static void ShowMessageDialog(System.Web.UI.Page Page,int message)
{

string m = "<script>alert('Message : " & message & "');";
m += "</script>";
if(!Page.IsStartupScriptRegistered("alert"))
Page.RegisterStartupScript("alert", m);
//Page.RegisterClientScriptBlock("ClientScript", m);
}*/

public static void CloseWindow(Page Page)
{
string mystring = "<script>window.returnValue='SAVED';window.close();<";
mystring += "/";
mystring += "script>";
Page.RegisterStartupScript("winclose", mystring);
}

public static void CloseWindowReturnValue(Page Page, string retVal)
{
string mystring = "<script>window.returnValue='" + retVal.Trim() + "';window.close();<";
mystring += "/";
mystring += "script>";
Page.RegisterStartupScript("winclose", mystring);
}

public static void RefreshParentWindow(Page Page, string ParentFormName)
{
string mystring = "<script>window.opener." + ParentFormName + ".submit();window.close();<";
mystring += "/";
mystring += "script>";
Page.RegisterStartupScript("winRefresh", mystring);
}

public static void RefreshParentWindow(Page Page, string ParentFormName, string ParentControl)
{
string mystring = "<script>window.opener.document." + ParentFormName + "." + ParentControl + ".value='Refresh';window.opener." + ParentFormName + ".submit();window.close();<";
mystring += "/";
mystring += "script>";
Page.RegisterStartupScript("winRefresh", mystring);
}

public static void PostBackParentWindow(Page Page, string ParentFormName, string ParentControl)
{
string mystring = "<script>if(window.opener.document!=null){window.opener.document." + ParentFormName + "." + ParentControl + ".click();window.close();}<";
mystring += "/";
mystring += "script>";
Page.RegisterStartupScript("winRefresh", mystring);
}

public static void RefreshParentWindow(Page Page, string ParentFormName, bool CloseCurrentWindow)
{
string mystring = "<script>window.returnValue='Refresh';<";
mystring += "/";
mystring += "script>";
Page.RegisterStartupScript("winRefresh", mystring);
}

public static void CloseWindow(Page Page, string document)
{
string mystring = "<script>window.returnValue='" + document.Trim() + "';window.close();<";
mystring += "/";
mystring += "script>";
Page.RegisterStartupScript("winclose", mystring);
}

public static string GetMultiSelectItems(ListBox lb)
{
string s = "";
foreach (ListItem li in lb.Items)
{
if (li.Selected == true)
{
if (li.Value.ToString().Trim().Equals("All"))
return li.Value.ToString().Trim();
s += "'" + li.Value.ToString().Trim() + "',";
}
}
return (s.Equals("") ? "" : "(" + s.Substring(0, (s.Length) - 1) + ")");
}

public static string GetMultiSelectItemsText(ListBox lb)
{
string s = "";
foreach (ListItem li in lb.Items)
{
if (li.Selected == true)
s += li.Text.ToString().Trim() + ",";
}
return (s.Equals("") ? "" : s.Substring(0, (s.Length) - 1));
}


public static string GetConditionFromScale(string scale)
{
string condition = "";
if (scale.Length >= 2 && !scale.Equals("10"))
condition = scale;
else
{
int scaleIndex = Int32.Parse(scale);
if (scaleIndex > 0 && scaleIndex < 5)
condition = "Mild";
else if (scaleIndex > 4 && scaleIndex < 8)
condition = "Moderate";
else if (scaleIndex > 7 && scaleIndex < 11)
condition = "Severe";

}
return condition;

}

public static string NumberToWord(string number)
{
int x;
int y;
int z;
int a;
int b = 0;
int c;
string num = "";
int no;
no = int.Parse(number);

x = no / 1000;
if (x >= 1)
{
num = RangeOneToNine(x) + " " + "Thousand";
}

y = no % 1000;
z = y / 100;
if (z >= 1)
{
num = num + " " + RangeOneToNine(z) + " " + "Hundred";
}
a = y % 100;
if (a >= 10 && a <= 19)
{
if (a >= 1)
{
num = num + " " + RangeTenToNineteen(a);
}
}
else
{
b = a / 10;
if (b >= 1)
{
num = num + " " + RangeTwentyToNinty(b);
}

c = a % 10;
if (c >= 1)
{
num = num + " " + RangeOneToNine(c);
}
}
return num;
}

private static string RangeOneToNine(int x1)
{
string num1 = "";
if (x1 == 1)
num1 = "one";
if (x1 == 2)
num1 = "two";
if (x1 == 3)
num1 = "three";
if (x1 == 4)
num1 = "four";
if (x1 == 5)
num1 = "five";
if (x1 == 6)
num1 = "six";
if (x1 == 7)
num1 = "seven";
if (x1 == 8)
num1 = "eight";
if (x1 == 9)
num1 = "nine";
return num1;
}

private static string RangeTenToNineteen(int x2)
{
string num2 = "";
if (x2 == 10)
num2 = "ten";
if (x2 == 11)
num2 = "eleven";
if (x2 == 12)
num2 = "twelve";
if (x2 == 13)
num2 = "thirteen";
if (x2 == 14)
num2 = "fourteen";
if (x2 == 15)
num2 = "fifteen";
if (x2 == 16)
num2 = "sixteen";
if (x2 == 17)
num2 = "seventeen";
if (x2 == 18)
num2 = "eighteen";
if (x2 == 19)
num2 = "nineteen";
return num2;
}

private static string RangeTwentyToNinty(int x3)
{
string num3 = "";
if (x3 == 2)
num3 = "twenty";
if (x3 == 3)
num3 = "thirty";
if (x3 == 4)
num3 = "fourty";
if (x3 == 5)
num3 = "fifty";
if (x3 == 6)
num3 = "sixty";
if (x3 == 7)
num3 = "seventy";
if (x3 == 8)
num3 = "eighty";
if (x3 == 9)
num3 = "ninety";
return num3;
}

public static string returnValues(string textValue)
{
if (textValue.StartsWith("."))
textValue = "0" + textValue;

return textValue;

}

//public static string ImportStyleSheet()
//{
// int i = 0;
// return "hello";
//}
//-------------------

}

No comments: