How to add connection string in Config File in WPF Application
Asked Answered
M

4

6

I am a beginner and learning WPF Application. I have a simple project and in that I want to read DB Configuration string from App.Config File. But I am not able to do so. Below is my attempt:

APP.Config File:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
  <connectionStrings>
    <add name="DBCS" connectionString="Data Source=.\;Initial Catalog=Connect;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

CS Code:

    public static void GetDataFromDB()
            {
                //var CS = @"Data Source=.\;Initial Catalog=Connect;Integrated Security=SSPI";
               // ABOVE CODE WORKS FINE
                string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                using (SqlConnection con = new SqlConnection(CS))
                {
                    con.Open();
                    SqlDataAdapter da = new SqlDataAdapter("Select * from tblTenant", con);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                }
            } 

enter image description here

Edit:

enter image description here

Memorabilia answered 21/3, 2018 at 15:10 Comment(3)
You may want to change the elements in your config to capital letters. It might be case sensitive.. Not sure.Reunite
I have a ASP.Net web project their I have EXACT same settings and it works fine. :-|. Only difference there I have added this in web.config file.Memorabilia
Kindly see my edit. I have added the solution explorer.Memorabilia
C
8

You need to put the connnection string in the App.config of the running WPF application and not in the DAL or any other class library.

The ConfigurationManager class reads the configuration file of the running executable.

Codding answered 21/3, 2018 at 15:26 Comment(0)
A
0

Add "clear" to your app.config before the connections string definition. It will look like this :

<connectionStrings>
  <clear/>
  <add name="DBCS" connectionString="Data Source=.\;Initial Catalog=Connect;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>
Andie answered 21/3, 2018 at 15:22 Comment(2)
Can you be more explicit? Still a null reference, or a different error? I put the same thing into a WPF application and it was fine. Presumably your configuration manager is System.Configuration.ConfigurationManager? Also, if you look at the ConfigurationManager.ConnectionStrings collection, is it empty? In my code, I reference the collection by index, not name, i.e. ConnectionStrings[0].ConnectionString (But actually, both work).Andie
I did silly mistake. Actually my app.config file was in different project which was added as a class library. Sorry for the confusion. I am a beginner so I did not know that.Memorabilia
T
0

Try to change in the App.config:

Tetrachloride answered 21/3, 2018 at 15:25 Comment(1)
Try than in the App.config to replace with: <add name="DBCS" connectionString="Data Source=.\;Initial Catalog=Connect;Integrated Security=SSPI" providerName="System.Data.SqlClient" />Tetrachloride
S
0

You can add Connection string to both DAL and UI projects.

Try removing Data Source =.\ to Data Source =.

Saltcellar answered 19/4, 2021 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.