Help with a cast error

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 4
Reputation: mtarby is an unknown quantity at this point 
Solved Threads: 0
mtarby mtarby is offline Offline
Newbie Poster

Help with a cast error

 
0
  #1
Jun 13th, 2007
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:

  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)

  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!
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 275
Reputation: f1 fan is an unknown quantity at this point 
Solved Threads: 11
f1 fan f1 fan is offline Offline
Posting Whiz in Training

Re: Help with a cast error

 
0
  #2
Jul 5th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC