Click here to Skip to main content
16,022,368 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am currently developing a simple web application with using ASP.net c#.

I am building a connectionstring and to check if if have a valid or existing connectionstring in web.config file.

C#
class WebVacationManagerDac
{
   Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");
   ConnectionStringSettings connString;

   if (config.ConnectionStrings.ConnectionStrings.Count > 0)
   {
      connString = config.ConnectionStrings.ConnectionStrings["FTDP"];
   }
}


The problem is have is that I declared 'config' and try to check if there is one connectionString in web.config. however, I get an error saying "WebVacationManagerDac.config it is 'field' but is used like a 'type'" within IF Statement

Any suggestion or advice??? Please
Posted
Updated 3-Nov-14 11:51am
v3

You have to write connection String in Web.config under Configuration Section Like following:
<connectionstrings>
<add name="ConStr" connectionstring="Server=.;DataBase=test;integrated Security=true" providername="System.Data.SqlClient">

And Access the connection String in code behind like following:

C#
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString);

OR

C#
string str=ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
Now you can use "str" as connection String any where
 
Share this answer
 
v2
ConnectionStrings.ConnectionStrings ?
 
Share this answer
 
Comments
stephenak 3-Nov-14 14:20pm    
Hi, I got ConnectionStrings.ConnectionStrings from http://msdn.microsoft.com/en-us/library/ms178411(v=vs.100).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1 website
PIEBALDconsult 3-Nov-14 14:25pm    
Don't trust stuff you found lying around the 'net. If you are using Visual Studio (or another modern IDE), then what does it tell you about the code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900