943,874 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 1478
  • ASP.NET RSS
Jun 13th, 2007
0

Help with a cast error

Expand Post »
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:

ASP.NET Syntax (Toggle Plain Text)
  1. Namespace LMC.news
  2. Public Class news
  3. Inherits System.Web.UI.Page
  4.  
  5. Sub Page_Load(sender As Object, e As System.EventArgs)
  6. Response.Clear()
  7. Response.ContentType = "text/xml"
  8.  
  9. Dim objX As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
  10. objX.WriteStartDocument()
  11. Dim strPI
  12. strPI = "type='text/xsl' href='news.xsl'"
  13. objX.WriteProcessingInstruction("xml-stylesheet", strPI)
  14. objX.WriteStartElement("rss")
  15. objX.WriteAttributeString("version", "2.0")
  16. objX.WriteStartElement("channel")
  17. objX.WriteElementString("title", "Press Releases")
  18. objX.WriteElementString("link", "/news/news.aspx")
  19. objX.WriteElementString("description", "Recent Press Releases from the Office of Communications")
  20. objX.WriteElementString("language", "en-us")
  21. objX.WriteElementString("copyright", "(c) 2006 Le Moyne College. All rights reserved.")
  22. objX.WriteElementString("webmaster", "webmaster@lemoyne.edu")
  23. objX.WriteElementString("ttl", "20")
  24. Dim objConnection As New SqlConnection(ConfigurationSettings.AppSettings("wwwConnection"))
  25. objConnection.Open()
  26. Dim sql As String = "SELECT * FROM press_releases where press_post > '1/1/2007' order by press_post desc"
  27. Dim objCommand As New SqlCommand(sql, objConnection)
  28. Dim objReader As SqlDataReader = objCommand.ExecuteReader()
  29. While objReader.Read()
  30. objX.WriteStartElement("item")
  31. objX.WriteElementString("title", objReader.GetString(0))
  32. objX.WriteElementString("description", objReader.GetString(1))
  33. objX.WriteElementString("link", "http://www.lemoyne.edu/communications/Comm_Press.asp?id=" + objReader.GetInt32(2).ToString())
  34. objX.WriteElementString("pubDate", objReader.GetDateTime(3).ToString("R"))
  35. objX.WriteEndElement()
  36. End While
  37. objReader.Close()
  38. objConnection.Close()
  39. objX.WriteEndElement()
  40. objX.WriteEndElement()
  41. objX.WriteEndDocument()
  42. objX.Flush()
  43. objX.Close()
  44. Response.End()
  45.  
  46. End Sub
  47. End Class
  48.  
  49. End Namespace
I'm not sure if its the connection I added to my web config file (the tables are on 2 separate databases)

ASP.NET Syntax (Toggle Plain Text)
  1. <appSettings>
  2. <add key="strConnection" value="server=mssql;uid=xxxxx;pwd=xxxxx;database=xxxxx" />
  3. <add key="wwwConnection" value="server=mssql;uid=xxxxxx;pwd=xxxxx;database=xxxxx" />
  4. </appSettings>
Any assistance would be appreciated!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mtarby is offline Offline
4 posts
since Apr 2007
Jul 5th, 2007
0

Re: Help with a cast error

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.
Reputation Points: 26
Solved Threads: 11
Posting Whiz in Training
f1 fan is offline Offline
275 posts
since Jan 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: ? ItemTemplate in TemplateField of a GridView
Next Thread in ASP.NET Forum Timeline: Problem with ContentPlaceHolder content format in IE, ok in Firefox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC