Click here to Skip to main content
15,898,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I get a connection string in cs file in window file. If I create connectionstring in app config file .
XML
<add name="ROMS.Properties.Settings.DBRomsConnectionString" connectionstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Resources\DBRoms.mdb;Persist Security Info=True" providername="System.Data.OleDb" />
Always it creates problem when we move project one system to another system so I want to set some specific path to project so it call constant path every time.
Posted
Updated 29-Jan-14 6:36am
v3
Comments
Kornfeld Eliyahu Peter 28-Jan-14 5:03am    
Can you show a connection string that creates problem and elaborate on the nature of the problem?

Write it in your code
string con =ConfigurationManager.connectionstring[dbcon].connectionstring();
 
Share this answer
 
Comments
Rohit85 28-Jan-14 5:54am    
In window Configurationmanager shows error
NitishBharadwaj 28-Jan-14 7:43am    
Post ur error message...
you can use "Environment.SpecialFolder.LocalApplicationData" for such things:

C#
string path = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),this.AssemblyCompany), this.AssemblyProduct);


A very basic usage:

C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test_Path1
{
    class Program
    {
        public static string AssemblyCompany = "Company1";
        public static string AssemblyProduct = "App1";
        static void Main(string[] args)
        {
            string connStr = @"Data Source=.\sqlexpress; initial catalog=TEST; integrated security=true;";
            string myweb = @"http://google.com";

            string allConfig = connStr + Environment.NewLine + myweb;
            MyConfig cfg = new MyConfig();
            cfg.Save(allConfig);

            string[] rows = cfg.Read();

            Console.WriteLine(rows[0]);
            Console.WriteLine(rows[1]);
        }
    }

    class MyConfig
    {
        public string ConfigPath
        {
            get
            {
                return Path.Combine(Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                    Program.AssemblyCompany), Program.AssemblyProduct);
            }
        }

        public MyConfig()
        {
            if (!Directory.Exists(this.ConfigPath))
                Directory.CreateDirectory(this.ConfigPath);
        }

        public void Save(string config)
        {
            using (StreamWriter s = new StreamWriter(Path.Combine(this.ConfigPath, "MyConfig.txt")))
            {
                s.Write(config);
            }
        }

        public string[] Read()
        {
            using (StreamReader s = new StreamReader(Path.Combine(this.ConfigPath, "MyConfig.txt")))
            {
                return s.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            }
        }

    }
}
 
Share this answer
 
v2
Comments
Rohit85 28-Jan-14 6:04am    
Plz Give me such example it show error .
Vedat Ozan Oner 28-Jan-14 10:17am    
what error?
Rohit85 29-Jan-14 5:48am    
i m Uisng this in file.cs file connection error
where should i put this string .
Vedat Ozan Oner 29-Jan-14 6:22am    
what connection error? how do you use it? What I did is simply to use this path for application-specific configuration files or other resources. you can save your connection string into a text file in that directory.
Rohit85 29-Jan-14 6:44am    
Simpley i use , i add appconfig file in my window project . than i write connection string using access . Microsoft OleDb . than i write this line in my form2.cs code page.
can you give me some hint to where should i use this ditails
If you want to get Connection string from appconfig, try this :
C#
var connectionString = ConfigurationManager.ConnectionStrings["Name"];
 
Share this answer
 
Comments
Rohit85 28-Jan-14 6:05am    
it show red line under ConfigurationManager
dharam1 28-Jan-14 6:08am    
add reference System.Configuration;
Rohit85 28-Jan-14 6:35am    
i add this but error show in word configuration manager
dharam1 28-Jan-14 7:32am    
now try below one
string connectionString = ConfigurationManager.ConnectionStrings["Name"].ConnectionString;
Alexander Dymshyts 29-Jan-14 11:10am    
do you still have error?
// in web config file
<appSettings>
<add key="TESTCon" value="Data Source=COM1;Initial Catalog=TEST;User ID=sa;Password=test@123"/>
</appSettings>

// In cs file
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["TESTCon"]);
 
Share this answer
 
sample/cn.rar

AddCity
appcodecity
City
CityCode
conn
EmailHandler
packagedetail
 
Share this answer
 
C#
public static SqlConnection GetConnection()
        {
            string ConnectionString = null;

            SqlConnection myConn = null;

            try
            {
                ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ToString();
                myConn = new SqlConnection(ConnectionString);
            }
            catch (Exception e)
            {
                throw e;
            }
             return myConn;
        }



Include Namespaces:-
C#
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;



And in the web.config

XML
<connectionStrings>
    
    <add name="connStr" providerName="System.Data.SqlClient" connectionString="data source=YOUR PC NAME;initial catalog=YOUR DATABASE NAME;user id=USERID OF CONNECTION TO DB;password=PASSWORD OF CONNECTION TO DB;"/>
  </connectionStrings>
 
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