advertisement

Search | Recent | Register

Forum => Article discussions => Building an RSS feed made simple

Jump to:

Next Oldest | Next Newest

1/19/2004 6:29:34 PM Link | Reply | Edit | Quote

uber:ASP.Net

Location: | Joined: 1/1/2000 | Posts: 12 | Online

Article discussion for:
Building an RSS feed made simple

1/30/2004 8:43:12 AM Link | Reply | Edit | Quote

John B. Hopper

Profile Send Private Message
Location: | Joined: 1/30/2004 | Posts: 1 | Offline
This article brought me to your site for the first time and I registered immediately. I found the writing refreshingly direct and clear, the content relevant and timely, and the code clean, readable, and useful. Excellent job. I'm making this site a regular stop.
1/30/2004 8:49:47 AM Link | Reply | Edit | Quote

Jeff

Profile Send Private Message Web Site AOL Instant Message ICQ Message
Location: Cleveland, OH, USA | Joined: 8/15/2000 | Posts: 813 | Offline
Thanks! I appreciate the feedback.

Jeff 'Jones' Putz
POP World Media, LLC

2/2/2004 3:49:37 PM Link | Reply | Edit | Quote

SuperLite17

Profile Send Private Message
Location: | Joined: 1/10/2004 | Posts: 22 | Offline
Hello Jeff,

Quick question for you. How difficult would it be to implement an RSS feed with the Pop Forum, like it's done at Asp.Net?


2/2/2004 5:50:17 PM Link | Reply | Edit | Quote

Jeff

Profile Send Private Message Web Site AOL Instant Message ICQ Message
Location: Cleveland, OH, USA | Joined: 8/15/2000 | Posts: 813 | Offline
Not hard. In the article code you'd just call Forum.GetTopics() instead of getting data from the database.

Jeff 'Jones' Putz
POP World Media, LLC

2/4/2004 9:16:18 PM Link | Reply | Edit | Quote

Mike U

Profile Send Private Message
Location: | Joined: 2/5/2004 | Posts: 2 | Offline
When I follow through your example I get the following error message: Specified cast is not valid. and get the following stack trace:

[InvalidCastException: Specified cast is not valid.]   System.Data.SqlClient.SqlDataReader.GetSqlDateTime(Int32 i) +46   System.Data.SqlClient.SqlDataReader.GetDateTime(Int32 i) +33   admin.cstain.RSS.newsfeed.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\v2\newsfeed.aspx.vb:38   System.Web.UI.Control.OnLoad(EventArgs e) +67   System.Web.UI.Control.LoadRecursive() +35   System.Web.UI.Page.ProcessRequestMain() +731

I am fairly new to asp.net from what I can gather this is happening on the time formatting. I am still unsure of all of the formatting options (one page i viewed show 17 examples alone and there are still more) so I am just lost of how to correct this. When I save the timestamp in my database i just use the standard formatting of the now command. which is m/d/yy 00:00:00 AM/PM I seen you were using R as your formatting type. I am unsure if that is the format you are saving it in your database or if you are reformatting your data to the R type. If you could help me I would appreciate it. I have also incluided the code I used on the page.

Imports System

Imports System.Configuration

Imports System.Data

Imports System.Data.SqlClient

Imports System.Text

Imports System.Web

Imports System.Xml

Namespace cstain.RSS

Public Class newsfeed

Inherits System.Web.UI.Page

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

Response.Clear()

Response.ContentType = "text/xml"

Dim objX As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)

objX.WriteStartDocument()

objX.WriteStartElement("rss")

objX.WriteAttributeString("version", "2.0")

objX.WriteStartElement("channel")

objX.WriteElementString("title", "Crimson Stain Productions")

objX.WriteElementString("link", "http://www.crimsonstain.net/newsfeed.aspx")

objX.WriteElementString("description", "The latest in what going on with Crimson Stain Productions")

objX.WriteElementString("copyright", "(c) 2004, Crimson Stain Productions, All rights reserved.")

objX.WriteElementString("ttl", "5")

Dim objConnection As New SqlConnection(ConfigurationSettings.AppSettings("MyConnectionString"))

objConnection.Open()

Dim sql As String = "SELECT TOP 10 title, main, postid, time FROM cstain_news ORDER BY time DESC"

Dim objCommand As New SqlCommand(sql, objConnection)

Dim objReader As SqlDataReader = objCommand.ExecuteReader()

While objReader.Read()

objX.WriteStartElement("item")

objX.WriteElementString("title", objReader.GetString(0))

objX.WriteElementString("description", objReader.GetString(1))

objX.WriteElementString("link", "http://www.crimsonstain.net/GetArticle.aspx?id=" + objReader.GetInt32(2).ToString())

objX.WriteElementString("pubDate", objReader.GetDateTime(3).ToString("G"))

