0

I want to connect with a db with a connectionstring in an application config file but always get this error :

object reference not set to an object instance

In the first code line with the ConfigurationManger I have checked it several times but I do not think that I have made a mistake. Hope you can find something.

Code

string con = ConfigurationManager.ConnectionStrings["dbConnect"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(con))
            {
                SqlDataAdapter adapter = new SqlDataAdapter("Select* From dsds m with (nolock) inner join asdas a with (nolock) on a.id = m.id where  test....., conn);
                ds = new DataTable(" ");
                adapter.Fill(ds);
                dataGridView1.DataSource = ds;

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="dbConnect"
     connectionString="Data Source=test;Initial Catalog=as;Integrated Security=True"
     providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
1

1 Answer 1

2
string con = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnect"].ConnectionString;

Edit:

As you have two app.config, you can either delete one, or if you want to control programatically which app.config to use, Refer How can I use several Application Configuration Files in one project?

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

6 Comments

Have you referenced the System.Configuration dll in your project?
Do you have multiple app.config files?
yes i have otherwise i would not have get this error
Sorry. I could not get for which question it was Yes.
Can you please verify if System.Configuration is present in the References folder of your project?
|

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.