Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to connect Mysql Database to .netcore MVC project

$
0
0

Hello Guys,

I am new to .netcore 2.0 please help me on this, I have to connect a MySQL database but I am not able to connect it.i have used some references but it is still showing object reference not set.

Below are my some of the codes everytime I run the code I am getting at MySQL line IN COMPANY MODEL THAT OBJECT REFERENCE NOT SET.PLEASEHELP

App.setting

{
  "ConnectionStrings": {"DefaultConnection": "server=localhost;userid=root;password=admin123;database=mydb;"
 },"Logging": {"IncludeScopes": false,"LogLevel": {"Default": "Warning"
    }
  }
}
CompanyProfile Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using MySql.Web;
using MySql.Data.MySqlClient;
using System.Configuration;
using Microsoft.EntityFrameworkCore;


namespace NCSTEST.Models
{
    public class CompanyProfile
    {

        private NCSDBContext context;

        public int num_id { get; set; }
        public string str_cmp_name { get; set; }
        public string str_cmp_code { get; set; }
        public DataSet Tempstore { get; set; }


        MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);

         MySqlCommand cmd = new MySqlCommand();

        public DataSet DisplayCompany()
        {

            DataSet ds = new DataSet();
            cmd.CommandText = "select * from mydb.tb_company_master";
            cmd.Connection = con;
            try
            {
                con.Open();
                MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                da.Fill(ds);
                return ds;
            }
            catch (Exception es)
            {
                throw es;
            }
        }
    }

}
    


Company Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NCSTEST.Models;
using System.Data;

namespace NCSTEST.Controllers
{
    public class CompanyController : Controller
    {
        CompanyProfile obj = new CompanyProfile();
        public IActionResult Company(CompanyProfile CP )
        {

            CP.Tempstore = obj.DisplayCompany();
            return View(CP);
        }
    }
}
startup.cs

using Microsoft.Extensions.DependencyInjection;
using MySql.Data;
using MySql.Data.MySqlClient;
using Microsoft.EntityFrameworkCore;
using MySql.Data.EntityFrameworkCore.Extensions;
using Microsoft.Extensions.Logging;
using NCSTEST.Models;

namespace NCSTEST
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
               .SetBasePath(env.ContentRootPath)
               .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
               .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
               .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.Add(new ServiceDescriptor(typeof(NCSDBContext), new NCSDBContext(Configuration.GetConnectionString("DefaultConnection"))));

        }


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>