Wednesday, July 29, 2009
Create Folder
System.IO.Directory.CreateDirectory(@"c:\NewFolder");
System.IO.Directory.CreateDirectory(@"c:\NewFolder\NewSubFolder");
// Specify a "currently active folder"
//Create a new subfolder under the current active folder
string activeDir = @"c:\testdir2";
string newPath = System.IO.Path.Combine(activeDir, "mySubDir");
// Create the subfolder
System.IO.Directory.CreateDirectory(newPath);
System.IO.Directory.CreateDirectory(@"c:\NewFolder\NewSubFolder");
// Specify a "currently active folder"
//Create a new subfolder under the current active folder
string activeDir = @"c:\testdir2";
string newPath = System.IO.Path.Combine(activeDir, "mySubDir");
// Create the subfolder
System.IO.Directory.CreateDirectory(newPath);
Tuesday, July 28, 2009
Convert from string to byte
public static byte[] StringToByte(string InString)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(InString);
return bytes;
}
}
Monday, July 27, 2009
Array
The following code declares an array that can store 100 items starting from index 0 to 99.
int [] intArray;
intArray = new int[100];
int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
string[,] names = new string[2, 2] { {"A","B"}, {"X","Y"} };
int[] numArray = {1, 3, 5, 7, 9, 11, 13};
foreach (int num in numArray)
{
System.Console.WriteLine(num.ToString());
}
Saturday, July 25, 2009
IsDate
public bool IsDate(string sdate)
{
DateTime dt;
bool isDate = true;
try
{
dt = DateTime.Parse(sdate);
}
catch
{
isDate = false;
}
return isDate;
}
{
DateTime dt;
bool isDate = true;
try
{
dt = DateTime.Parse(sdate);
}
catch
{
isDate = false;
}
return isDate;
}
Convert from Array to DataTable
Convert Array List to DataTable
string[] AList = { "78", "73", "5", "3", "4" };
DataTable dt = new DataTable();
for (int i = 0; i <= 4; i++)
{
row = dt.NewRow();
row["xName"] = AList[i];
row["xID"] = AList[i];
dt.Rows.Add(row);
}
return dt;
string[] AList = { "78", "73", "5", "3", "4" };
DataTable dt = new DataTable();
for (int i = 0; i <= 4; i++)
{
row = dt.NewRow();
row["xName"] = AList[i];
row["xID"] = AList[i];
dt.Rows.Add(row);
}
return dt;
DataSet to Array List
Converting DataSet to Arrary String in C#
public static string[] GetArrayListX()
{
string[] ArrayListX = new string[] { };
string sqlCommand = "Select a_desc as descrption, a_id as ID FROM table Order by a_desc";
Database db = DatabaseFactory.CreateDatabase(); //Using Microsoft Enterprise Library
DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
DataSet ds = db.ExecuteDataSet(dbCommand);
DataTable DT = ds.Tables[0];
DataRowCollection Rows = DT.Rows;
int icount = ds.Tables[0].Rows.Count;
int i = 0;
if (icount > 0)
{
ArrayListX = new string[icount];
foreach (DataRow row in Rows)
{
ArrayListX[i] = row["descrption"].ToString();
i++;
}
}
return ArrayList;
}
public static string[] GetArrayListX()
{
string[] ArrayListX = new string[] { };
string sqlCommand = "Select a_desc as descrption, a_id as ID FROM table Order by a_desc";
Database db = DatabaseFactory.CreateDatabase(); //Using Microsoft Enterprise Library
DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
DataSet ds = db.ExecuteDataSet(dbCommand);
DataTable DT = ds.Tables[0];
DataRowCollection Rows = DT.Rows;
int icount = ds.Tables[0].Rows.Count;
int i = 0;
if (icount > 0)
{
ArrayListX = new string[icount];
foreach (DataRow row in Rows)
{
ArrayListX[i] = row["descrption"].ToString();
i++;
}
}
return ArrayList;
}
Convert Double from String
public static double getDoublefromString(string svalue)
{
double i = 0.00;
try
{
i = Double.TryParse(svalue, out i) ? i : 0.00;
}
catch
{
}
return i;
}
{
double i = 0.00;
try
{
i = Double.TryParse(svalue, out i) ? i : 0.00;
}
catch
{
}
return i;
}
Convert Int from String
public static int getIntfromString(string svalue)
{
int i = -1;
try
{
i = int.TryParse(svalue, out i) ? i : -1;
}
catch
{
}
return i;
}
{
int i = -1;
try
{
i = int.TryParse(svalue, out i) ? i : -1;
}
catch
{
}
return i;
}
Formating
18.333333 ---> 18.33
String.Format("{0:#0.00}", 18.333333); //Please note that above 18.3333 is not a string value
s = String.Format("{0:c}", price) //price is a double
Percentage
lblX.Text = String.Format("{0:0%}", ABC); //ABC is a double
Inline ASPX page
lblX.Text='<%# Bind("sDate","{0:d}") %>'
lblY.Text='<%# Bind("XOAmount","{0:c}") %>'
String.Format("{0:#0.00}", 18.333333); //Please note that above 18.3333 is not a string value
s = String.Format("{0:c}", price) //price is a double
Percentage
lblX.Text = String.Format("{0:0%}", ABC); //ABC is a double
Inline ASPX page
lblX.Text='<%# Bind("sDate","{0:d}") %>'
lblY.Text='<%# Bind("XOAmount","{0:c}") %>'
Common Dates
DateTime tomorrow = DateTime.Today.AddDays(1);
DateTime amonthago = DateTime.Today.AddMonths(-1);
String lastmonthdate = amonthago.ToShortDateString();
String stodaydate = DateTime.Today.ToShortDateString();
DateTime amonthago = DateTime.Today.AddMonths(-1);
String lastmonthdate = amonthago.ToShortDateString();
String stodaydate = DateTime.Today.ToShortDateString();
Subscribe to:
Posts (Atom)