The ConnectionString property has not been initialized

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kingsep11
    New Member
    • Feb 2008
    • 1

    The ConnectionString property has not been initialized

    hi
    I am doing project under entire architecture in ASP.Net ,, But i am getting coonection error while running the program

    connection at webconfig is below:
    [code=xml]
    <appSettings>

    <add key="Myconn" value="server=. ;uid=;pwd=;Init ial Catalog=tempdb; Integrated Security=true"/>
    </appSettings >
    [/code]

    my code on calling connection is

    Code:
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    
    /// <summary>
    /// Summary description for clsvarDeclaration
    /// </summary>
    public class clsvarDeclaration
    {
        private static SqlConnection con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["Myconn"]);
    		
        
    	public clsvarDeclaration()
    	{
    		//
    		// TODO: Add constructor logic here
    		//
    	}
    
        public static SqlConnection connection
        {
            get
            {
                return con;
            }
            set
            {
                con = value;
            }
        }
    }
    but it shows error: The ConnectionStrin g property has not been initialized


    Any suggetions most Welcome
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Well, I am not sure why that doesnt work, but I have used this with success:
    Code:
    private static AppSettingsReader ap = new AppSettingsReader();
    
    public static string ConnectionString
    {
       get
       {
          return ap.GetValue("mycon", typeof(string)).ToString();
       }
    }
    ALSO: On the connection string you have (I assume you removed those values for safety reasons), using Integrated Security=true negates the uid and pwd statements, which are not even valid statements for SqlConnection:
    404 - Page Not Found. Shown when a URL cannot be mapped to any kind of resource.

    Comment

    Working...