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

3
  • You may want to change the elements in your config to capital letters. It might be case sensitive.. Not sure. Commented Mar 21, 2018 at 15:12
  • 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. Commented Mar 21, 2018 at 15:19
  • Kindly see my edit. I have added the solution explorer. Commented Mar 21, 2018 at 15:25

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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>

2 Comments

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).
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.
0

Try to change in the App.config:

1 Comment

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" />
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.