Connectionstring returning null

Asked

Viewed 291 times

0

When trying to run my project is returned me the error:

System.Nullreferenceexception: 'Object reference not set to an instance of an object.'

I have a bank class with the manufacturer:

public bd()
{
    conexao = new SqlConnection(ConfigurationManager.ConnectionStrings["conexaoBD"].ConnectionString);
    conexao.Open();
}

I am using Visual Studio 2017, can anyone help me in this error? Thanks in advance.

  • App.config? Is this in the layer of your Asp.net? See if your App.config is going to Embedded Properties -> Build Actions

  • is in the repository layer

  • files . config serves to configure an executable, it is not interesting to have it in a library layer, such as a repository. Your connection string must exist within the layer that the executable exists. Try to place your connectionstring inside your web.config

  • Even in the Asp.net layer returns the same error...

  • Thank you very much Gabriel, that’s right, he has to stay inside the web.config, is that he was watching a video class only that the person put inside the app.config but it was a console project. Thanks for the help!

  • @Desalex you might be interested in: https://www.connectionstrings.com/

Show 1 more comment

1 answer

2


Your connectionstring must exist within the project that has the executable, in the case of Asp.net must exist within the web.config or other file. config that is referenced by web.config.

In your case the problem was that, when installing the Entity Framework package, it created an app.config in the layer and you were trying to configure it in this location.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.