Oleg
Oleg

Reputation: 1775

To Read connection String from web.config

WCF service on IIS. I have got a problem with reading connection string from web.config file. my connection string looks like this :

    <connectionStrings>
  <add 
    name="ABC" 
    connectionString="DEF"
    providerName="GGG"
  />
</connectionStrings>

My code in Global.asax.cs :

protected void Application_Start(object sender, EventArgs e)
        {                    

           cons_Webdata =  WebConfigurationManager.ConnectionStrings["ABC"].ConnectionString;

        }

I have error message in "cons_Webdata = ......." :

Object reference not set to an instance of an object.

What's wrong? Thanks.

Upvotes: 2

Views: 11842

Answers (2)

Rajesh
Rajesh

Reputation: 7886

Check out on how to use WebConfigurationManager in web application here. You need to read the section connection strings from the config file and then use them as shown below:

           // Get the connectionStrings section.
            ConnectionStringsSection connectionStringsSection =
                WebConfigurationManager.GetSection("connectionStrings")
                as ConnectionStringsSection;

            // Get the connectionStrings key,value pairs collection.
            ConnectionStringSettingsCollection connectionStrings =
                connectionStringsSection.ConnectionStrings;

            // Get the collection enumerator.
            IEnumerator connectionStringsEnum =
                connectionStrings.GetEnumerator();

            // Loop through the collection and 
            // display the connectionStrings key, value pairs.
            int i = 0;
            Console.WriteLine("[Display the connectionStrings]");
            while (connectionStringsEnum.MoveNext())
            {
                string name = connectionStrings[i].Name;
                Console.WriteLine("Name: {0} Value: {1}",
                name, connectionStrings[name]);
                i += 1;
            }

Upvotes: 1

Trent Scholl
Trent Scholl

Reputation: 2387

Try using ConfigurationManager instead of WebConfigurationManager

protected void Application_Start(object sender, EventArgs e)
{                    
    cons_Webdata =  ConfigurationManager.ConnectionStrings["ABC"].ConnectionString;
}

Otherwise, use the code sample from http://msdn.microsoft.com/en-us/library/ms178411.aspx

        System.Configuration.Configuration rootWebConfig =
            System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");
        System.Configuration.ConnectionStringSettings connString;
        if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0)
        {
            connString =
                rootWebConfig.ConnectionStrings.ConnectionStrings["NorthwindConnectionString"];
            if (connString != null)
                Console.WriteLine("Northwind connection string = \"{0}\"",
                    connString.ConnectionString);
            else
                Console.WriteLine("No Northwind connection string");
        }

Upvotes: 5

Related Questions