Connection problem with the database
System.NullReferenceException: 'The object reference is not defined to an instance of an object.'
System.Configuration.ConnectionStringSettingsCollection.this[string].get returned null.
My connection string :
<add name="Constring" connectionString="data source=.; database = namedatabase; integrated security = SSPI" providerName="System.Data.EntityClient" />
Code :
public ActionResult Affhicherinfo(string code)
{
List<Resultat> infogenes = new List<Resultat>();
string constr = ConfigurationManager.ConnectionStrings["sConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = " SELECT Nom,Id FROM Information_General where nom_de_site= @code ";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con; con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
infogenes.Add(new Resultat { Nom = sdr["Nom"].ToString(), Id = Convert.ToInt32(sdr["Id"]) });
}
}
con.Close();
}
}
return View(infogenes);
}