Saturday 28 April 2012

Firebird Classic, SuperClassic and Superserver characteristics

Firebird Classic, SuperClassic and Superserver characteristics

Running several Firebird Instances per Server Machine

http://www.destructor.de/firebird/multiinstance.htm

Friday 27 April 2012

Reading and Writing XML Files

To create or manipulate XML files, you must use the classes and methods inside the System.XML namespace of the .NET Framework.

Writing XML FilesWe can use the XmlWriter class to write XML files. It allows you to write XML text into a stream and then save it into an xml file.

using System.Xml;

public class Program
{
    public static void Main()
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;

        XmlWriter writer = XmlWriter.Create("Products.xml", settings);
        writer.WriteStartDocument();
        writer.WriteComment("This file is generated by the program.");
        writer.WriteStartElement("Product");
        writer.WriteAttributeString("ID", "001");
        writer.WriteAttributeString("Name", "Soap");
        writer.WriteElementString("Price", "10.00");
        writer.WriteStartElement("OtherDetails");
        writer.WriteElementString("BrandName", "X Soap");
        writer.WriteElementString("Manufacturer", "X Company");
        writer.WriteEndElement();
        writer.WriteEndDocument();

        writer.Flush();
        writer.Close();
    }
}


We first import the System.Xml namespace. We use the XmlWriterSettings class to create a setting for the XmlWriter that we will use. We dictated the program to use proper indention when writing the xml by using this settings. We then create an XmlWriter object by calling the XmlWriter.Create method and supplying the filename and the XmlWriterSettings object.

Using the XmlWriter class, we can write our xml. We first use the XmlWriter.WriteStartDocument() method which writes the xml declaration you find on the top of most xml files. Next we create a sample comment using teh XmlWriter.WriteComment() method. To write an Xml Element, we use the XmlWriter.WriteStartElement() and supply the name of the element as the argument. The XmlWriter.WriteStartElement() must be paired with XmlWriter.WriteEndElement which writes the closing tag of that element. We now create some attributes for the element using the XmlWriter.WriteAttributeString() method and supply the name and the value for that attribute.

We used the XmlWriter.WriteElementString() method with a name and a value as the arguments to write an element that is wrapping a value. We then nested another element inside the Products element by writing one more set of XmlWriter.WriteStartElement() and XmlWriter.WriteEndElement() . Inside it, we add two more elements that contain values. To mark the end of the document, we simly call the XmlWriter.WriteEndDocument() method.
Finally, we used the XmlWriter.Flush() method to clean the contents of the stream and the XmlWriter.Close() method to save the file and stop the program from using it.
The above code will produce the following XML file contents:

<?xml version="1.0" encoding="utf-8"?>
<!--This file is generated by the program.-->
<Product ID="001" Name="Soap">
  <Price>10.00</Price>
  <OtherDetails>
    <BrandName>X Soap</BrandName>
    <Manufacturer>X Company</Manufacturer>
  </OtherDetails>
</Product>


Reading XML Files
To read xml files, we can use the XmlReader class. We will use the XML file we created earlier to assign the values into variables. Note that we will simply use variables for storing the values from the XML. A better approach is by creating a Products class the follows the heirarchy of the XML file. The following program demonstrates the use of XmlReader class.

using System;
using System.Xml;

public class Program
{
    public static void Main()
    {
        XmlReader reader = XmlReader.Create("Products.xml");

        while (reader.Read())
        {
            if (reader.NodeType == XmlNodeType.Element
                && reader.Name == "Product")
            {
                Console.WriteLine("ID = " + reader.GetAttribute(0));
                Console.WriteLine("Name = " + reader.GetAttribute(1));

                while (reader.NodeType != XmlNodeType.EndElement)
                {
                    reader.Read();
                    if (reader.Name == "Price")
                    {
                        while (reader.NodeType != XmlNodeType.EndElement)
                        {
                            reader.Read();
                            if (reader.NodeType == XmlNodeType.Text)
                            {
                                Console.WriteLine("Price = {0:C}", Double.Parse(reader.Value));
                            }
                        }

                        reader.Read();
                    } //end if
                    if (reader.Name == "OtherDetails")
                    {
                        while (reader.NodeType != XmlNodeType.EndElement)
                        {
                            reader.Read();
                            if (reader.Name == "BrandName")
                            {
                                while (reader.NodeType != XmlNodeType.EndElement)
                                {
                                    reader.Read();
                                    if (reader.NodeType == XmlNodeType.Text)
                                    {
                                        Console.WriteLine("Brand Name = " + reader.Value);
                                    }
                                }
                                reader.Read();
                            } //end if

                            if (reader.Name == "Manufacturer")
                            {
                                while (reader.NodeType != XmlNodeType.EndElement)
                                {
                                    reader.Read();
                                    if (reader.NodeType == XmlNodeType.Text)
                                    {
                                        Console.WriteLine("Manufacturer = " + reader.Value);
                                    }
                                }

                            } //end if
                        }
                    } //end if
                } //end while
            } //end if

        } //end while
    }
}


