Database connection string in MVC Core class

I'm trying to learn the MVC core 2.0 framework using c sharp and I'm coming from vb.net and asp.net forms.  To try and learn the language/framework, I've created a test MVC core 2.0 website.

Currently I'm trying to get a database connection in a class called DropDown (my data access layer).  

Inside of this DropDown class I've got a few methods that query a sql database using ado.net. That isn't a problem; however, I can't seem to pass the database connection information from the appsettings.json file.  I added this:

        public IConfiguration configuration;
        public HomeController(IConfiguration config)
        {
            this.configuration = config;
          
        }

Open in new window


To my home controller to see if it returns a value and it does; however, this same code will not work in my DropDown class.
This is (more than likely) because I had to overload the DropDown method and the one I want doesn't ever get hit.

...Any help would be appreciated...THANKS!

I've tried multiple ways, but the most current is this:
appsettings.json
{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }

  },
  "ConnectionStrings": {
    "TestDB": "Server=localhost\\sqlexpress; Database=Test; Trusted_Connection=true; MultipleActiveResultSets=true"

  }

}

Open in new window


HomeController:
namespace MyFirstAspNetCoreApp.Controllers
{

    public class HomeController : Controller
    {

      //Added for testing purposes.
        public IConfiguration configuration;
        public HomeController(IConfiguration config)
        {
            this.configuration = config;
           (this.configuration, "DefaultConnection");
        }


        DataAccess.DropDown Get_DropDown = new DataAccess.DropDown();

      public IActionResult DropDown() {

            DataSet ds = Get_DropDown.Get_State();
            List<SelectListItem> stateList = new List<SelectListItem>();

            foreach (DataRow dr in ds.Tables[0].Rows)

            {

                stateList.Add(new SelectListItem { Text = dr["State"].ToString(), Value = dr["StateName"].ToString() });

            }

            ViewBag.State = stateList;

            return View();
        }//DropDown

Open in new window


and my DropDownclass:
amespace MyFirstAspNetCoreApp.DataAccess
{


    public class DropDown
        {

        private readonly IConfiguration configuration;

        public DropDown()
        {
        }

        public DropDown(IConfiguration config)
        {
            this.configuration = config;
        }



        public DataSet Get_State() //Country

        {
            string conStr = configuration.GetConnectionString("TestDB");
            // string conStr = _iconfiguration.GetSection("Data").GetSection("TestDB").Value; 
            //string conStr = configuration["Data:DefaultConnection:TestDB"];

            SqlConnection conSql = new SqlConnection(conStr);

            SqlCommand com = new SqlCommand("Select * from State", conSql);

            SqlDataAdapter da = new SqlDataAdapter(com);

            DataSet ds = new DataSet();

            da.Fill(ds);

            return ds;

        }// Ends State
         .....

Open in new window

LVL 1
badrhinoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.