Click here to Skip to main content
15,881,730 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to connect to my sql server through WPF application. i defined a connectionstring in my MYOWN.COnfig file but i am not able to access it in my code.

i have already added reference to System.Configuration in my refrence folder my config looks like

"in configuration section i have defined my connectionstring as below
-configuration-
-configsections-
-/configsections-

-connectionstrings-
-add name="con" connectionstring="Data Source=MyserverName;Initial<br mode=" hold=" /> Catalog=myDBName;Integrated Security=false;User Id=AdminUser;<br mode=" providername="System.Data.SqlClient" /-

-/connectionstrings-


/configuration-"

please help

Thanks in advance. if you can mail me any samples that will be a great help
Posted
Updated 20-Aug-15 19:29pm
v2
Comments
Suvendu Shekhar Giri 6-Sep-15 8:10am    
Why not app.config?
Any specific reason for that?

1 solution

It is recommended to use app.config instead of such config unless there is truly exists demand for such design.

You need to map the config file through ExeConfigurationFileMap Class[^]

Try this-
C#
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = @"D:\YourFolder\myown.config";
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
string constring=config.AppSettings.Settings["con"].Value.ToString();


Hope, it helps :)
 
Share this answer
 

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