System.InvalidOperationException: Sequence contains no elements

Problem When you call .First() on an empty IEnumerable, you get the following exception: System.InvalidOperationException: Sequence contains no elements Solution Option 1 – Use .FirstOrDefault() instead of .First() When the IEnumerable is empty, .FirstOrDefault() returns the default value for the type. For reference types this returns null. For value types this returns 0 or that … Read more System.InvalidOperationException: Sequence contains no elements

WinForms: How to check if another form is open

If you’re working on a Windows Forms project and need to know which forms are open, use: This gives you an IEnumerable collection of form objects that are currently open. You can lookup a form by name, by type, or loop through the list. Example scenarios There are many scenarios where you’d want to know … Read more WinForms: How to check if another form is open

KeyNotFoundException: The given key was not present in the dictionary

Problem The following exception is thrown when you try to get a value from a dictionary using a key that doesn’t exist in the dictionary: KeyNotFoundException: ‘The given key was not present in the dictionary.’ Consider the following the example: When the user enters a city name that isn’t in the dictionary, such as “New … Read more KeyNotFoundException: The given key was not present in the dictionary

SqlTypeException: SqlDateTime overflow

Problem I am executing a SQL query and trying to set a datetime column to DateTime.MinValue. I’m getting the following exception: System.Data.SqlTypes.SqlTypeException: ‘SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.’ The problem is that DateTime.MinValue is 1/1/0001 12:00:00 AM and the SQL Server DateTime minimum value is 1/1/1753 12:00 AM. Solution … Read more SqlTypeException: SqlDateTime overflow

Visual Studio 2019: MSTest unit tests are not running in Test Explorer

Problem I have a unit test project in Visual Studio 2019. When I click Run All Tests, none of the tests run. I looked in the Tests output window and saw a message that says: Test project does not reference any .NET NuGet Adapter. Test discovery or execution might not work for this project. It’s … Read more Visual Studio 2019: MSTest unit tests are not running in Test Explorer

‘CREATE/ALTER PROCEDURE’ must be the first statement in a query batch

Problem You have a SQL query like this: While executing this you get the following error: ‘CREATE/ALTER PROCEDURE’ must be the first statement in a query batch Solution The error message says it all: the CREATE PROCEDURE statement cannot be executed after other queries in the same batch of queries. The solution is to execute … Read more ‘CREATE/ALTER PROCEDURE’ must be the first statement in a query batch

OpenQA.Selenium.DriverServiceNotFoundException: The chromedriver.exe file does not exist in the current directory

Problem I’m trying to use Selenium with the ChromeDriver and I’m running into the following exception: Unhandled Exception: OpenQA.Selenium.DriverServiceNotFoundException: The chromedriver.exe file does not exist in the current directory or in a directory on the PATH environment variable. The driver can be downloaded at http://chromedriver.storage.googleapis.com/index.html Solution The simplest solution is to install the Selenium.Chrome.WebDriver nuget … Read more OpenQA.Selenium.DriverServiceNotFoundException: The chromedriver.exe file does not exist in the current directory

OpenQA.Selenium.WebDriverException: Element is not clickable. Other element would receive click.

Problem I am using Selenium to automate navigating a website. I’m trying to click a button: When I try to click the button it’s throwing the following exception: OpenQA.Selenium.WebDriverException: Element is not clickable. Other element would receive click. The reason this is happening is because Selenium is trying to click the button by its coordinates, … Read more OpenQA.Selenium.WebDriverException: Element is not clickable. Other element would receive click.

Run a Windows Service as a console app

When working on a Windows Service project you may want to run it as a Console App so you can see what’s going on. If you try to run the service in Visual Studio you’ll get an error that says: “Cannot start service from the command line or a debugger. A Windows Service must first … Read more Run a Windows Service as a console app

How to update UI from another thread

I often need to be able to run multiple threads and update the UI based on the results. For example, I may need to execute GET requests to 10 different endpoints concurrently, and then report their results in a datagrid as they come back. The problem is you can’t just update the UI from any … Read more How to update UI from another thread