I am trying to make an RSS Feed with asp.net,sql and xml. I am getting an error
Compiler Error Message: CS0103: The name ‘MyConnString’ does not exist in the current context’ on line 22 ‘SqlConnection objConnection = new SqlConnection(‘MyConnString’);
My web config contains
<connectionStrings> <add name='MyConnString' connectionString=' providerName='System.Data.SqlClient' /> </connectionStrings>
Here is the code
<%@ Page Language='C#' MasterPageFile='ContentMasterPage.master' Debug='true' %> <%@ Import Namespace='System.Xml'%> <%@ Import Namespace='System.Data' %> <%@ Import Namespace='System.Data.SqlClient' %> <script runat='server'> void Page_load(object sender, System.EventArgs e) { Response.Clear(); Response.ContentType = 'text/xml'; XmlTextWriter objX = new XmlTextWriter(Response.OutputStream, Encoding.UTF8); objX.WriteStartDocument(); objX.WriteStartElement('rss'); objX.WriteAttributeString('version', '2.0'); objX.WriteElementString('title', 'News'); objX.WriteElementString('link', 'http://news.ca/news.aspx'); objX.WriteElementString('description', 'The latest headlines'); objX.WriteElementString('copyright', '(c)2009, News Club, All rights reserved.'); objX.WriteElementString('ttl', '5'); SqlConnection objConnection = new SqlConnection('MyConnString'); objConnection.Open(); string sql = 'SELECT TOP 5 Title, Description, ArticleID, DatePulished FROM articles ORDER BY DatePublished DESC'; SqlCommand objCommand = new SqlCommand(sql, objConnection); SqlDataReader objReader = objCommand.ExecuteReader(); while (objReader.Read()) { objX.WriteStartElement('item'); objX.WriteElementString('title', objReader.GetString(0)); objX.WriteElementString('description', objReader.GetString(1)); objX.WriteElementString('link', ('http://news.ca/GetArticle.aspx?id=' + objReader.GetInt32(2).ToString())); objX.WriteElementString('pubDate', objReader.GetDateTime(3).ToString('R')); objX.WriteEndElement(); } objReader.Close(); objConnection.Close(); objX.WriteEndElement(); objX.WriteEndElement(); objX.WriteEndDocument(); objX.Flush(); objX.Close(); Response.End(); } </script>
This is the line that the compiler is having a difficult time with. Did you replace your actually connection string with ‘MyConnString’ or is MyConnString supposed to be a string value on the page?
I have a similar line of code in my app:
You can also get this value from the Web.Config