Output:
ID = 001
Name = Soap
Price = $10.00
Brand Name = X Soap
Manufacturer = X Company


There are other ways to read Xml files but this tutorial will focus on using the XmlReader class. First, we create an XmlReader object using the static method Create and passing the filename of the XML file that will be read.

XmlReader reader = XmlReader.Create("Products.xml");

We now enter a loop that will read each node in the xml file including the whitespaces. We use the XmlReader.Read() that returns true if there are more nodes to read or false if there is no more or if it reaches the end of the file.
We first look at the Product element. The first condition checks if the current node read by the reader is an element and has a name of "Product". The XmlNodeTpe enumeration list all the possible node types including whitespaces, text and comments. We then show the values of the attributes of the product element:

Console.WriteLine("ID = " + reader.GetAttribute(0));
Console.WriteLine("Name = " + reader.GetAttribute(1));

The GetAttribute() method gets the values of the attribute of an element. The number indicates what attribute you are getting. 0 indicates the first attribute and 1 indicates the second attribute.
You will now encounter a series of nested while and if statements. The first while loop will search for the Price element. The Read() method will go to the next node while it is not an end element. If the type of the node is a text, we display it on the screen.
Basically, reading xml files using XmlReader requires you to look carefully at the structure of your XML.

Thursday 26 April 2012

Select XML Nodes by Attribute Value

Select XML Nodes by Attribute Value [C#]


This example shows how to select nodes from XML document by attribute value. Use method XmlNode.Selec­tNodes to get list of nodes selected by the XPath expression. Suppose we have this XML file.
[XML]
<Names>
    <Name type="M">John</Name>
    <Name type="F">Susan</Name>
    <Name type="M">David</Name>
</Names>

To get all name nodes use XPath expression /Names/Name. To get only male names (to select all nodes with specific XML attribute) use XPath expression /Names/Name[@type='M'].

[C#]
XmlDocument xml = new XmlDocument();
xml.LoadXml(str);  // suppose that str string contains "<Names>...</Names>"
XmlNodeList xnList = xml.SelectNodes("/Names/Name[@type='M']");
foreach (XmlNode xn in xnList)
{
  Console.WriteLine(xn.InnerText);
}

The output is:
John
David

Saturday 21 April 2012

Adding a simple text watermark to an image

if (pictureBox1.Image != null)
{
    //Create image.
    Image tmp = pictureBox1.Image;
    //Create graphics object for alteration.
    Graphics g = Graphics.FromImage(tmp);

    //Create string to draw.
    String wmString = "Code Project";
    //Create font and brush.
    Font wmFont = new Font("Trebuchet MS", 10);
    SolidBrush wmBrush = new SolidBrush(Color.Black);
    //Create point for upper-left corner of drawing.
    PointF wmPoint = new PointF(10.0F, 10.0F);
    //Draw string to image.
    g.DrawString(wmString, wmFont, wmBrush, wmPoint);
    //Load the new image to picturebox
    pictureBox1.Image= tmp;       
    //Release graphics object.
    g.Dispose();               
}

Friday 20 April 2012

.Net Windows Form Event Sequence

The events in the lifecycle of a Form from the time it is launched to the time it is closed are listed below:

  • Move: This event occurs when the form is moved. Although by default, when a form is instantiated and launched, the user does not move it, yet this event is triggered before the Load event occurs.
  • Load: This event occurs before a form is displayed for the first time.
  • VisibleChanged: This event occurs when the Visible property value changes.
  • Activated: This event occurs when the form is activated in code or by the user.
  • Shown: This event occurs whenever the form is first displayed. 
  • Paint: This event occurs when the control is redrawn.
  • Deactivate: This event occurs when the form loses focus and is not the active form.
  • Closing: This event occurs when the form is closing.
  • Closed: This event occurs when the form is being closed.

Friday 6 April 2012

Validating Properties In A .Net Propertygrid Control


The easiest way to validate a certain property in a .Net propertygrid control is using the PropertyValueChanged event.The code bellow shows how to limit the valid range of Age property to be 1-150

using System.Windows.Forms;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //init person object
            Person person = new Person();
            person.FirstName = "George";
            person.Age = 33;
            propertyGrid.SelectedObject = person;
            propertyGrid.PropertyValueChanged+=  new PropertyValueChangedEventHandler( propertyGrid_PropertyValueChanged );
        }


        private void propertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            if (e.ChangedItem.Label == "Age" && !IsAgeValid((int)e.ChangedItem.Value) )
            {
                // the entered age value is wrong - show error message
                e.ChangedItem.PropertyDescriptor.SetValue( propertyGrid.SelectedObject, e.OldValue);
                MessageBox.Show("Wrong age", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }


        ///<summary>
        /// Determines whether age is valid
        /// </summary>
        /// <param name="age">The age.</param>
        /// <returns>
        /// <c>true</c> if is age valid ; otherwise, <c>false</c>.
        /// </returns>

        private static bool IsAgeValid( int age )
        {
            return ((age > 0) && (age < 150));
        }
    }
}