Im getting error at HomeController as
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Configuration.ConnectionStringSettingsCollection.this[string].get returned null.
can you please help me in solving this?
HomeController.cs
using jQuery_AJAX_WebAPI_MVC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using jQuery_AJAX_MVC.Models;
using System.Runtime.Remoting.Contexts;
using Microsoft.JScript;
using System.Data.SqlClient;
using System.Configuration;
namespace jQuery_AJAX_WebAPI_MVC.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult HomePage()
{
return View();
}
private List<SelectListItem> GetEmployeeListSelectListItem(List<Department> dep)
{
List<SelectListItem> items = new List<SelectListItem>();
foreach (Department d in dep)
{
items.Add(new SelectListItem
{
Text = d.DEPNAME,
Value = d.DEPID.ToString()
});
}
return items;
}
public List<Department> GetEmployeeList()
{
List<Department> items = new List<Department>();
string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = " SELECT DEPID,DEPNAME,MANAGER FROM Department";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
items.Add(new Department
{
Text = sdr["DEPNAME"].ToString(),
Value = sdr["DEPID"].ToString()
});
}
}
con.Close();
}
}
return items;
}
}
}
web.config
<connectionStrings>
<add name="EmployeeEntities10" connectionString="metadata=res://*/Models.ModelData.csdl|res://*/Models.ModelData.ssdl|res://*/Models.ModelData.msl;provider=System.Data.SqlClient;provider connection string="data source=LENOVO-PC\MSSQL_SERVER;initial catalog=Employee;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
in connection string I can see
Data Source=LENOVO-PC\MSSQL_SERVER;Initial Catalog=Employee;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework