Archive for July, 2009

Thycotic Design Pattern Articles

Great bunch of articles on different design patterns up on the Thycotic Blog;

No Comments

Syndicating to RSS using the built in .net SyndicationFeed classes

My boss recently showed me a pretty handy bunch of classes for generating RSS and ATOM feeds, all built into dot net.

Start by adding a new reference to your project; System.Services.Web.

The SyndicationFeed class holds all the data about your feed and lets you spit everything out in a number of different formats (RSS2.0, ATOM1.0)- each item in the feed is held in a generic list of SyndicationItem objects. As I work for a publishers, I was creating an RSS feed of coming soon books so I added an extension method to my book class- ToSyndicationItem();

public static SyndicationItem ToSyndicationItem(this Book book)
{
    return new SyndicationItem(book.CoverTitle + " " + book.Subtitle, book.Description, new Uri(book.GetURL()))
    {
        Summary = new TextSyndicationContent(book.DescriptionShort, TextSyndicationContentKind.Plaintext),
        Id = book.Id.ToString(),
        LastUpdatedTime = book.InsertedDate
    };
}

This allows me to use all the standard select methods I already in my book class, to also populate my RSS feed. So to create the feed all I need do in my RSS.ashx handler file is;

List<Book> books = Book.GetBookPublishedBetween(DateTime.Now,DateTime.Now.AddDays( int.Parse( GroupConstants.ComingSoonMaxDays)));
List<SyndicationItem> syndicationItems = new List<SyndicationItem>();
foreach (Book book in books)
    syndicationItems.Add(book.ToSyndicationItem());

feed = new SyndicationFeed(syndicationItems)
{
    Title = new TextSyndicationContent("Coming Soon Titles"),
    Description = new TextSyndicationContent("Forthcoming publications."),
    BaseUri = new Uri(LinkHelper.GetBaseUrl())
};

var output = new StringWriter();
var writer = new XmlTextWriter(output);

new Rss20FeedFormatter(feed).WriteTo(writer);

context.Response.ContentType = "application/rss+xml";
context.Response.Write(output.ToString());

, , ,

1 Comment

RSS XSLT

Simple piece of XSL to format an RSS Feed;

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html"></xsl:output>
    <xsl:template match="rss/channel/item">
    <li>
      <a href="{link}" title="{title}">
        <xsl:value-of select="title" />
      </a>
      <p>
        <xsl:value-of select="description" />
      </p>
    </li>
  </xsl:template>
</xsl:stylesheet>

No Comments

Using URLEncode, URLDecode and other Http functions from a non Web file

I found a class which allows you to access various web based functions which you can generally reference using the Server class when working on a web page- If you’re building a class or working on a windows app and find you need access to any of those utility functions you can do so using HttpUtility ;

http://msdn.microsoft.com/en-us/library/system.web.httputility.aspx

No Comments

WatiN Testing Tools

Found an awesome looking tool for testing ASP.net website which allows you to test the front end interactions of your site.  All integrates with dotnet- hope to use it in my next project so will post again then…  In the mean time have a butchers yourself; WatiN Home.

No Comments

Consuming RSS Feeds using ASP.net controls

Real simple example, using an XmlDataSource and my new super best friend, the ListView control;

    <asp:ListView ID="RSSList" runat="server" DataSourceID="RSSData">
        <LayoutTemplate>
            <ul>
                <li id="itemPlaceholder" runat="server" />
            </ul>
        </LayoutTemplate>
        <ItemTemplate>
            <li><h2>
                <%#XPath("title") %>
            </h2>
            
            <%#XPath("author") %>
            <a href='<%#XPath("link") %>' title=''>View Original Post</a>
            </li>
        </ItemTemplate>
    </asp:ListView>
    <asp:XmlDataSource 
        ID="RSSData" 
        runat="server" 
        DataFile="http://www.shawson.co.uk/codeblog/feed/"
        XPath="rss/channel/item">
    </asp:XmlDataSource>

No Comments

Conditional Order-By clause in SQL 2005

In SQL Server 2005 onwards you can have conditional Order By clauses in your stored proc’s- for example

SELECT EmployeeID, SalariedFlag
FROM HumanResources.Employee
ORDER BY CASE SalariedFlag WHEN 1 THEN EmployeeID END DESC
        ,CASE WHEN SalariedFlag = 0 THEN EmployeeID END;
