Click here to Skip to main content
12,998,892 members (68,429 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 2 Jul 2007

Creating an RSS feed for your website from your SQL database

, 2 Jul 2007
Rate this:
Please Sign up or sign in to vote.
How to create an RSS feed for your website from your SQL database.


Some time ago, I was thinking about starting my own business and registered a domain etc., but then I was made an offer I couldn't refuse, and so I put these plans on hold. I still had my domain ( and was paying for hosting, so I thought I would do something, and blogging seemed to be the natural choice. I therefore wrote myself a blog system and started using it. Shortly thereafter, one of my readers asked me where my RSS feed was, and I realised that while I had heard of RSS, I had no idea exactly what it was...


And so, I did a bit of research on RSS. RSS is generally considered to stand for Real Simple Syndication, and in a nutshell, is a way to "push" content from your website to users who have chosen to subscribe to this content. You can read more about blogs and blogging over here: I thought that it might be complicated, but was pleasantly surprised at how easy it was to create an RSS feed from my blog.

Using the code

In your ASP.NET project, add a new Web Form called MyFeed.aspx. Open the source of this page and delete everything but the first line, which should read something like:

<%@ Page Language="VB" AutoEventWireup="false" 

         CodeFile="MyFeed.aspx.vb" Inherits="MyFeed" %>

Below this, add the following line:

<%@ OutputCache Duration="300" VaryByParam="Category" %>

OutputCache is the amount of time in seconds that the ASP.NET server will keep the page in its cache so that the database is not interrogated every time someone opens your RSS feed. You can decrease this depending on how often your feed is updated.

The optional VaryByParam allows you to pass a parameter to your SQL query that will change the content of your RSS feed.

That's it for the ASPX page. Make sure you save it, and let's move on to the code-behind. Open the code-behind page and import the following namespaces:

Imports System.Data.SqlClient
Imports System.Text
Imports System.Xml

Now, add the following to the Page_Load event:

Response.ContentType "application/rss+xml;"

Dim xrFeed As XmlTextWriter = New XmlTextWriter
Response.OutputStream, Encoding.UTF8)

There are a few elements we need to include in all our RSS feeds: a title, a link, a ttl, and a description.

  • title - the name of your channel and how people will refer to it.
  • link - the URL of the website from which the feed originates.
  • ttl - "time to live" - the number of minutes a feed can be cached before refreshing from the source.
  • description - a phrase describing the RSS feed.

More information on these elements can be found here.

xrFeed.WriteAttributeString("version", "2.0")
xrFeed.WriteElementString("title", "Latest Blog Items")
xrFeed.WriteElementString("link", "")
xrFeed.WriteElementString("ttl", "10")
xrFeed.WriteElementString("description", "The latest items from my blog.")

Now we connect to our database to retrieve our blog items for our feed. I first select the email address of the website owner from my "settings" table, and then I select the items from my blog.

Dim clsUtils As New Utils
Dim intBlogID As Integer = clsUtils.intBlogID
Dim strConnection As String = clsUtils.strConnection

Dim connSql As SqlConnection = New SqlConnection(strConnection)
Dim cmdSql As SqlCommand = New SqlCommand

cmdSql.Connection = connSql
cmdSql.CommandText = "SELECT strOwnerEmail From " & _ 
                     "tblSiteSettings WHERE intUniqueID = " & intBlogID
Dim strOwnerEmail As String = cmdSql.ExecuteScalar

cmdSql = New SqlCommand
cmdSql.Connection = connSql
Dim strSql as String = ""
If Trim(Request.QueryString("cID")) <> "" Then
    strSql = "SELECT * FROM tblItems WHERE I.intFKBlogItemCategoryID = " 
    strSql += Request.QueryString("cID") & " AND I.bitActive = 1 "
    strSql += "AND I.intFKBlogID = " & intBlogID.ToString & _
              " ORDER BY dtPosted DESC"
    strSql = "SELECT * FROM tblItems WHERE I.bitActive = 1 AND "
    strSql += "I.intFKBlogID = " & intBlogID.ToString & _
              " ORDER BY dtPosted DESC"
End If
cmdSql.CommandText = strSql

Dim drSql As SqlDataReader = cmdSql.ExecuteReader
Do While drSql.Read

For each item I want in my feed, I create several elements:

  • title - the title of this particular item
  • description - the content of this particular item
  • category - the category to which this particular item belongs
  • link - the URL to this particular item
  • guid - a string that uniquely identifies this particular item
  • pubDate - the date that this particular item was published (watch out for the formatting on this one)
xrFeed.WriteElementString("title", drSql("strTitle").ToString)
xrFeed.WriteElementString("description", Replace(drSql("strBody").ToString(), "", vbCrLf))
xrFeed.WriteElementString("category", drSql("strCategory").ToString)
xrFeed.WriteElementString("author", strOwnerEmail & " (" & _
                          drSql("strPostedBy").ToString & ")")
xrFeed.WriteElementString("link", _
                          "" & _
xrFeed.WriteElementString("guid", _
                          "" & _
xrFeed.WriteElementString("pubDate", drSql.GetDateTime(4).ToString("R"))

Now I just close everything off.




And that's it!

Now, if you want your visitors to know that your site has an active RSS feed, add the following link into the <head> section of your site's pages:

<link rel="alternate" type="application/atom+xml" 

      title="Latest Items" href="" />

When they visit any of your pages with this link, their browser will know that you have an RSS feed available. Also, if your page has the above link in it, then an RSS reader pointed at the page will automatically pick up your feed.

Points of Interest

RSS feeds have the added bonus of being very search engine friendly, and may contribute to your site ranking higher in search results.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
South Africa South Africa
No Biography provided

You may also be interested in...


Comments and Discussions

Questionerror in rss (suscribe) Pin
vikram1919-Apr-08 23:16
membervikram1919-Apr-08 23:16 
QuestionAny body explain Pin
vikram1919-Apr-08 23:15
membervikram1919-Apr-08 23:15 
Generalconversion error Pin
raju@12313-Sep-07 1:53
memberraju@12313-Sep-07 1:53 
Generalconversion error Pin
raju@12313-Sep-07 1:53
memberraju@12313-Sep-07 1:53 
Generalerror Pin
raju@12311-Sep-07 21:16
memberraju@12311-Sep-07 21:16 
GeneralRe: error Pin
mikvos11-Sep-07 21:34
membermikvos11-Sep-07 21:34 
GeneralRe: error Pin
raju@12312-Sep-07 19:22
memberraju@12312-Sep-07 19:22 
GeneralRe: error Pin
mikvos12-Sep-07 20:11
membermikvos12-Sep-07 20:11 
QuestionDatabase design Pin
browncake14-Aug-07 9:34
memberbrowncake14-Aug-07 9:34 
AnswerRe: Database design Pin
mikvos14-Aug-07 18:25
membermikvos14-Aug-07 18:25 
GeneralRe: Database design Pin
browncake15-Aug-07 1:11
memberbrowncake15-Aug-07 1:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170622.1 | Last Updated 3 Jul 2007
Article Copyright 2007 by mikvos
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid