Thursday, May 2, 2013

C# ComboBox using DataTable

Load Combobox Using DataTable.
Make an OleDbConnection.
Create DataTable from OleDbDataReader.

Sample code:
 
//include this
using System.Data.OleDb;

//declare 
OleDbConnection OCON = null;
OleDbDataReader ODR = null;
//create connection
OCON = new OleDbConnection(@"Provider=SQLOLEDB;Data Source=192.168.0.1;Initial Catalog=TESTDATABASE;User ID=TESTUSER;Password=TESTPASS;");

try{
    //open connection
    OCON.Open();            
    
    //create command    
    OleDbCommand OCMD = null;        
    OCMD.CommandText = "SELECT ID, DESCRIPTION FROM TABLE";
    //execute command
    ODR = OCMD.ExecuteReader();
    
    //load datareader to datatable       
    DataTable DT = new DataTable();
    DT.Load(ODR);

    //attach datatable to combobox
    comboBox1.DisplayMember = "DESCRIPTION";
    comboBox1.ValueMember = "ID";
    comboBox1.DataSource = DT;

    //close connection
    OCON.Close();
}catch(OleDbException ex){
    MessageBox.Show(ex.ToString());
}
Additional Reading at :
  1. MSDN OleDb
  2. MSDN ComboBox

Wednesday, May 1, 2013

C# string ToTitleCase. Capitalize the first letters of all words.

Convert string to title case. Capitalize the first letters of all words.


Sample code:
 
//include this
using System.Globalization;

//how to convert from uppercase string to titlecase
string title = "THE TITLE STRING";

TextInfo ti = CultureInfo.CurrentCulture.TextInfo;

title = ti.ToTitleCase(title.ToLower());

//result : "The Title String"
Additional Reading at :
  1. MSDN ToTitleCase

C# DateTimePicker CustomFormat Month and Year

Using DateTimePicker to select month and year only.
//to configure datetimepicker for month and year.

dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MMM-yyyy";
dateTimePicker1.ShowUpDown = true;

//to get value of the datetimepicker.
dateTimePicker1.Value.ToString("yyyy-MM");


Additional Reading at :
  1. MSDN DateTimePicker
  2. MSDN CustomFormat
  3. MSDN ShowUpDown