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>
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
myworld
  • 23
  • 2
  • 10
  • 1
    Possible duplicate of [What does "Object reference not set to an instance of an object" mean?](http://stackoverflow.com/questions/779091/what-does-object-reference-not-set-to-an-instance-of-an-object-mean) – ViRuSTriNiTy Aug 23 '16 at 10:11

1 Answers1

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?

Community
  • 1
  • 1
Richa Garg
  • 1,888
  • 12
  • 23