Emil’s Blog

Programming Windows, .Net, EPiServer and whatnot…

[Powered by WordPress.]

November 19, 2007

Retrieving the database id

by @ 12:22. Filed under SQL Server

It can sometimes be useful to know the id of a given database, e.g. for filtering events when profiling. Here are a few ways to do that:

-- Current database
SELECT db_id()

-- Named database
SELECT db_id('Northwind')

-- All databases
USE master
SELECT name, dbid FROM sysdatabases

November 15, 2007

Signalling events from a class

by @ 16:23. Filed under .Net programming

For easy reference, here's how to create an event in a class that listeners can react to:

class DeleteSubOrderEventArgs : EventArgs
{
    public int SubOrderId;
    public DeleteSubOrderEventArgs(int subOrderId)
    {
        SubOrderId = subOrderId;
    }
}

class Foobar
{
    public delegate void DeleteEventDelegate(object sender, DeleteSubOrderEventArgs e);
    public event DeleteEventDelegate DeleteSubOrderClick;
   
    protected void DeleteButton_Click(object sender, ImageClickEventArgs e)
    {
        // Signal to listeners
        if (DeleteSubOrderClick != null)
        {
            DeleteSubOrderClick(this, new DeleteSubOrderEventArgs(SubOrder.SubOrderId));
        }
    }
}

November 14, 2007

How to embed an Xslt-file in an assembly

by @ 9:07. Filed under .Net programming

Problem
How do I embed an Xslt file into an assembly so that I won't have to deploy the file together with the assembly, set configuration options to refer to the file, etc?

Solution

  1. Create a resource (.resx) file in the project
  2. In the resource designer, click "Add Resource" and choose "Add Existing File...". Select the Xslt file.
  3. Give the new resource a describing name, such as "FilterContentXslt". The contents of the Xslt file will be available in a string property with this name in the Resource manager.
  4. Code that performs the transformation:
    // Parse the content into an XmlDocument
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xmlValue);

    // Retrieve the embedded resource containing the XSLT transform
    XmlDocument xsltDoc = new XmlDocument();
    xsltDoc.LoadXml(Resources.FilterContentXslt);

    XslCompiledTransform trans = new XslCompiledTransform();
    trans.Load(xsltDoc);

    // Perform the transformation
    StringWriter writer = new StringWriter();
    trans.Transform(doc, writer);
    string newXmlValue = writer.ToString();

Simple, and it works.

/Emil

[powered by WordPress.]

jour·nal n. A personal record of occurrences, experiences, and reflections kept on a regular basis; a diary.

Internal links:

Categories:

Search blog:

Archives:

November 2007
M T W T F S S
« Sep   Dec »
 1234
567891011
12131415161718
19202122232425
2627282930  


View Emil Åström's profile on LinkedIn

General links:

I read:

Visitors

Recent Comments

Spam caught

Other:

Clicky Web Analytics

36 queries. 0.939 seconds