The referenced component could not be found

Problem When I open a C# project in Visual Studio, none of the references are loading. In the error list it says “The referenced component could not be found” for several references. Here’s a snippet showing just a few of the reference errors: The referenced component ‘System’ could not be found.The referenced component ‘Microsoft.CSharp’ could … Read more

Create a queue in Amazon SQS

In this article we’re going to create a queue in Amazon Simple Queue Service (SQS) using an AWS Stack template. Amazon SQS and Spring Series This article is part of a series: What is a message queue Message queues allow different parts of a system to communicate and process operations asynchronously. A producer sends a … Read more

HackerRank – Sock Merchant solution

In this article, I’ll explain how to solve the Sock Merchant algorithm problem on HackerRank. Problem statement: You’re given an integer array and need to count the number of pairs of matching integers. The integers will be between 1 and 100. For example, [3,1,3] has one matching pair (the 3s match). Approach First, we know … Read more

Auto-increment build numbers in Visual Studio

You need to auto-increment your build numbers in order to easily tell which code you’re working with. In this article I’ll explain how to auto-increment your build numbers in Visual Studio. I’ll be using text templating to generate the Assembly Version. 1 – Choose a versioning scheme I’m going to be using the version scheme: … Read more

How to mock static methods

The need to mock static methods in order to add a unit test is a very common problem. It’s often the case that these static methods are in third-party libraries. There are many utility libraries that are completely made up of static methods. While this makes them very easy to use, it makes them really … Read more

C# – Example of using BlockingCollection

The BlockingCollection class is a blocking concurrent queue. It provides an implementation of the producer-consumer pattern. There are two parts to this pattern: BlockingCollection is thread-safe, which means it’s designed to be used by many threads at once. Here’s an example of using BlockingCollection with one consumer and two producers: Note: BlockingCollection.IsCompleted means the queue … Read more

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

Problem When you’re executing a CREATE/ALTER statement to create a procedure/view/function/trigger, you get one of the following errors: ‘CREATE/ALTER PROCEDURE’ must be the first statement in a query batch ‘CREATE VIEW’ must be the first statement in a query batch. ‘CREATE FUNCTION’ must be the first statement in a query batch. ‘CREATE TRIGGER’ must be … Read more

Error: 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 Solution The simplest solution is to install the Selenium.Chrome.WebDriver package: … Read more

C# – Example of using events

Events are an implementation of the observer pattern and consists of two parts: There are three simple steps to using events, which I’ll show below. 1 – Add an event field The first step is to add an event field to a class with the event keyword and a delegate type (such as the generic … Read more

Multithreaded quicksort in C#

One day I decided to challenge myself by trying to implement multithreaded quicksort. I wanted to see how it would compare to the built-in Array.Sort() method. I came up with two algorithms that were 2-4x faster than Array.Sort(): After continuing to tinker, in attempts to further optimize, I came across the AsParallel().OrderBy() method (PLINQ). After … Read more

How to set a timeout for TcpClient.ConnectAsync()

TcpClient has no direct way to set the connection timeout. It doesn’t have any parameters that allow you to control it, and SendTimeout / ReceiveTimeout don’t apply to the initial connection. The way I control the connection timeout is by awaiting a Task.WhenAny() with TcpClient.ConnectAsync() and Task.Delay(). Task.WhenAny() returns when any of the tasks complete. … Read more

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

SQL – Sort into groups, then sort within groups

I recently came across a complex sorting problem that required sorting the data into groups, then further sorting the data within each group. I’ll explain this problem with a concrete example, and then I’ll show the SQL query I used to solve the problem. Let’s say we have orders, departments, and users. Users can be … Read more

C# – Using custom attributes

Attributes are used to store additional info about a class/method/property. The attributes are read at runtime and used to change the program’s behavior. Here are a few examples of commonly used built-in attributes: In general, you should try to use built-in attributes when possible. When it makes sense, you can create your own custom attribute. … Read more

Could not find an implementation of the query pattern for source type

Problem Any time you try to use a Linq extension method (such as Select() to transform list elements) and you haven’t added ‘using System.Linq’, you’ll get a compiler error like this: Could not find an implementation of the query pattern for source type ‘Your Type’.  ‘Select’ not found. In newer versions, the error looks like … Read more

Objects added to a BindingSource’s list must all be of the same type

Problem When you’re binding a control to a list of objects by adding a BindingList to a BindingSource, you get the following exception: System.InvalidOperationException: Objects added to a BindingSource’s list must all be of the same type. Here’s the code causing this: Solution Instead of adding the BindingList to the BindingSource with Add(), set BindingSource.DataSource … Read more

ReportViewer doesn’t appear in the toolbox in Visual Studio

Problem You’re trying to use the ReportViewer control in Visual Studio but it’s not in the toolbox. Other symptoms: Solution Note: Tested in Visual Studio 2017 and Visual Studio 2019. The exact instructions may have slightly different steps depending on which version of VS you’re using. There are two different problems to solve. First, you … Read more