Friday, April 11, 2008

Creating an RSS feed for your site using Asp.net,C#,Access database

Create a new Item in visual stdios 2005 generic handler( *.ashx ).
Live code example is following:

using System;
using System.Web;
using System.Data.OleDb;
using System.Text;
using System.Xml;
public class rss : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
string sTxt = BuildXmlString();
context.Response.ContentType = "text/xml";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Write(sTxt);
}
private string BuildXmlString()
{
string sTitle = "Super Value Deals and Deals of The Day";
string sSiteUrl = "http://www.visli.com";
string sDescription = "Super Value Deals and Deals of The Day";
string sTTL = "60";
System.Text.StringBuilder oBuilder = new System.Text.StringBuilder();
oBuilder.Append("");
oBuilder.Append("");
oBuilder.Append(""); <br />oBuilder.Append(sTitle); <br />oBuilder.Append("");
oBuilder.Append("");
oBuilder.Append(sSiteUrl);
oBuilder.Append("");
oBuilder.Append("");
oBuilder.Append(sDescription);
oBuilder.Append("
");
oBuilder.Append("");
oBuilder.Append(sTTL);
oBuilder.Append("
");

AppendItems(oBuilder);

oBuilder.Append("
");
return oBuilder.ToString();
}

public void AppendItems(System.Text.StringBuilder oBuilder)
{
string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;data source=c:\\sites\\Single15\\visli\\database\\AmazonDB.mdb";
OleDbConnection myOleDbConnection = new OleDbConnection(connectionString);
OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();
string ProdTy = "M2";
string Strsql = "SELECT top 15 * from DetailUrl where ProdType='M2'";
myOleDbCommand.CommandText = Strsql;
myOleDbConnection.Open();
OleDbDataReader myOleDbDataReader = myOleDbCommand.ExecuteReader();
myOleDbDataReader.Read();
while (myOleDbDataReader.Read())
{
string sTitle = "Super Value Deals and Deals of The Day";
string sLink = "http://www.visli.com/";
string sDescription = "Super Value Deals and Deals of The Day";
string sPubDate = "System.DateTime.Now";
oBuilder.Append("");
oBuilder.Append(""); <br />oBuilder.Append(myOleDbDataReader["TextUrl"].ToString()); <br />oBuilder.Append("");
oBuilder.Append("");
oBuilder.Append(myOleDbDataReader["NameUrl"].ToString());
oBuilder.Append("");
oBuilder.Append("");
oBuilder.Append("<img src=" + myOleDbDataReader["ImageUrl"].ToString() + ">" + "<br>" + myOleDbDataReader["Price"].ToString() + "<br>visit <a href=http://www.visli.com>http://www.visli.com</a>");
oBuilder.Append("
");
oBuilder.Append("");
oBuilder.Append(myOleDbDataReader["UpdDate"].ToString());
oBuilder.Append("
");
oBuilder.Append("
");

}
}

public bool IsReusable {
get {
return false;
}
}
}

Thanks to asp.net forum

No comments: