Archive

Posts Tagged ‘C#’

Simple custom event handling

February 17th, 2009

The event model in C# is based on idea of publisher and subscribers. Each publisher can have more subscribers subscribed to each event. Publisher will do some logic and publish an event to all subscribers. Subscribers will do their logic reacting to raised event. In C#, any object can publish a set of events to which other applications can subscribe. When the publishing class raises an event, all the subscribed applications are notified. Example of this behaviour can be that the application will subscribe to event which will be raised from database connection control when the connection is lost. Main application will know, that this happened and can react on that event –reconnect, show alert message, etc.

Conventions

The following important conventions are used with events:

  • Event Handlers in the .NET Framework return void and take two parameters.
  • The first paramter is the source of the event; that is the publishing object.
  • The second parameter is an object derived from EventArgs.
  • Events are properties of the class publishing the event.
  • The keyword event controls how the event property is accessed by the subscribing classes.

Example

Lets say that we have Product class which has Name property. When the name is changed we want ot change this name in Label as well. We don’t know from where this name can be changed. We will “convert” product class into event publisher and the window where is the label will subscribe to this event.

First step will be to create the event and method which will fire this event.


public class Product
{
  public string Name { get; set; }
  // Delegate
  public delegate void PropertyChangeHandler (object sender,  EventArgs data);
  // The event
  public event PropertyChangeHandler PropertyChange;
  // The method which fires the Event
  protected void OnPropertyChange (object sender,  EventArgs data)
  {
    // Check if there are any Subscribers
    if (PropertyChange!= null)
    {
      // Call the Event
      PropertyChange (this, data);
    }
  }
}

We created delegate which encapsulates any method that takes these attributes, this delegate must be implemented by all subscribers. Now we need one small change to the class. That will be to fire the event, when Name property is changed. It can be nice to pass information about this change to subscribers with what have been changed and to have old value and new value. For this we will create PropertyChangeEventArgs class which will be derived from EventArgs.


public class PropertyChangeEventArgs : EventArgs
{
  public string PropertyName { get; internal set; }
  public object OldValue { get; internal set; }
  public object NewValue { get; internal set; }
  public PropertyChangeEventArgs(string propertyName, object oldValue, object newValue)
  {
    this.PropertyName = propertyName;
    this.OldValue = oldValue;
    this.NewValue = newValue;
  }
}

public class Product
{
  private string name;
  public string Name {
    get
    {
      this.name;
    }
    set
    {
      Object old = this.name;
      this.name = value;
      OnPropertyChange(this, new PropertyChangeEventArgs(“Name”, old, value));
    }
  }

  // Delegate
  public delegate void PropertyChangeHandler (object sender,  PropertyChangeEventArgs data);
  // The event
  public event PropertyChangeHandler PropertyChange;
  // The method which fires the Event
  protected void OnPropertyChange (object sender,  PropertyChangeEventArgs data)
  {
    // Check if there are any Subscribers
    if (PropertyChange!= null)
    {
      // Call the Event
      PropertyChange (this, data);
    }
  }
}

Now we are done with the Product class (publisher) and can subscribe to the OnPropertyChange event.


protected void Page_Init(object sender, EventArgs e)
{
  this.Product = new Product();
  this.Product.OnPropertyChange += new Product.PropertyChangeHandler(PropertyHasChanged);
}
protected void Page_Load (object sender, EventArgs e)
{
  this.Product.Name = "New name";
}

public void PropertyHasChanged (object sender,  PropertyChangeEventArgs data)
{
  if(data.PropertyName == “Name”)
  {
    this.ProductLabel.Text = (string)data.NewValue + " was " + (string)data.OldValue;
  }
}

In the Init event we created product instance and subscribe to OnPropertyChange event of Product class. In Load phase we changed name of that product. This operation fired PropertyChange and this was send to all subscribers. This calls PropertyHasChanged method with all informations.

Donwload example: Download

C# , ,

Storing Database Connection String in Web.Config

October 9th, 2008

Today I found, that storing connection strings were little bit changed between .NET 2.0 and .NET 3.5.

