question

JerryLipan-2008 avatar image
0 Votes"
JerryLipan-2008 asked JerryLipan-2008 commented

Calling appsettings.json from Controller

114909-001.png

Why ?

This is my appsettings.json

 {
   "connectionStrings": {
     "defaultConnection": "Data Source=DESKTOP-5EFGK9O;Initial Catalog=Company;user id=dbCompany;password=6181;Connect Timeout=30;Encrypt=False;"
   },
   "Logging": {
     "LogLevel": {
       "Default": "Information",
       "Microsoft": "Warning",
       "Microsoft.Hosting.Lifetime": "Information"
     }
   },
   "AllowedHosts": "*"
 }


The Home Controller is,

 using AppsCore.Models;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.Extensions.Logging;
 using System;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Linq;
 using System.Threading.Tasks;
 using System.ComponentModel.DataAnnotations;
 using System.Data;
 using System.Data.SqlClient;
 using Microsoft.Extensions.Configuration;
    
 namespace AppsCore.Controllers
 {
     public class HomeController : Controller
     {
         private IConfiguration Configuration;
         private readonly ILogger<HomeController> _logger;
    
         public HomeController(IConfiguration _configuration, ILogger<HomeController> logger)
         {
             Configuration = _configuration;
             _logger = logger;
         }
    
         public HomeController()
         {
    
         }
    
         public IActionResult Index()
         {
             string ConnStr = this.Configuration.GetConnectionString("defaultConnection");
    
             ViewBag.dsGrid = Employee.GetAll(ConnStr).ToArray();
             ViewBag.dsDepartment = Department.GetAll(ConnStr).ToArray();
             return View();
         }
    
         public ActionResult Insert([FromBody] CRUDModel<Employee> value)
         {
             string ConnStr = this.Configuration.GetConnectionString("defaultConnection");
    
             var emp = value.value;
    
             //using (SqlConnection sql = new SqlConnection("Data Source=DESKTOP-5EFGK9O;Initial Catalog=Company;user id=dbCompany;password=6181;Connect Timeout=30;Encrypt=False;"))
             using (SqlConnection sql = new SqlConnection(ConnStr))
             {
                 using (SqlCommand cmd = new SqlCommand("InsertEmployee", sql))
                 {
                     cmd.CommandType = System.Data.CommandType.StoredProcedure;
                     sql.Open();
                     cmd.Parameters.AddWithValue("@DepartmentId", emp.DepartmentId);
                     cmd.Parameters.AddWithValue("@Name", emp.EmployeeName);
                     cmd.Parameters.AddWithValue("@Designation", emp.Designation);
    
                     SqlParameter newSqlParam2 = new SqlParameter();
                     newSqlParam2.ParameterName = "@EmployeeId";
                     newSqlParam2.SqlDbType = SqlDbType.Int;
                     newSqlParam2.Direction = ParameterDirection.Output;
                     cmd.Parameters.Add(newSqlParam2);
    
                     cmd.ExecuteNonQuery();
    
                     emp.EmployeeId = (int)cmd.Parameters["@EmployeeId"].Value;
    
                     sql.Close();
                 }
             }
    
             ViewBag.dsGrid = Employee.GetAll(ConnStr).ToArray();
    
             return Json(emp);
         }
    
    
         public IActionResult Privacy()
         {
             return View();
         }
    
         [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
         public IActionResult Error()
         {
             return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
         }
    
    
    
         public class CRUDModel<T> where T : class
         {
             public string action { get; set; }
    
             public string table { get; set; }
    
             public string keyColumn { get; set; }
    
             public object key { get; set; }
    
             public T value { get; set; }
    
             public List<T> added { get; set; }
    
             public List<T> changed { get; set; }
    
             public List<T> deleted { get; set; }
    
             public IDictionary<string, object> @params { get; set; }
         }
    
         public class Employee
         {
             public static List<Employee> emp = new List<Employee>();
             public Employee()
             {
    
             }
             public Employee(int EmployeeId, int DepartmentId, string DepartmentName, string EmployeeName,
             string Designation)
             {
                 this.EmployeeId = EmployeeId;
                 this.DepartmentId = DepartmentId;
                 this.DepartmentName = DepartmentName;
                 this.EmployeeName = EmployeeName;
                 this.Designation = Designation;
             }
    
             public int? EmployeeId { get; set; }
             public int? DepartmentId { get; set; }
             public string DepartmentName { get; set; }
    
             [Required(ErrorMessage = "Employee Name is required")]
             [StringLength(50)]
             public string EmployeeName { get; set; }
             public string Designation { get; set; }
    
    
             public static List<Employee> GetAll(string ConnStr)
             {               
                 if (emp.Count() == 0)
                 {
                     //using (SqlConnection sql = new SqlConnection("Data Source=DESKTOP-5EFGK9O;Initial Catalog=Company;user id=dbCompany;password=6181;Connect Timeout=30;Encrypt=False;"))
                     using (SqlConnection sql = new SqlConnection(ConnStr))
                     {
                         using (SqlCommand cmd = new SqlCommand("getEmployee", sql))
                         {
                             cmd.CommandType = System.Data.CommandType.StoredProcedure;
                             sql.Open();
    
                             using (var reader = cmd.ExecuteReader())
                             {
                                 while (reader.Read())
                                 {
                                     emp.Add(new Employee(
    
                                             (int)reader["Id"],
                                             (int)reader["DepartmentId"],
                                             reader["DepartmentName"].ToString(),
                                             reader["Name"].ToString(),
                                             reader["Designation"].ToString()
                                         ));
                                 }
                             }
    
                             sql.Close();
    
                         }
                     }
    
                 }
                 return emp;
             }
    
                
         }
    
         public class Department
         {
             public static List<Department> dpm = new List<Department>();
             public Department()
             {
    
             }
             public Department(int DepartmentId, string DepartmentName)
             {
                 this.DepartmentId = DepartmentId;
                 this.DepartmentName = DepartmentName;
             }
    
             public int? DepartmentId { get; set; }
             public string DepartmentName { get; set; }
    
             public static List<Department> GetAll(string ConnStr)
             {
                 if (dpm.Count() == 0)
                 {
                     //using (SqlConnection sql = new SqlConnection("Data Source=DESKTOP-5EFGK9O;Initial Catalog=Company;user id=dbCompany;password=6181;Connect Timeout=30;Encrypt=False;"))
                     using (SqlConnection sql = new SqlConnection(ConnStr))
                     {
                         using (SqlCommand cmd = new SqlCommand("getDeparment", sql))
                         {
                             cmd.CommandType = System.Data.CommandType.StoredProcedure;
                             sql.Open();
    
                             using (var reader = cmd.ExecuteReader())
                             {
                                 while (reader.Read())
                                 {
                                     dpm.Add(new Department(
    
                                             (int)reader["Id"],
                                             reader["Name"].ToString()
                                         ));
                                 }
                             }
    
                             sql.Close();
    
                         }
                     }
    
                 }
                 return dpm;
             }
    
    
         }
    
    
    
    
    
    
     }
 }

Please help


dotnet-aspnet-core-mvc
001.png (76.8 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Remove the parameterless constructor below.

          public HomeController()
          {
        
          }


0 Votes 0 ·

@AgaveJoe Sir,

Yes. It workable

0 Votes 0 ·

1 Answer

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.