I'm trying to migrate some code to generate an rss feed from classic ASP to .NET (because its cleaner). I've got it working perfectly for the first feed I try, but when I tried to modify the code for the next feed I get a Specified cast is not valid.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

The only thing different in the code from feed a to feed b is the call to the different connection string:

Namespace LMC.news
    Public Class news
        Inherits System.Web.UI.Page

            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()
            Dim strPI
            strPI = "type='text/xsl' href='news.xsl'"
            objX.WriteProcessingInstruction("xml-stylesheet", strPI)
            objX.WriteStartElement("rss")
            objX.WriteAttributeString("version", "2.0")
            objX.WriteStartElement("channel")
            objX.WriteElementString("title", "Press Releases")
            objX.WriteElementString("link", "/news/news.aspx")
            objX.WriteElementString("description", "Recent Press Releases from the Office of Communications")
            objX.WriteElementString("language", "en-us")
            objX.WriteElementString("copyright", "(c) 2006 Le Moyne College. All rights reserved.")
            objX.WriteElementString("webmaster", "webmaster@lemoyne.edu")
            objX.WriteElementString("ttl", "20")
             Dim objConnection As New SqlConnection(ConfigurationSettings.AppSettings("wwwConnection"))
            objConnection.Open()
            Dim sql As String = "SELECT *  FROM press_releases where press_post > '1/1/2007' order by press_post 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.lemoyne.edu/communications/Comm_Press.asp?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

I'm not sure if its the connection I added to my web config file (the tables are on 2 separate databases)

<appSettings>
        <add key="strConnection" value="server=mssql;uid=xxxxx;pwd=xxxxx;database=xxxxx" />
        <add key="wwwConnection" value="server=mssql;uid=xxxxxx;pwd=xxxxx;database=xxxxx" />
    </appSettings>

Any assistance would be appreciated!

It is the data not your code. You are putting something in there that cant be converted at runtime. Probably a null value somewhere - you have no testing of the data before you manipulate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.