Abraham Olatubosun

Abraham Olatubosun

  • NA
  • 471
  • 113.6k

IConfiguration not working

Dec 21 2019 3:55 PM
My dear GURU's, I hope this meet you all in good health.
 
I am trying to get my connection string from appsetting.json but it returning 'null'
here are my appsetting.json
  1. {  
  2.   "Logging": {  
  3.     "LogLevel": {  
  4.       "Default": "Warning"  
  5.     }  
  6.   },  
  7.   "AllowedHosts": "*",  
  8.   "ConnectionString": {  
  9.     "IQCare": "Data Source=.\\SQLEXPRESS;Initial Catalog=patientdb;Integrated Security=True;",  
  10.     "NMRSConn": "Data Source=localhost;Port=3306Database=openmrs; User Id=openmrsPassword=Admin123;"  
  11.   }  
  12.     
  13. }  
my controller from where i want to access the connection string
  1. /* 
  2.         * Create Dipendency Injection into the controller 
  3.         * */  
  4.   
  5.        public  IConfiguration Configuration { get; }  
  6.        public MigrateController(IConfiguration configuration)  
  7.        {  
  8.            Configuration = configuration;  
  9.        }  
  10.         
  11.  
  12.        #region ---- Create connection access method   
  13.        public SqlConnection GetSqlConn()  
  14.        {  
  15.            var connStr = Configuration["IQCare"];  
  16.            return new SqlConnection(connStr);  
  17.        }  
  18.   
  19.        public MySqlConnection GetMySqlConn()  
  20.        {  
  21.            var connStr = Configuration["NMRSConn"];  
  22.            return new MySqlConnection(connStr);  
  23.        }  
  24.   
  25. using (SqlConnection connection = GetSqlConn())  
  26.                {  
  27.                    connection.Open();  
  28.                    var sql = "select idno, facid, fac_name, state_id from tbl_facility where state_id = '"+dd.Trim()+"'";  
  29.                    using (SqlCommand cmd = new SqlCommand(sql, connection))  
  30.                    {  
  31.                        using (SqlDataReader reader = cmd.ExecuteReader())  
  32.                        {  
  33.                            if (reader.HasRows)  
  34.                            {  
  35.   
  36.                                while (reader.Read())  
  37.                                {  
  38.                                    connE.Facilities.Add(new Facility  
  39.                                    {  
  40.   
  41.                                        FacilityName = reader["fac_name"].ToString(),  
  42.                                        DatimCode = reader["facid"].ToString(),  
  43.                                        State = reader["state_id"].ToString()  
  44.                                    });  
  45.                                }  
  46.                            }  
  47.                        }  
  48.                    }  
  49.                }  
can anyone help me see why GetSqlConn() is returning 'null'
 
Thank you all 

Answers (1)