2009-04-15 7 views
0

J'essaie de créer un flux RSS avec asp.net, sql et xml. Je reçois une erreurErreur de configuration ConnectionString

compilateur Message d'erreur: CS0103: Le nom 'myConnString' n'existe pas dans le contexte actuel "sur la ligne 22 "SqlConnection objConnection = new SqlConnection (" myConnString");

Ma config web contient

<connectionStrings> 
    <add name="MyConnString" connectionString=" 
    providerName="System.Data.SqlClient" /> 
    </connectionStrings> 

Voici le 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> 

Répondre

3
SqlConnection objConnection = new SqlConnection("MyConnString"); 

Ceci est la ligne que le compilateur est un moment difficile avec. Avez-vous remplacé votre chaîne de connexion par "MyConnString" ou est-ce que MyConnString est censé être une valeur de chaîne sur la page?

je une ligne similaire de code dans mon application:

SqlConnection oConn= new SqlConnection(_connString); 

Vous pouvez également obtenir cette valeur de la Web.Config

SqlConnection oConn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString); 
0

SqlConnection prend la connectionString pas une référence dans le web.config. Ou vérifiez votre web.config

+0

oui,

0

Comme l'exception dit, vous avez une erreur à cette ligne:

SqlConnection objConnection = new SqlConnection("MyConnString"); 

En supposant que vous avez votre chaîne de connexion à web.config dans la section connectionStings, y ous devez définir votre chaîne de connexion comme ça:

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

Par ailleurs, vous devez ajouter System.Configuration comme référence à votre projet yo utiliser la classe ConfigurationManager.

Questions connexes