objX.WriteEndElement()

End While

objReader.Close()

objConnection.Close()

objX.WriteEndElement()

objX.WriteEndElement()

objX.WriteEndDocument()

objX.Flush()

objX.Close()

Response.End()

End Sub

End Class

End Namespace

Thanks,

Mike

2/4/2004 9:22:38 PM Link | Reply | Edit | Quote

Jeff

Profile Send Private Message Web Site AOL Instant Message ICQ Message
Location: Cleveland, OH, USA | Joined: 8/15/2000 | Posts: 813 | Offline
Which one is line 38? I assume it's this one:

objX.WriteElementString("pubDate", objReader.GetDateTime(3).ToString("G"))

Are you using a non-US version of SQL Server and/or the server OS? Are you absolutely sure that your "time" column is the type datetime?


Jeff 'Jones' Putz
POP World Media, LLC

2/4/2004 9:36:07 PM Link | Reply | Edit | Quote

Mike U

Profile Send Private Message
Location: | Joined: 2/5/2004 | Posts: 2 | Offline
The datetime part was the issue. I had it set as varchar. When creating the table it showed the size of the field as 8 when I would add test data it would only show the date. But after testing it just to see fi it would stop that error i see that it does infact store the date and the time even though a visual editor for the database doesnt show anything but the date. Thank you for your help.
2/4/2004 11:09:08 PM Link | Reply | Edit | Quote

Jeff

Profile Send Private Message Web Site AOL Instant Message ICQ Message
Location: Cleveland, OH, USA | Joined: 8/15/2000 | Posts: 813 | Offline
That's perhaps one warning regarding using the get methods of the readers. You have to be sure that the types match what you have in the database. Way back in the stone age I made the same mistake, so don't sweat it.

Jeff 'Jones' Putz
POP World Media, LLC

10/22/2005 7:16:58 PM Link | Reply | Edit | Quote

mark1504

Profile Send Private Message Web Site
Location: UK | Joined: 10/22/2005 | Posts: 2 | Offline

Many thanks Jeff,

This is just what I was looking for. Had it up and running in no time. Now I just have to see if any of my customers use it!

Regards
Mark

5/17/2006 12:52:57 PM Link | Reply | Edit | Quote

mtarby

Profile Send Private Message
Location: | Joined: 5/17/2006 | Posts: 2 | Offline

Great Article - I'm trying to get it to work with a news database I'm using and keep getting an XML Parsing Error: no element found

Imports System
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Imports System.Web
Imports System.Xml

Namespace LMC.RSS
Public Class NewsWire
Inherits System.Web.UI.Page
Private Sub Page_Load(sender As Object, e As System.EventArgs)
Response.Clear()
Response.ContentType = "text/xml"
Dim objX As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
objX.WriteStartDocument()
objX.WriteStartElement("rss")
objX.WriteAttributeString("version", "2.0")
objX.WriteStartElement("channel")
objX.WriteElementString("title", "LMC.Net NewsWire")
objX.WriteElementString("link", "http://www.lemoyne.edu/rss/rss.aspx")
objX.WriteElementString("description", "The latest headlines and events from around le Moyne College")
objX.WriteElementString("copyright", "(c) 2006 Le Moyne College. All rights reserved.")
objX.WriteElementString("ttl", "20")
Dim objConnection As New SqlConnection(ConfigurationSettings.AppSettings("MyConnectionString"))
objConnection.Open()
Dim sql As String = "SELECT TOP 20 NewsHeadline, NewsDetails, NewsNo, NewsExpDate FROM DailyNews ORDER BY NewsExpDate DESC"
Dim objCommand As New SqlCommand(sql, objConnection)
Dim objReader As SqlDataReader = objCommand.ExecuteReader()
While objReader.Read()
objX.WriteStartElement("item")
objX.WriteElementString("title", objReader.GetString(0))
objX.WriteElementString("description", objReader.GetString(1))
objX.WriteElementString("link", "/rss/GetArticle.aspx?id=" + objReader.GetInt32(2).ToString())
objX.WriteElementString("pubDate", objReader.GetDateTime(3).ToString("R"))
objX.WriteEndElement()
End While
objReader.Close()
objConnection.Close()
objX.WriteEndElement()
objX.WriteEndElement()
objX.WriteEndDocument()
objX.Flush()
objX.Close()
Response.End()
End Sub
End Class
End Namespace

Any suggestions as to what I've done wrong would be appreciated!

5/17/2006 4:24:27 PM Link | Reply | Edit | Quote

Jeff

Profile Send Private Message Web Site AOL Instant Message ICQ Message
Location: Cleveland, OH, USA | Joined: 8/15/2000 | Posts: 813 | Offline

What line does the error occur on?


