Ad Code

Responsive Advertisement

Console App Task 19: How to run a console application and check the output of Console.WriteLine("Connection String: " + connectionString); in C# ?

Console App Task 19: How to run a console application and check the output of Console.WriteLine("Connection String: " + connectionString); in C# ?  

Running a console application and checking the output of `Console.WriteLine` in a .NET application is straightforward. Below are the detailed steps to run the console application and view the output, as well as troubleshooting steps if you encounter issues.


Step-by-Step Guide to Run a Console Application

1. Create or Open a Console Application

If you haven't already created a console application, you can create one as follows:

1. Open Visual Studio.
2. Click on "Create a new project".
3. Select "Console App (.NET Framework)" or "Console App (.NET Core)" depending on your needs.
4. Give your project a name and click "Create".

2. Write the Code

Here is a simple example code that retrieves a connection string from the configuration file and prints it to the console:

using System;
using System.Configuration; // Make sure this namespace is included

namespace SchedulerToClearDataAndQueue
{
    class Program
    {
        static void Main(string[] args)
        {
            // Retrieve the connection string from app.config or web.config
            string connectionString = ConfigurationManager.ConnectionStrings["BajajTMSResultMasterEntities"].ConnectionString;

            // Print the connection string to the console
            Console.WriteLine("Connection String: " + connectionString);

            // Wait for user input to close the console window
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }
}

3. Configure the Connection String

Ensure that the connection string is properly configured in your `app.config` or `web.config` file:

<configuration>
  <connectionStrings>
    <add name="BajajTMSResultMasterEntities" connectionString="data source=DC-SOF-001\SQLSERVER2014;initial catalog=TRAFFIC_MANAGEMENT_CDMS_RESULT_MASTER_BAJAJ;user id=sa;password=dmc@123;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

4. Build the Project

To build your project in Visual Studio:

1. Go to the "Build" menu.
2. Select "Build Solution" or press `Ctrl + Shift + B`.

5. Run the Console Application

You can run the application in different ways:

Method 1: Run in Debug Mode

1. Press `F5` or click on the "Start" button in the toolbar to run the application in Debug mode.
2. The console window should appear, displaying the connection string and waiting for a key press.

Method 2: Run in Release Mode

1. Change the build configuration to "Release" by selecting it from the dropdown in the toolbar.
2. Press `F5` or `Ctrl + F5` to run the application. `Ctrl + F5` will run the application without debugging, which can be faster.

Method 3: Run from the Command Line

1. Open a Command Prompt or PowerShell.
2. Navigate to the directory where your project’s executable is located, typically in the `bin/Debug` or `bin/Release` folder.
3. Run the application by typing the name of the executable file (e.g., `SchedulerToClearDataAndQueue.exe`) and pressing `Enter`.

6. Check the Output

After running the application, you should see the output in the console window, including the connection string and the prompt "Press any key to exit...". Press any key to close the console window.

Troubleshooting

If you encounter issues, consider the following:

- ConfigurationManager Reference: Ensure you have a reference to `System.Configuration` and have included `using System.Configuration;` at the top of your code file.
- Connection String Name: Double-check that the name of the connection string in your code matches exactly with the one in your configuration file.
- Application Errors: If the application throws exceptions, use `try-catch` blocks to handle errors and print exception messages for debugging.

try
{
    string connectionString = ConfigurationManager.ConnectionStrings["BajajTMSResultMasterEntities"].ConnectionString;
    Console.WriteLine("Connection String: " + connectionString);
}
catch (Exception ex)
{
    Console.WriteLine("An error occurred: " + ex.Message);
}
finally
{
    Console.WriteLine("Press any key to exit...");
    Console.ReadKey();
}

Conclusion

Following these steps will allow you to run a console application and check the output of `Console.WriteLine`. If you have any further questions or encounter specific issues, feel free to ask for additional help!

If you like comment and share. 🚀

Post a Comment

0 Comments

Ad Code

Responsive Advertisement