Here is an example of how to store a database connection string in the application’s Web.Config file for .NET 2.0. The code for accessing the connection string is located in the C# code below.

.NET 2.0


<appSettings>
<add key="ConnectionString" value="server=localhost;database=TestDB;uid=sa;password=secret;" />
</appSettings>

And here is the C# code how to get it.


using System.Configuration;

public class TestGetConnection
{
  public TestGetConnection()
  {
    try
    {
      // Get connection string from Web.Config
      string strConnection = ConfigurationSettings.AppSettings("ConnectionString");
    }
  }
}

.NET 3.5

ConfigurationSettings.AppSettings is now deprecated.


<connectionStrings>
  <add name="YourConnectionString" connectionString="server=localhost;database=TestDB;uid=sa;password=secret;" providerName="System.Data.SqlClient" />
  <add name="NewString" connectionString="server=localhost;database=TestDB;uid=sa;password=secret;" providerName="System.Data.SqlClient" />
</connectionStrings>

And here is the C# code how to get it.


using System.Configuration;

public class TestGetConnection
{
  public TestGetConnection()
  {
    try
    {
      // Get connection string from Web.Config
      string strConnection = ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString;
    }
  }
}

You can use <remove /> to delete connection setting from web.config.


<connectionStrings>
  <remove name="YourConnectionString"/>
  <add name="YourConnectionString" connectionString="server=localhost;database=TestDB;uid=sa;password=secret;" providerName="System.Data.SqlClient" />
</connectionStrings>

Connection String Syntax Notes

  • Values may be delimited by single or double quotes, (for example, name=’value’ or name=”value”). Either single or double quotes may be used within a connection string by using the other delimiter, for example, name=”value’s” or name=’value”s’,but not name=’value’s’ or name=”"value”". The value type is irrelevant.
  • All blank characters, except those placed within a value or within quotes, are ignored.
  • Keyword value pairs must be separated by a semicolon ( ; ). If a semicolon is part of a value, it also must be delimited by quotes.
  • Names are not case sensitive. If a given name occurs more than once in the connection string, the value associated with the last occurence is used.
  • No escape sequences are supported.

If you need to connect to SQL Express, you shold use connection string with specifying the file you want to attach.


connectionString="Server=.\SQLExpress;AttachDbFilename=c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\SpamTestimonial.mdf;Database=SpamTestimonial;Trusted_Connection=Yes;"

If you need to find all possible connection string, go to web page dedicated to them for more databases than you can image. http://www.connectionstrings.com/

.NET , , , ,

Stripping span tag from WebControl

September 15th, 2008

When I started developing web controls which are created by CreateChildControls method i run into problem that this control is surrounded by span tag.


[ToolboxData("<{0}:ExtendedLabel runat=\"server\" Text=\"Label\" />")]
public class ExtendedLabel : WebControl, INamingContainer
{
  protected override void CreateChildControls()
  {
    Label label = new Label();
    label.Text = this.Text;
    this.Controls.Add (label);
  }
}

This example will product not so nice HTML code:


<span id=”ExtendedLabel”>
  <span name=”ExtendedLabel:Label”>Test string</span>
</span>

The first label come from RenderBeginTag method which contains by default span tag. What you can do is to override both RenderBeginTag and RenderEndTag.


protected override void RenderBeginTag(HtmlTextWriter writer)
{
  writer.Text = writer.Text + "<div id=\"myTag\">";
}
protected override void RenderBeginTag(HtmlTextWriter writer)
{
  writer.Text = writer.Text + "</div>";
}

Or remove this tag completely by overriding Render method.

Original:


protected override void Render(HtmlTextWriter writer)
{
  this.RenderBeginTag(writer);
  this.RenderContents(writer);
  this.RenderEndTag(writer);
}

New:


protected override void Render(HtmlTextWriter writer)
{
  this.RenderContents(writer);
}

In these examples you have full control over behavior of the control. These is another possibility how to change or remove the outlining tag. You can extend your web control from another class.


protected ExtendedLabel () : base()
{ }

.NET, C# , , ,