GO

CASE (Transact-SQL).

No Comments

How To Display Hierarchical Data Using Nested Repeater Controls and Visual C# .NET

Just found a piece of functionality which I’ve never noticed before, for populating nested Repeaters, without doing so in the code behind with a “OnDataBound” Event.

It’s as simple as just setting the DataSource on the child repeater, to a property of the parent repeaters DataItem.  For example;

<asp:Repeater ID="BookRepeater" runat="server" onitemdatabound="bibRepeater_ItemDataBound">
    <HeaderTemplate><ul></HeaderTemplate>
    <ItemTemplate>
      <li>
         <h1><%# DataBinder.Eval(Container.DataItem, "BookTitle")%></h1>
         <asp:Repeater ID="AuthorsRepeater" runat="server" DataSource='<%# ((Book)Container.DataItem).Authors %>'>
         <HeaderTemplate><h5>Author: </HeaderTemplate>
             <ItemTemplate>
                 <a href='/AuthorDetails.aspx?id=<%# DataBinder.Eval(Container.DataItem, "Id")%>' title="<%# DataBinder.Eval(Container.DataItem, "FirstName")%> <%# DataBinder.Eval(Container.DataItem, "LastName")%>"><%# DataBinder.Eval(Container.DataItem, "FirstName")%> <%# DataBinder.Eval(Container.DataItem, "LastName")%></a>
             </ItemTemplate>
             <SeparatorTemplate>, </SeparatorTemplate>
             <FooterTemplate></h5></FooterTemplate>
         </asp:Repeater>
      </li>
    </ItemTemplate>
    <FooterTemplate></ul></FooterTemplate>
</asp:Repeater>

You may need to add a declaration to the namespace in which your entities reside, at the top of your aspx file, like this;

<%@ Import Namespace="Hachette.Entities.Orchard" %>

full details on MSDN; How To Display Hierarchical Data by Using Nested Repeater Controls and Visual C# .NET.

No Comments

ASP.Net Role & Membership Providers (Under IIS7) – DOESN’T Work!

Ok well, it does, but I’ve had a very stressful afternoon at it’s merciless hands!  Just trying to get the ASP.Net roles & membership working on asp.net 3.5, using IIS7.  I created the project and clicked “ASP.Net Confguration” from the project menu, but no working providers existed.

I defined some custom providers and nothing would get picked up.

It turns out, under IIS you need to define a connection string with a specific name to make this work–

 <connectionStrings>
    <clear/>
    <add name="LocalSqlServer" connectionString="your real connection string goes here!" />
  </connectionStrings>

So we’re clearing out the default inherited connection strings then overriding the default LocalSqlServer connection to have the correct connection to our real database, as this appears to be the connection string it defaults to.

Save the web.config file, the fire up the “ASP.net configuration” site again and you should be away!

Thanks to Mr Ledger for channelling the awesome power of google through himself to yield that nugget of joy and relief!

No Comments

Collation issues setting up ASP.Net membership tables

Setup failed.

Exception:
An error occurred during the execution of the SQL file ‘InstallRoles.sql’. The SQL error number is 446 and the SqlException message is: Cannot resolve collation conflict for equal to operation.
Cannot resolve collation conflict for equal to operation.

I recently had this problem while trying to setup the roles & memberships gubbins on a database using the aspnet_regsql tool on the v2 .net framework.

This turned out to be because this was a SQL 2005 database which we had imported from a third party, produced on SQL2005.  The SQL2005 database had a collaction of SQL_Latin1_General_CP1_CI_AS while our sql 2000 box’s default collection was Latin1_General_CI_AS.

The fix was simple; having backed up our database (!) simply run this command;

Alter Database testdatabase Collate Latin1_General_CI_AS

This converted the database to the same Latin1_General_CI_AS collation as the server’s default, and allowed the aspnet_regsql to do it’s job unhindered!

remove_modeNOTE: If you’ve ran aspnet_regsql once already before correcting your collation (which i would imagine you probably did!), make sure you run it again in remove mode before running a final time in install mode, as the first time it runs it will bomb out after creating some of the tables already leaving you with tables in your database still using the old collation!

No Comments