Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello.
i have some problem with retriving connection string from aapcofig file.
please help me.
i m giving codes below.

<in config="" file="">

XML
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <connectionStrings>

    <add name="con" connectionString="Server=.;Database=prince;Integrated Security=true"/>

  </connectionStrings>

</configuration>





(Code Behind)


string connection = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
but its not working.
i have also used
C#
using System.Configuration;
using System.Data.SqlClient;


please help me
regards
dibyaranjan
Posted
Updated 21-Feb-13 23:28pm
v2
Comments
Om Prakash Pant 22-Feb-13 6:01am    
try this:
ConfigurationManager.ConnectionStrings["con"].ToString();
José Amílcar Casimiro 22-Feb-13 6:03am    
Are you getting any error ?
Orcun Iyigun 22-Feb-13 6:07am    
What is the error?
Shubh Agrahari 22-Feb-13 7:40am    
this is basically in window application guys......1st understand the exactly need of code seeker....

Hi all

1st of all i want two share that web.config and app.config is two different files that use different different role 1st one for web application and 2nd one is for window application ....

there is two way to achieve connection string by Integrated Security ="true" or by setting User ID and Password...

you have to try this one

XML
<configuration>
  <appsettings>
    <add key="con" value="server=.;User Id=root;password=password;database=prince;" />
  </appsettings>
</configuration>


happy to help.
 
Share this answer
 
Comments
[no name] 22-Feb-13 7:54am    
+5
Shubh Agrahari 22-Feb-13 8:15am    
thanks boss always ready to help...
here is the way to give connection string


<appsettings>
<connectionstrings>



XML
<configuration>
<pre lang="HTML">
 <add name="mmisConnectionString" connectionString="server=localhost;User Id=root;password=password;database=mmis" providerName="MySql.Data.MySqlClient" />


</connectionStrings>

</configuration>
 
Share this answer
 
v2
Comments
Orcun Iyigun 22-Feb-13 6:18am    
the provider name is not a necessity.
[no name] 22-Feb-13 6:22am    
ya but also no any error issue at all with provider name.....
If for all above solution your code is still not working then

You need to add referece to reference folder in solution explorer for System.Configuration
 
Share this answer
 
HI
XML
<connectionstrings>
    <add name="con" connectionstring="Data Source=localhost;Initial Catalog=DataBaseName;Integrated Security=True;" providername="System.Data.SqlClient" />
  </connectionstrings>


localhost or your computername
 
Share this answer
 
HI,

Use the following code in your webconfig file :

C#
<configuration>
  <appsettings>
    <add key="conn" value="connection string here" />    
  </appsettings>
  </configuration>


From the code behind you can able to access that like the following code.

myConnection = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["conn"]);


Thanks
 
Share this answer
 
v2

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