Jeff 'Jones' Putz
POP World Media, LLC

5/18/2006 7:51:40 AM Link | Reply | Edit | Quote

mtarby

Profile Send Private Message
Location: | Joined: 5/17/2006 | Posts: 2 | Offline

in my rss.aspx page, it says its occurring in line 1 position 1

5/18/2006 9:29:21 AM Link | Reply | Edit | Quote

Jeff

Profile Send Private Message Web Site AOL Instant Message ICQ Message
Location: Cleveland, OH, USA | Joined: 8/15/2000 | Posts: 813 | Offline

Wow, I'm not sure. Your code looks like a straight port from my C# code, and I know that works (it's on this site's feed!).


Jeff 'Jones' Putz
POP World Media, LLC

5/25/2006 3:35:09 PM Link | Reply | Edit | Quote

du8die

Profile Send Private Message
Location: Southeast Wisconsin | Joined: 1/16/2004 | Posts: 126 | Offline

Wednesday, May 17, 2006 10:32 AM 119 rss.aspx
Wednesday, May 17, 2006 10:05 AM 147 rss.aspx.bak
Wednesday, May 17, 2006 12:45 PM 2244 RSS.aspx.cs
Wednesday, May 17, 2006 12:45 PM 2251 RSS.aspx.cs.bak
Wednesday, May 17, 2006 10:31 AM 4647 Web.config
Wednesday, May 17, 2006 10:26 AM 4657 Web.config.bak

Is this truly the directory structure? And, is the code you posted your code? If so, a few questions. First of all, you're trying to write VB code in a C# file. That's probably your error.

Beyond that, have you complied? Etc...

d8


The cause of the problem is:
The POP server is out of Coke .

4/3/2007 5:49:58 PM Link | Reply | Edit | Quote

Tim Grant

Profile Send Private Message
Location: | Joined: 4/3/2007 | Posts: 2 | Offline

Hi,

I have a question about the c# RSS Feed code. This will sound strange, but I implemented it, it all seemed fine, validated it OK, and I subscribed it into Newscrawler to test. It loaded fine, but when I close and reopen Newscrawler, they all disappear - read and unread. I try to hit update - nothing happens. This doesnt happen with other feeds I added. Any ideas?

Thanks in advance

Tim

4/3/2007 7:08:11 PM Link | Reply | Edit | Quote

Jeff

Profile Send Private Message Web Site AOL Instant Message ICQ Message
Location: Cleveland, OH, USA | Joined: 8/15/2000 | Posts: 813 | Offline

No idea. Sounds like more of an issue with the reader. I've used many different readers without issue. You never can tell for sure when you don't have control of that side of the equation.


Jeff 'Jones' Putz
POP World Media, LLC

4/5/2007 10:15:39 AM Link | Reply | Edit | Quote

Tim Grant

Profile Send Private Message
Location: | Joined: 4/3/2007 | Posts: 2 | Offline

Thanks Jeff,

I think I've sorted it. When validating it, the validator told me to switch Coding to ASCII from UTF8 to match my server, but I think this was responsible. I switched it to UTF8 and doesn't happen anymore, so there you go, strange.

Tim

4/6/2007 2:43:44 PM Link | Reply | Edit | Quote

nyd

Profile Send Private Message
Location: | Joined: 4/6/2007 | Posts: 1 | Offline

Thanks for the code JEFF! As a hack programmer who
does not do .net work but inherited a .net site , I had problems with this statement.

SqlConnection objConnection = new SqlConnection(ConfigurationSettings.AppSettings["MyConnectionString"]);

Time and research helped me figure this out but it might help newbies like me if a tutorial existed for this. No pressure and it may already exist on the site. By help I mean using a fictious DB on a SQL Server with fake login and password so we replace the variables, atleast for a direct connection to the DB.

Another issue was dealing with .net 2.0, from my limited knowledge, your code would be problematic in a 2.0 environment. Is that right? I understand why, 2.0 did not exist when you wrote it.

To get around that, I made use of this guy's code, looks like a 2.0 version of your code, and pass it along for anyone else that may need it.

http://forums.asp.net/thread/1314507.aspx

go to the 11-06-2006, 10:41 AM response.

Thanks again for the code, big big help!

4/9/2007 12:48:46 PM Link | Reply | Edit | Quote

Jeff

Profile Send Private Message Web Site AOL Instant Message ICQ Message
Location: Cleveland, OH, USA | Joined: 8/15/2000 | Posts: 813 | Offline

The code works exactly the same in 2.0. I think if you compile or build you'll get a warning for a couple of things that have been deprecated, but they'll still work just fine. v2 has a web.config section for connection strings all its own.


Jeff 'Jones' Putz
POP World Media, LLC

Forum => Article discussions => Building an RSS feed made simple