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

Friday, April 26, 2013

C# Get Application Version via runtime

Setting Custom version number at project properties under application assembly information

Get application version via runtime code:

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

C# Read Registry Method

To get value from registry. Sample code:
 
//include this
//for registry read write
using Microsoft.Win32; 

public string ReadRegistryValue(string KeyName)
{
            
    // Opening the registry key
    RegistryKey key = Registry.LocalMachine;
    // Open a subKey as read-only
    RegistryKey subkey = key.OpenSubKey(@"SOFTWARE\SKYPE\PHONE");
            
    // If the RegistrySubKey doesn't exist -> (null)
    if (subkey == null)
    {
        return null;
    }
    else
    {
        try
        {
            //return value
            return (string)subkey.GetValue(KeyName);
        }
        catch (Exception e)
        {
            //display error
            MessageBox.Show("Reading registry Exception " + e.Message.ToString());
            return null;
        }
    }
}

//method usage
 MessageBox.Show(ReadRegistryValue("SkypePath"));
Additional Reading at :
  1. MSDN RegistryKey