Error connecting to SQLServer database C # Web.config

2

I'm trying to make a .NET MVC application, but when I'm going to connect to the bank it's giving the following error:

  

An exception of type 'System.NullReferenceException' occurred in GerarXML.dll but was not handled in user code Additional information: Object reference not set to an instance of an object.

My Web.config has all the right paths, look at string of connection

  

add name="EfDbContext" connectionString="Data Source=192.168.230.14;Initial Catalog=db_nfe;User ID=**;Password=******" providerName="System.Data.SqlClient"

And my controller is like this

public SqlConnection sqlConn = 
    new SqlConnection(ConfigurationManager
                       .ConnectionStrings["connectionStrings"]
                         .ConnectionString);

And the only method the bank uses is this

private string ConsultaChaveXML(string chaves)
{
    sqlConn.Open();
    string sql = $@"SELECT * FROM db_nfe..nota_fiscal_empresa
            WHERE chave_acesso = '{chaves}'";

    SqlCommand selectChave = new SqlCommand(sql);
    //Campo do xml = dsc_xml_nota_fiscal
    SqlDataReader retornoSelect = selectChave.ExecuteReader();

    return retornoSelect["dsc_xml_nota_fiscal"].ToString();
}
private XDocument CriarXML(string xml, string chave)
{
    var xmlFile = new XDocument("root", xml);
    return xmlFile;
}
    
asked by anonymous 27.07.2017 / 01:08

1 answer

1

Your ConnectionString has the wrong key name :

switch:

public SqlConnection sqlConn = 
    new SqlConnection(ConfigurationManager
                       .ConnectionStrings["connectionStrings"]
                         .ConnectionString);

why:

public SqlConnection sqlConn = 
    new SqlConnection(ConfigurationManager
                       .ConnectionStrings["EfDbContext"]
                         .ConnectionString);
    
27.07.2017 / 01:21