Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net Core 2.0 ArgumentNullException: Value cannot be null. Parameter name: connectionString

I'm using ASP.NET Core 2.0, Visual Studio 2017 Enterprise, Version 15.5.4 and a local DB on my PC.

I'm working for the first time with database first and I have encountered the following problem:

unhandled exception occurred while processing the request.
ArgumentNullException: Value cannot be null.
Parameter name: connectionString.

The problem remains after reading and trying every possible suggestions along with possible solutions.

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {    
        services.AddMvc();
        services.AddDbContext<VideosContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}
    {
      "ConnectionString": {
        "DefaultConnection": "Server =(localdb)\\mssqllocaldb;Database=Videos;Trusted_Connection=True;MultipleActiveResultSets=true"
    },
    "Logging": {
      "IncludeScopes": false,
      "LogLevel": {
        "Default": "Warning"
      }
    }
}

public static void Main(string[] args)
{
     BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
   WebHost.CreateDefaultBuilder(args)
      .UseStartup<Startup>()
      .Build(); 

What am I doing wrong? Thank you in advance for any suggestion.

like image 999
TAWH Avatar asked Nov 07 '22 11:11

TAWH


1 Answers

Does your app settings JSON file have the correct syntax?

{
    "ConnectionStrings": {
        "DefaultConnection": "Server =(localdb)\\mssqllocaldb;Database=Videos;Trusted_Connection=True;MultipleActiveResultSets=true"
    }
}
like image 110
zola25 Avatar answered Nov 15 '22 07:11

zola25