Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to access connectionString from app.config with winform application?

I write code to access connectionString but it gives error.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace WinAppThreeTier.DAL
{
  public class UserDetailDAL
    {
      SqlConnection conn = new SqlConnection();

      public void getConnection()
      {
////1st way to access ConnectionString

      conn.ConnectionString=System.Configuration.ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
      //this line gives error ConfigurationManager does't exits in the namespace System.Configuration;

////2nd way to access ConnectionString

      conn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["MyDBConnectionString"];
          //This line gives Warning.. System.Configuration.ConfigurationSettings.AppSettings' is obsolete: ' This method is obsolete,
          //it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings'
Posted
Updated 11-Nov-13 12:42pm
v3

Don't. Do it the easy way!
Open your solution in VS, and look in the Solution Explorer pane.
Open your project branch, and the Properties sub branch.
Double click "settings.settings"
In the dialog that appears, type the name of the setting: "MyDBConnection"
Set the Type to String, and the Scope to "User". Set teh default to a working config string.
Save and close the dialog.

In your code:
C#
string connectionString = Properties.Settings.Default.MyDBConnection;
will get you the connection string, and you can change it with:
C#
Properties.Settings.Default.MyDBConnection = connectionString;
Properties.Settings.Default.Save();
 
Share this answer
 
Comments
mangesh 21 11-Nov-13 15:31pm    
In the dialog that appears, What to set in Value column ?? (column) Value means could I have to set there Connectionstring as we write in app.config or anything else ?? or blank ??
OriginalGriff 11-Nov-13 15:46pm    
See above: "Set teh default to a working config string."
Hello if you are using 3 tier Architecture,put the below code inside your DataAccessLayer(DAL).



XML
Open the App.config put the below code
<pre lang="c#"><?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>

    <add name="Constr" connectionString="Server=.\SQLEXPRESS; Integrated Security=true;Initial Catalog=IMS"/>
  </connectionStrings>
</configuration></pre>

IDispose

<pre lang="c#">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
   public interface IDispose
    {
       void Dispose();
    }
}
</pre>


Base Class

C#
public abstract class BaseClass :IDispose
    {
       public SqlConnection sqlcon;
       public SqlCommand sqlcm;
       protected SqlDataReader sqlDr;

       public void Dispose()
       {
           sqlcon.Close();
       }
    }


Method Class


C#
public class Method :BaseClass
    {
        public Method()
        {
            string Connectionstring = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
            sqlcon=new SqlConnection(Connectionstring);
            sqlcon.Open();
            
        }
        public Method(bool isMaster)
        {
            string Connectionstring = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
            sqlcon = new SqlConnection(Connectionstring);
            sqlcon.Open();
        }
        public Method(string ConnectionString)
        {
            sqlcon = new SqlConnection(ConnectionString);
            sqlcon.Open();
        }
        public SqlDataReader ExecuteReader()
        {
            sqlcm.Connection = sqlcon;
            sqlDr = sqlcm.ExecuteReader();
            return sqlDr;
        }
        public DataTable GetDataTable()
        {
            DataTable Dt;
            DataSet Ds = FillData();
            Dt = Ds.Tables[0];
            return Dt;
        }



C#
public class SqlDataBase : Method
    {
       public SqlDataBase()
       {
           string Connectionstring = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
           sqlcon = new SqlConnection(Connectionstring);
           sqlcon.Open();
       }

       public SqlDataBase(string ConnectionString)
       {
           ConnectionString = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
           sqlcon = new SqlConnection(ConnectionString);
           sqlcon.Open();
       }

    }


Accessing Connection string

C#
public static bool Insert(PersonelData person)
     {
         const string insertdetails = "exec[INSERT_PERSONEL]@sid,@FName,@MName,@LName";
         DAL.Method method = new DAL.SqlDataBase();
         method.SelectQuery(insertdetails);
         method.AddParameter("@sid", person.SID);
         method.AddParameter("@FName", person.FName);
         method.AddParameter("@MName",person.MName );
         method.AddParameter("@LName",person.LName);
         int ins = method.ExecuteQuery();

         if (ins > 0)
         {
             return true;
         }

         return false;
       }
 
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