2013/07/15

Update app.config settings at runtime

using System.Configuration;

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;

appSettings["WorkingMinutes"].Value = numericUpDownWorkingPeriod.Value.ToString();
appSettings["RestMinutes"].Value = numericUpDownRestPeriod.Value.ToString();
appSettings["PhotoPath"].Value = textBoxPhotoPath.Text.Trim();

config.Save();
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);


Same as above, different style.
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;

appSettings["key1"].Value = "value1";
appSettings["key2"].Value = "value2";

config.Save();
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);

LINQ: Ways to have two filters.

string[] source = new string[5] { "12345", "1234", "45678", "6789", "6" };

// Method 1
var result1 = from o in source
where o.Length > 4 && o.StartsWith("1")
select o;
// Method 2
var result2 = from o in source
where (o.Length > 4 & o.StartsWith("1"))
select o;
// Method 3
var result3 = from o in source
where o.Length > 4
where o.StartsWith("1")
select o;
// Method 4
var result4 = source.Where(o => o.Length > 4).Where(o => o.StartsWith("1"));

Different style:
string[] source = new string[5] { "12345""1234""45678""6789""6" };

// Method 1
var result1 = from o in source
              where o.Length > 4 && o.StartsWith("1")
              select o;
// Method 2
var result2 = from o in source
              where (o.Length > 4 & o.StartsWith("1"))
              select o;
// Method 3
var result3 = from o in source
              where o.Length > 4
              where o.StartsWith("1")
              select o;
// Method 4
var result4 = source.Where(o => o.Length > 4).Where(o => o.StartsWith("1"));

2013/05/14

[Download] FxCop 10.0

I don't know why but when I follow the instruction of downloading the FxCop 10.0, I always got the FxCop 1.36 from the C:\Program Files\Microsoft SDKs\Windows\v7.0A\FXCop folder. After a long search, I found that someone kindly share the setup file at here. Thanks to him!

I put a backup copy here. :)

[TextBox, DataGridView, BindingSource] Search as you type

I created my own search as you type feature by using TextBox, DataGridView, and BindingSource. It's very simple to implement. Here is the code:

PS: I already have a TextBox & a DataGridView on the form.

public partial class LookupTable : Form
{
  // This will be the data source of the DataGridView.
  BindingSource oBindingSource = new BindingSource();
  CallerDTO caller = null;
 
  public LookupTable(DataTable sourceData, CallerDTO sourceDTO)
  {
    InitializeComponent();
    caller = sourceDTO;
 
    // Assign the DataTable to our BindingSource object.
    oBindingSource.DataSource = sourceData;
  }
 
  private void LookupTable_Load(object sender, EventArgs e)
  {
    toolTipFind.SetToolTip(textBoxFind, "You can search by Name or by Code.");
 
    // Assign the BindingSource object to the DataGridView.
    dataGridViewResult.DataSource = oBindingSource;
  }
 
  private void textBoxFind_TextChanged(object sender, EventArgs e)
  {
    TextBox oTextBox = sender as TextBox;
 
    // Here is the key of the whole "Search As You Type" function.
    oBindingSource.Filter = "NAME LIKE '%" + oTextBox.Text + "%' OR CODE LIKE '%" + oTextBox.Text + "%'";
  }
 
  private void dataGridViewResult_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
  {
    // Here I need to send back the value that user clicked on.
    caller.StringResult = dataGridViewResult.Rows[e.RowIndex].Cells[1].Value.ToString();
 
    // This is just a public method for me to do something after user clicking.
    caller.CallerLoad();
    this.Close();
  }
}

Result:

2013/03/29

Make Oracle Instant Client work

I installed Oracle Instant Client like others did, extract it at a folder, add the folder to path, change the folder's privilege, add some roles to that folder and have fully access... etc. None of them works.

So the ultimate way to make it work that I found is:
copy the following two files to your application folder "oci.dll" & "oraociei11.dll" and it will work. At least it works for me in my case.

PS: The "oraociei11.dll" has around 124MB of size, and this will make your application become a big monster. I hope you will never have to use this way.