Tuesday, February 14, 2012

Bind generic dictionary collection to combobox


Dictionary<string, string>  DicSearch;
DicSearch = new Dictionary<string, string>();
DicSearch.Add("1", "Mohit");
DicSearch.Add("2", "Agrawal");

cboSearch.DataSource  = new BindingSource(DicSearch, null);
cboSearch.DisplayMember = "value";
cboSearch.ValueMember = "key";
DicSearch = null;

Tuesday, February 7, 2012

Regular Expression


Regular Express class and validation done himself.

SqlCommand Query based on parameters


SqlCommand command = new SqlCommand();
command.CommandText = "SELECT * FROM Emplyoee emp WHERE  emp.Name=@EmpName";
command.Parameters.Add(new SqlParameter(" @EmpName", 50));

// Execute the SQL Server command...
SqlDataReader reader = command.ExecuteReader();
DataTable tblemplyee = new DataTable();
tblemplyee.Load(reader);

foreach (DataRow rowProduct in  tblemplyee.Rows)
{
    // Use the data...
}


NOTE: This is a good way to block SQL injection

"" vs String.Empty

String.Empty is faster than "" because it doesn't create a new string object instance. Creating new instances brings penalties on both execution speed and memory usage.

Example:
// Slowest
str1 == "";
// Slow
str1.Equals(" ") == false
//Fast
str1 == String.Empty
// Fastest
str1.Equals(String.Empty) == false

Hash Table in C#

 The Hashtable is a non-generic collection and used to store the key/value pairs based on the hash code of the key. Key will be used to acce...