Creating an RSS Feed with ASP.Net Written in C# - Code Behind Page
(Page 3 of 4 )
Create a new file called rss.aspx.cs in the same place you have created the rss.aspx page.
At the very top of the page, we will tell the pages about all the dot.net classes we will be using, as follows:
using System;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Xml;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
After that we create the namespace, and set it to be a web page
namespace SyndicationDemo
{
//Class for returning RSS feed Data
public partial class rss : System.Web.UI.Page
{
Now we will write the page load event. This will run every time someone requests the page. It basically queries our database for the information, and then binds this to the repeater control.
private void Page_Load(object sender, System.EventArgs e)
{
// Connect to the Database
SqlConnection myConnection = new SqlConnection("Data Source=DBServer1;Initial Catalog=rss_news;Persist Security Info=True;User ID=user1;Password=Pass;");
// Retrieve the SQL query results and bind it to the Repeater
const string SQL_QUERY = "SELECT top 50 id, headline, story_text, FROM news_stories";
SqlCommand myCommand = new SqlCommand(SQL_QUERY, myConnection);
myConnection.Open();
rptRSS.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
rptRSS.DataBind();
myConnection.Close();
}
The first thing we do is create a SQL server connection to our database. In a real world production application you would want to add some exception handling to the code; it is left out here to made the code more readable.
Once we have connected, we run a SQL query on the database to return our stories. The SQL here is using the TOP keyword so we only return a limited number of stories to show. In the real world you might limit this to stories within a certain date, of a certain status, and so on.
The TOP keyword might not work if you are using a database that is hosted on a non-Microsoft SQL server.
We then open the connection to the database, execute the SQL against this connection, bind the SQL queries results to the repeater and close the connections.
The last part of the code behind page is a simple function that will be called from the main page. This function replaces any chars in your stories that are XML reserved characters
protected string FormatForXML(object input)
{
string data = input.ToString(); //cast input to string
// replace those characters disallowed in XML documents
data = data.Replace("&", "&");
data = data.Replace(""", """);
data = data.Replace("'", "'");
data = data.Replace("<", "<");
data = data.Replace(">", ">");
return data;
}
The function just accepts one argument, casts it to a text type and does a find and replace on the reserved characters to make them valid.
Next: The Data Repeater >>
More C# Articles
More By Luke Niland