Tuesday, August 26, 2008
Convert Int to Enum value : C#
(yourEnumType)Enum.ToObject(typeof(yourEnumType), integerValue)
DomainName & Default Gateway : C#
Note: don't forget to include the namespace
using System.Net.NetworkInformation;
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
string intranetDomainName = ipProperties.DomainName;
foreach (NetworkInterface networkCard in NetworkInterface.GetAllNetworkInterfaces())
{
foreach (GatewayIPAddressInformation gatewayAddress in networkCard.GetIPProperties().GatewayAddresses)
{
string defaultGatewayAddress = gatewayAddress.Address.ToString();
}
}
Retrieve DomainName using javascript
1. document.domain
2. window.location.hostname
The entry 'KEY' has already been added : .Net
To resolve this issue
Add
in your web.config like below code.
Converting Class Array to DataTable : C#
private static void FillData(PropertyInfo[] propertyInfo, DataTable dataTable, Object objectSource)
{
DataRow dataRow = dataTable.NewRow();
foreach (PropertyInfo pi in propertyInfo)
dataRow[pi.Name] = pi.GetValue(objectSource, null);
dataTable.Rows.Add(dataRow);
}
private static DataTable CreateDataTable(PropertyInfo[] propertyInfo)
{
DataTable dataTable = new DataTable();
DataColumn dataColumn = null;
foreach (PropertyInfo pi in propertyInfo)
{
dataColumn = new DataColumn();
dataColumn.ColumnName = pi.Name;
dataColumn.DataType = pi.PropertyType;
dataTable.Columns.Add(dataColumn);
}
return dataTable;
}
public static DataTable ConvertToDataTable(Object[] array)
{
PropertyInfo[] propertyInfo = array.GetType().GetElementType().GetProperties();
DataTable dataTable = CreateDataTable(propertyInfo);
if (array.Length != 0)
{
foreach (object o in array)
FillData(propertyInfo, dataTable, o);
}
return dataTable;
}
Ramdom Password Generation : Using C#
The following C# code will generate a random number without repeating character
public static string GetRandomPassword(int passwordLength)
{
string allowedChars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?~ABCDEFGHIJKLMNOPQRSTUVWXYZ^&";
string newPassword = "";
Random randomNumber = new Random();
for (int i = 0; i <>
{
int charIndex = randomNumber.Next(allowedChars.Length);
//Don't Allow Repetation of Characters
if (!newPassword.Contains(allowedChars[charIndex] + ""))
newPassword += allowedChars[charIndex];
else
i--;
}
return newPassword;
Print a number in Currency Format
To print a number in currency (dollar - $) format, use the following code
Create an object for NumberFormatInfo Class in the class level
protected System.Globalization.NumberFormatInfo nfi = null;
Then create an instance for the object and assign the CurrencySymbol and CurrencyDecimalDigits as below in the Page_Load Event
nfi = new System.Globalization.NumberFormatInfo();
nfi.CurrencyDecimalDigits = 0;
nfi.CurrencySymbol = "$";
In the ASPX page to print the number in currency format use the following code
<%#String.Format(nfi,"{0:C}", Convert.ToDecimal (DataBinder.Eval(Container.DataItem,"Price"))) %>