public class HomeController : Controller
{
public IConfigurationRoot GetConnection()
{
var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appSettings.json").Build();
return builder;
}
public IActionResult Index()
{ Dropdownlist multi_Dropdownlist = new Dropdownlist
{
custlist = GetCustomerList(),
emplist = GetEmployeeList()
};
return View(multi_Dropdownlist);
}
public List<Customer_list> GetCustomerList()
{
var connection = GetConnection().GetSection("ConnectionStrings").GetSection("DefaultConnection").Value;
SqlConnection con = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand("Select customerId,FirstName as Name From Customers", con);
con.Open();
SqlDataReader idr = cmd.ExecuteReader();
List<Customer_list> customers = new List<Customer_list>();
if (idr.HasRows)
{
while (idr.Read())
{
customers.Add(new Customer_list
{
custId = Convert.ToInt32(idr["customerId"]),
custName = Convert.ToString(idr["Name"]),
});
}
}
con.Close();
return customers;
}
public List<Employee_list> GetEmployeeList()
{
var connection = GetConnection().GetSection("ConnectionStrings").GetSection("DefaultConnection").Value;
SqlConnection con = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand("Select * From Employees", con);
con.Open();
SqlDataReader idr = cmd.ExecuteReader();
List<Employee_list> Employees = new List<Employee_list>();
if (idr.HasRows)
{
while (idr.Read())
{
Employees.Add(new Employee_list
{
Empid = Convert.ToInt32(idr["EmpoyeeId"]),
EmpName = Convert.ToString(idr["EmpName"]),
});
}
}
con.Close();
return Employees;
}
}