Ad Code

Responsive Advertisement

C# Error 04: The name 'ConfigurationManager' does not exist in the current context SchedulerToClearDataAndQueue

C# Error 04: The name 'ConfigurationManager' does not exist in the current context SchedulerToClearDataAndQueue exception  occurred in C# 

The error "The name 'ConfigurationManager' does not exist in the current context" typically occurs because the required namespace is not included, or the necessary package is not referenced in your project. Below are the steps to resolve this issue in a .NET Framework application.


Step 1: Ensure the Correct Namespace is Included

Ensure that you have included the `System.Configuration` namespace in your code file:

using System.Configuration;

Step 2: Ensure the Correct Assembly Reference

Make sure your project references the `System.Configuration` assembly. Follow these steps:

1. In Visual Studio:
   - Right-click on your project in the Solution Explorer.
   - Select Add > Reference.
   - In the Reference Manager, go to Assemblies > Framework.
   - Find and check System.Configuration.
   - Click OK to add the reference.

Step 3: Check Your Project Type

The `ConfigurationManager` class is available in .NET Framework applications but not in .NET Core or .NET 5/6/7 projects by default. If you are using .NET Core or .NET 5+, you should use the `Microsoft.Extensions.Configuration` package instead.

Example Code for .NET Framework

Here’s an example of how to properly use `ConfigurationManager` in a .NET Framework application:

using System;
using System.Configuration;

namespace SchedulerToClearDataAndQueue
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["BajajTMSResultMasterEntities"].ConnectionString;

            Console.WriteLine("Connection String: " + connectionString);
        }
    }
}

Example Code for .NET Core or .NET 5+

If you are working with .NET Core or .NET 5+, use the `Microsoft.Extensions.Configuration` package. First, install the package:

- In the Package Manager Console, run:

  Install-Package Microsoft.Extensions.Configuration
  Install-Package Microsoft.Extensions.Configuration.Json


- Then, you can use it as follows:

using Microsoft.Extensions.Configuration;
using System;
using System.IO;

namespace SchedulerToClearDataAndQueue
{
    class Program
    {
        static void Main(string[] args)
        {
            var configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();

            string connectionString = configuration.GetConnectionString("BajajTMSResultMasterEntities");

            Console.WriteLine("Connection String: " + connectionString);
        }
    }
}


Example `appsettings.json` File

For .NET Core or .NET 5+ projects, ensure your `appsettings.json` file is structured correctly:

{
  "ConnectionStrings": {
    "BajajTMSResultMasterEntities": "data source=DC-SOF-001\\SQLSERVER2014;initial catalog=TRAFFIC_MANAGEMENT_CDMS_RESULT_MASTER_BAJAJ;user id=sa;password=dmc@123;MultipleActiveResultSets=True;App=EntityFramework"
  }
}

Additional Steps

- Ensure Proper Installation: For .NET Framework, verify the `System.Configuration` assembly is installed.
- Correct Project Target: Confirm that your project targets a compatible .NET version.

By following these steps, you should resolve the "The name 'ConfigurationManager' does not exist in the current context" error and successfully access your application settings. If you like comment and share. 🚀

Post a Comment

0 Comments

Ad Code

Responsive Advertisement