Wednesday, November 3, 2010

Adding a Text Writer Trace Listener via the Application Configuration File

When using the Trace class, a mechanism for recording messages that are sent to it are necessary. This is done by the use of listeners. The purpose of a listener is to collect store, and route tracing messages.

To send output to the listeners collection :

System.Diagnostics.Trace.WriteLine("Attempting to load form.");
The following example shows how to use a listener to send an output to a file:
System.Diagnostics.Trace.Listeners.Clear();
System.Diagnostics.Trace.Listeners.Add(
new System.Diagnostics.TextWriterTraceListener(@"C:\traceOutput.txt"));

The listener can also be configured by means of an application configuration file (App.config):

  <?xml version="1.0" encoding="utf-8" ?>
  <configuration>
    <system.diagnostics >                
      <trace autoflush="true" >
        <listeners>
          <clear />
            <add name="textWriterListener"
                 type="System.Diagnostics.TextWriterTraceListener"      
                 initializeData="C:\traceOutput.txt">      
            </add>           
        </listeners>                                 
      </trace >        
    </system.diagnostics>
  </configuration>

Exercise
1.       Create a new C# winforms application.
2.       Write a couple of trace statements in the form constructor.
   public SampleForm() {
System.Diagnostics.Trace.Write("Initializing Components...");
InitializeComponent();
System.Diagnostics.Trace.WriteLine("Done");
   }
3.       Add an application configuration file (app.config).
4.       Under configuration, add the following lines:
    <system.diagnostics >                   
      <trace autoflush="true" >
        <listeners>
          <clear />
            <add name="textWriterListener"
                 type="System.Diagnostics.TextWriterTraceListener"      
                 initializeData="C:\traceOutput.txt">           
          </add>       
        </listeners>                                     
      </trace >        
    </system.diagnostics>
5.       Run the application. The application will generate a C:\traceOutput.txt which contains the trace statements added in the form constructor.

Tuesday, October 5, 2010

Using the DateTimeFormatInfo to retrieve Culture Specific Date Values

This Blog entry discusses how to use the System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat object to access culture specific date values.

Retrieving Culture Specific Day Names
Access the DayNames property or AbbreviatedDayNames property to access a list of day names for the particular culture.
Example:

DateTimeFormatInfo d = Thread.CurrentThread.CurrentCulture.DateTimeFormat;
foreach (string abbreviatedDayName in d.AbbreviatedDayNames)
Console.WriteLine (abbreviatedDayName);

Retrieving Culture Specific Month Names
Access the MonthNames property or AbbreviatedMonthNames property to access a list of month names for the particular culture.
Example:

DateTimeFormatInfo d = Thread.CurrentThread.CurrentCulture.DateTimeFormat;
foreach (string abbreviatedMonthName in d.AbbreviatedMonthNames)
Console.WriteLine (abbreviatedDayName);

Play with it:

This tutorial allows you to retrieve culture specific date values in French:

1.       Set the CurrentCulture to French (Canada)
Thread.CurrentThread.CurrentCulture = new CultureInfo("FR-CA");

2.       Declare a DateTimeFormatInfo and set it to the CurrentThread’s DateTimeFormat property
DateTimeFormatInfo d = Thread.CurrentThread.CurrentCulture.DateTimeFormat;           

3.       Write the day names for French (Canada)
foreach (string dayName in d.DayNames)
Console.WriteLine(dayName);

4.       Write the month names for French (Canada)
foreach (string monthName in d.MonthNames )
Console.WriteLine(monthName);

Editing a Connection String through the DataLink properties screen

This Blog entry discusses how to edit a connection string using the Data Link Properties screen.

To edit a database connection string through the Data Link Properties screen, you would need the following references:
-          ADODB
-          MSDASC

Declare an object of type MSDASC.DataLinks and assign it a new instance.
Example:
MSDASC.DataLinks dataLink = new MSDASC.DataLinksClass();

Declare an ADODB Connection object.
Example:
ADODB._Connection connection;

Set  the connection object to a new ConnectionClass.
Example:
connection = new ADODB.ConnectionClass();

Set the connectionString property of the connection to the connectionString to be edited.
Example:
connection.ConnectionString = connectionString;


Call the PromptEdit method of the datalink and assign it to the ADODB connection and pass the connection as a reference parameter.
Example:
dataLink.PromptEdit(ref oConnection)

Access the connection string property of the connection object
return connection.ConnectionString.ToString();

Example Code:
MSDASC.DataLinks dataLinks = new MSDASC.DataLinksClass();
ADODB._Connection connection;
connection = new ADODB.ConnectionClass();
connection.ConnectionString = connectionString;
dataLink.PromptEdit(ref oConnection)
return connection.ConnectionString.ToString();

Obtain a Connection String through the Data Link Properties Screen

This Blog entry discusses how to obtain a connection string from users using the Data Link Properties screen.

To obtain a database connection string through the Data Link Properties screen, you would need the following references:
-          ADODB
-          MSDASC

Declare an object of type MSDASC.DataLinks and assign it a new instance.
Example:
MSDASC.DataLinks dataLink = new MSDASC.DataLinksClass();

Declare an ADODB Connection object.
Example:
ADODB._Connection connection;

Call the PromptNew method of the datalink and assign it to the ADODB connection. This will return an ADODB connection object.
Example:
connection = (ADODB._Connection)dataLinks.PromptNew();

Access the connection string property of the connection object
return connection.ConnectionString.ToString();

Example Code:
MSDASC.DataLinks dataLinks = new MSDASC.DataLinksClass();
ADODB._Connection connection;
connection = (ADODB._Connection)dataLinks.PromptNew();
return connection.ConnectionString.ToString();