Click here to Skip to main content
15,896,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii... I'm developing WPF application..I want to know how to declare connectionstring in App.Config..Please help me out..

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Demo_wpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {

        }




    }
}


app config Code:-
XML
<configuration>
  <connectionStrings>
    <add name="connectionString" connectionString="Data Source=ASHISH-PC;Initial Catalog=b;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>



But it gives an error that is:- The name 'ConfigurationManager' does not exist in the current context
Posted
Updated 15-Dec-17 2:20am

1 solution

Please read this:
How to get Connection String from App.Config in C#[^]

Make sure you added a reference to the System.Configuration namespace
as well as using System.Configuration;
to your source code.
 
Share this answer
 
v2
Comments
J{0}Y 27-Jan-15 4:38am    
i'm trying this but it's not working
TheRealSteveJudge 27-Jan-15 4:59am    
I could reproduce the error you reported.
Therefore I updated the solution.
J{0}Y 27-Jan-15 5:09am    
yup it's working thanks TheRealSteveJudge
TheRealSteveJudge 27-Jan-15 5:16am    
You're welcome.

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