Friday, May 25, 2007

What's the range of each value data type in C#?

bool - System.Boolean (true and false)
byte - System.Byte (0 to 255)
sbyte - System.SByte (-128 to 127)
short - System.Int16 (-32768 to 32767)
ushort - System.Uint16 (0 to 65535)
int - System.Int32 (-2,147,483,648 to 2,147,483,647)
uint - System.UInt32 (0 to 4,294,967,295)
long - System.Int64 (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
ulong - System.UInt64 (0 to 18,446,744,073,709,551,615)
double - System.Double (-1.79769313486232e308 to 1.79769313486232e308)
float - System.Single (-3.402823e38 to 3.402823e38)
char - System.Char (0 to 65535)
decimal - System.Decimal (-79,228,162,514,264,337,593,543,950,335 to 79,228,162,514,264,337,593,543,950,335)

Wednesday, May 23, 2007

MD5 Decription

public static string Decrypt(string toDecrypt, string key, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

if(useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;

ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

return UTF8Encoding.UTF8.GetString(resultArray);
}

MD5 Encription

public static string Encrypt(string toEncrypt, string key, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

if(useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;

ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}

Selecting Items on Populated DropDownList

DropDownList1.SelectedValue = dr["type"].ToString();
// Value you have given as "value" once you populate combo

- OR -

DropDownList1.Items.FindByValue(dr["id"].ToString()).Selected = true;
//Search for the value on the list, then select it

- OR -

DropDownList1.Items.FindByText(dr["desc"].ToString()).Selected = true;
//Search for the text on the list, then select it

Tuesday, May 22, 2007

StrConv - vbProperCase in C#


public static string ProperCase(string stringInput)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
bool fEmptyBefore = true;
foreach (char ch in stringInput)
{
char chThis = ch;
if (Char.IsWhiteSpace(chThis))
fEmptyBefore = true;
else
{
if (Char.IsLetter(chThis) && fEmptyBefore)
chThis = Char.ToUpper(chThis);
else
chThis = Char.ToLower(chThis);
fEmptyBefore = false;
}
sb.Append(chThis);
}
return sb.ToString();
}

Monday, May 21, 2007

Sorting TreeView .Net 1.1

I got a assignment to Sort a TreeView on a application.., It had some elements already. Once user addes a new element it has to be get sorted among exsisting elements. I searched on the net, But no sucess. Finally, I have implemented my own method, Here I hope to share that with you all...


First Implement a "IComparable" class :


private class TreeSort : IComparable
{
private TreeNode _tvNode;
public TreeNode tvNode { get { return _tvNode; } set { _tvNode = value; } }
public TreeSort (TreeNode tvnode) { _tvNode = tvnode; }
public int CompareTo(object obj)

{

if (obj is TreeSort)

{

TreeSort ts = (TreeSort)obj;

return _tvNode.Text.CompareTo(ts.tvNode.Text); } return 0;

}

}

Now add follwoing code and call where ever you want to Sort the tree:
private void Sort(ref TreeView tv)

{

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

ArrayList UserVar = new ArrayList( tv.Nodes.Count );

foreach ( TreeNode childNode in tv.Nodes )

{

UserVar.Add( new TreeSort(childNode));

}

UserVar.Sort( ); //Sorting takes place here

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

tv.Nodes.Clear(); //Remove all nodes from tree

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

TreeNode root = new TreeNode("User Variables");

foreach(TreeSort ts in UserVar)

{

TreeNode tn = ts.tvNode;

root.Nodes.Add(tn);

}

tv.Nodes.Add(root);

root.Expand();

if(root.Nodes.Count>0) tv.SelectedNode = root.Nodes[0];

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

}

Date Time Formatting with C#

Here is the simplest way of formatting DateTime in to string,

string sDateTime = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");

you can change last part into any format you like...

Ex.
dd-MM-yyyy
dd-MM-yyyy HH:mm
dd-MM-yy HH:mm:ss
dd-MM-yyyy HH:mm:ss