views:

318

answers:

3

Hi everyone 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>
A: 

SqlConnection takes the connectionString not a reference in the web.config. Or double check your web.config

Daniel A. White
yes,<connectionStrings> <add name="MyConnString" connectionString="" providerName="System.Data.SqlClient" /> </connectionStrings>
+3  A: 
SqlConnection objConnection = new SqlConnection("MyConnString");

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:

SqlConnection oConn= new SqlConnection(_connString);

You can also get this value from the Web.Config

SqlConnection oConn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString);
RSolberg
A: 

As the exception says, you have a mistake at this line :

SqlConnection objConnection = new SqlConnection("MyConnString");

Assuming you have your connection string at web.config in connectionStings section, you should set your connection string like that :

SqlConnection objConnection = new 
SqlConnection(ConfigurationManager.ConnectionStrings["MyConnString"]);

By the way you should add System.Configuration as reference to your project yo use ConfigurationManager class.

Canavar