Category Archives: Programming

How to initialize an enum from a string or int value in c#

I didn’t know you could do this, but I tried and it seems fine so it’s worth sharing. I’m working in a large code base, where my input might come from a web form, or an imported file, or even … Continue reading

Posted in Programming | Tagged , | Leave a comment

New methods not showing up in WCF Test Client? Are you using IIS Express and did you move your code to a new directory without deleting the old one?

I gave the answer to my problem of today in the title… I work on multiple projects. But we also recently changed our source control provider from SVN to Git. If I haven’t touched a code-base for months, I don’t … Continue reading

Posted in Programming | Tagged | Leave a comment

Sample code to send email via gmail in c# (updated)

I wrote a post recently with some code to send email via gmail in c#. The other day, I needed to update it to allow for multiple attachments. Rather than update the original post, I’ve written this new one… (The … Continue reading

Posted in Programming | Tagged , | Leave a comment

Sample code to send email via gmail in c#

For some time, I’ve had a block of code that I copied and pasted all over the place, to send emails via gmail. It didn’t add an attachment, but I needed to do so today. So rather than copy and … Continue reading

Posted in Programming | Tagged , | Leave a comment

Beware of calling Contains in the Where predicate on IQueryable<T> in LINQ to SQL if your collection has more than 2100 items

Recently I ran into a problem I’d never seen before – the 2100 parameter limit in the SQL that’s generated when you call Contains in the IQueryable<T>.Where predicate in LINQ to SQL. To make matters worse, this happened when filtering … Continue reading

Posted in Programming | Tagged , | Leave a comment

How to load a cursor from embedded resources in C#

Last night, being the last night my son was staying with his cousins, I was bored and spent some time looking at some old code. I don’t know if anyone else still uses Windows Forms applications, but in case you … Continue reading

Posted in Programming | Tagged , , | Leave a comment

A pet hate and anti-pattern: A method returning a bool that can never return false, but can throw an Exception.

Recently I came across some code that, while syntactically correct, does something that always gets to me. Let’s illustrate it with an example: using System; namespace Example { public class AntiPatterns { public bool NeverReturnsFalse() { try { // Do … Continue reading

Posted in Programming | Tagged | Leave a comment

Beware of WCF POST service methods that receive JSON that contain DateTime values

I ran into a weird problem today… I have a third party that returns some data to our endpoint in JSON format. I created my endpoint, posted JSON to it, and all seemed well. But when the third party posted … Continue reading

Posted in Programming | Tagged , , , | Leave a comment

How to convert a string to ASCII in c#

This is one of those cases where smart-ass developers will tell you that you should not be converting strings to ASCII, because reasons… However, if you’re sending a file to a host-to-host banking system, or sending text to an external … Continue reading

Posted in General, Programming | Tagged | Leave a comment

When cleanup fails and tells me that I need to run cleanup

Huh? Normally I wouldn’t write a post like this because I don’t have a solution. But this one is just too funny in its own stupid way. Yesterday I needed to commit a simple change to a project, and we … Continue reading

Posted in Programming, Work | Tagged | Leave a comment

Convert a non-generic collection to an array – my most useful extension method ever…

Funny how things work out… I wrote one of my most simple extension methods years ago, one that converts a non-generic collection to an array (because arrays implement IEnumerable<T> on which I can call Linq extension methods). Lately I find … Continue reading

Posted in Programming | Tagged , , | 2 Comments

How to get the row index in the client side code of a DataItemTemplate in an ASPxGridView column

This was pissing me off for far too long today. I also have a DataItemTemplate with an ASPxHyperlink control in one of my columns of an ASPxGridView. When the user clicks the hyperlink, I need to call a JavaScript function … Continue reading

Posted in Programming | Tagged , , , | 2 Comments

How to add to the Windows recent documents in C#

I realize that I haven’t written anything here for ages… so I looked through my old code for something simple but useful, and found this. If ever you need to add a file to the Windows recent document list, this … Continue reading

Posted in Programming | Tagged , | Leave a comment

How to ensure that a number is always round up to the nearest integer

Yes, this is silly. I never used Math.Ceiling before; so I never even knew what it was for. In my use case, I’m sending SMS messages via a third party WCF service. It works well, but in my own WCF … Continue reading

Posted in Programming | Tagged , | Leave a comment

How to convert Unicode text to plain ASCII

Let’s say you have a string that was input by somebody out of your control… Maybe it got to you via a call to your web service from a remote client, indirectly via somebody else’s software. And you need to … Continue reading

Posted in Programming | Tagged , | Leave a comment

How to encode or decode a base64 string

This is an absolutely trivial programming task, but one that annoyed me enough to have to write some code to do… I use Filezilla for FTP uploads/downloads at work, but never remember my passwords for FTP sites. Nor do I … Continue reading

Posted in Programming | Tagged | Leave a comment

The curious case of the generic complimentary comment. I hate SPAM!

I published this on my other blog yesterday. But it is related to programming in a way, so now it’s published here too. Recently I received this comment to one of my posts: Hello! I’ve been reading your site for … Continue reading

Posted in Programming | Tagged | Leave a comment

Experimental new C++ operator joke

This is fucking hilarious… Raymond Chen introduced some “new C++ tadpole operators” on his blog. It’s a joke, one that I didn’t get immediately, but what makes it even funnier is that many commenters also didn’t get it. They’re upset … Continue reading

Posted in Programming | Tagged , | Leave a comment

Do NOT cache SSL certificates on the client side – How to post XML asynchronously and read the response using HttpClient in c#

I had one hell of a day yesterday. The evening before, I’d renewed the certificate on a WCF service of ours, because the old one was about to expire. After renewing it, I tested the service to confirm that I … Continue reading

Posted in Programming | Tagged , , , , | Leave a comment

Link to: Emulating double dispatch with c#

“Link to” in the title indicates that I am writing about something that somebody else wrote, somebody a lot smarter than myself. Eric Lippert wrote a great post on emulating double dispatch in c#. Actually this is part 3 of … Continue reading

Posted in Programming | Tagged | Leave a comment

Failed to establish a backside connection

We get this error occasionally when using a bank’s web service for AVS-R. (Real-time bank account validation.) Maybe I’m weird but I laugh every time I see that error. I read it as “Cannot find anus.”

Posted in Programming, Work | Tagged | Leave a comment

Zipping a directory and including relative directory paths is non-intuitive with DotNetZip

I recently had to write some code to auto-update an application via an FTP server. It uses home-grown code since ClickOnce is too limiting, and is written in a Windows Forms .NET 3.5 Visual Studio 2010 project. It also had … Continue reading

Posted in Programming | Tagged , | Leave a comment

How to implement IDisposable properly

Every so often I write a class with disposable members, so it needs to implement IDisposable. I always copy and paste from one of my other classes, so I figured it would be useful to share the pattern of IDisposable … Continue reading

Posted in Programming | Tagged , | Leave a comment

SA Mobile Drug Testing… Located somewhere in the tropical Atlantic ocean off the Gulf of Guinea?

This is what happens when you let clueless developers build a website… I was searching for local drug testing options when I found this… Surprising that their top two results for drug testing in Johannesburg are in Cape Town and … Continue reading

Posted in Programming | Tagged | Leave a comment

So funny: I never thought of Bobby Tables this way

I just found this in a comment to Raymond Chen’s latest blog post. I hadn’t seen it before and it’s too funny not to share.

Posted in Programming | Tagged | Leave a comment

A pattern for implementing the == operator in C#

I’ve been neglecting this blog for a while because I have very little time to write here… Every now and then I need to implement the == operator on one of my classes; typically because I want to compare two … Continue reading

Posted in Programming | Tagged , , | Leave a comment

A nice way of handling timesheets

I mentioned a while back that I have started using Toggl for my time sheets… What I did this month, which was the first month I sent the Toggl-based report to my boss, was export a detailed report, then remove … Continue reading

Posted in Programming, Work | Tagged , , | Leave a comment

An asynchronous implementation of File.WriteAllLines

This is an example that (optionally) uses an extension method I wrote about back in April 2013. See the original post here. Last time around the post was about the caveats involved when working with streams. This time, I’m going … Continue reading

Posted in Programming | Tagged | Leave a comment

Feeling like an imposter again

Sheesh… I read a few programming blogs, but one of them always ends up making me feel really stupid. Eric Lippert, I hate you! … OK, maybe I don’t, but the guy writes the most beautiful, concise code I have … Continue reading

Posted in General, Programming | Tagged | Leave a comment

Thank you, Word auto recovery. Gosh, which one shall I use?

Perplexing… My notebook crashed on Friday, and I just returned to this Word document now. Word conveniently saved two versions of the document for me, including the original draft, apparently written some 370 years before I was born, and 51 … Continue reading

Posted in General, Programming | Tagged , | Leave a comment

Memory gates checking failed because the free memory (n bytes) is less than 5% of total memory.

Ever had this horrendous error? WebHost failed to process a request. Sender Information: System.ServiceModel.ServiceHostingEnvironment+HostingManager/45653674 Exception: System.ServiceModel.ServiceActivationException: The service ‘/YourServiceName.svc’ cannot be activated due to an exception during compilation. The exception message is: Memory gates checking failed because the free memory … Continue reading

Posted in Programming, Work | Tagged , , | 2 Comments

How to force (hack?) kendo-ui grid to display the correct date

We have some grids displaying the data from SQL queries, including dates. But for whatever reason, the time zone is being applied to the date twice. For example, the date is today, at 12PM, and we are using GMT-2, here … Continue reading

Posted in Programming | Tagged , | 5 Comments

Bootstrap’s modal jQuery dialog doesn’t have to be dismissed when the user clicks elsewhere

We have a Node server running, developed using the express framework and a few other components, all new to me. I’m no Node expert… actually I hate it, and estimate it takes me more than five times longer to develop … Continue reading

Posted in Programming | Tagged , | Leave a comment

A simple c# ASP.Net Pin-pad user control

This is just a work-in-progress, but it does the job, and should be enough to get you started on adding a PIN option to your web site. It’s inspired by this entry on StackOverflow. The concept is: Besides your normal … Continue reading

Posted in Programming | Tagged | Leave a comment

How to call HttpClient.PostAsync with a query string

This has to be one of the most non-discoverable APIs ever, and annoyed the hell out of me earler today. Hence my posting it here so that others may not have to struggle over something so trivial. All I want … Continue reading

Posted in Programming | Tagged | 8 Comments

Dumb searches: EnumWindowProc eats CPU

I normally don’t do these types of post on this blog. I assume the searcher arrived here because of this post where I called that API function years ago… Well well… You are enumerating all the Windows on the machine … Continue reading

Posted in Programming | Tagged | Leave a comment

1366×768 is not a resolution you should be using (or targeting), and some ASPxDashboadViewer hackery

Just a rant today… My screen resolution on my work laptop is 1920×1080, but my boss uses 1366×768. That’s a crap resolution. Since he demos the web applications we work on, on his machine, that frequently means that we have … Continue reading

Posted in General, Programming, Work | Tagged , | Leave a comment

Quack quack: A somewhat malicious program I wrote many years ago

I was reading The Daily WTF earlier, and came across this image, which reminded me of something… I think it’s an amusing story, at least it is to me, so I tracked down a program I wrote many years ago, … Continue reading

Posted in Programming | Tagged , , , | 1 Comment

How to align ASPxMenu items on both left and right-hand side

Download the code: Right-aligned menu.zip As mentioned recently, we are making extensive use of DevExpress controls in my new job. Today I needed to align menu items on the DevExpress menu to both sides of the screen. In case you … Continue reading

Posted in Programming | Tagged | Leave a comment

An easy way to print a web modal popup window

In my new job, we are using DevExpress controls quite extensively, and that involves using an ASPxPopupControl. I need to print the contents of a modal popup, and I naively thought… How difficult can it be? It turns out to … Continue reading

Posted in Programming | Tagged , , , , | 8 Comments

Just to pass time… One way to approach a simple C# programming assessment

I posted this on my other blog earlier, so excuse the first couple of paragraphs that have nothing to do with this blog… Why must it be so heart-breaking to drop a child at crèche? She’s happy and chatters all … Continue reading

Posted in Family, Programming | Tagged , | 3 Comments

How to exclude files from a FOR loop in a batch file

The other day, my colleague ran into an interesting problem… he needed to process a bunch of files in a directory using a batch file, and one by one, archive them and send them somewhere by FTP. The files were … Continue reading

Posted in Programming, Work | Tagged | Leave a comment

A quick and easy way to call a Web Service without the generated configuration

I recently had to call a web service without being able to use the section it adds to the application configuration file. The reason for this is that my project is a class library running in the process of a … Continue reading

Posted in Programming | Tagged , | Leave a comment

On unwanted software

Yesterday Scott Hanselman wrote an excellent blog-post on the subject of unwanted software. It’s a pity in a way that his is a development blog, because the people who need to read it are not the ones who will get … Continue reading

Posted in Programming | Tagged | Leave a comment

How to write an empty XML element with XmlWriter in c#

This is probably trivial to most of us, but sometimes it’s the simple stuff that is so easy to miss. I have a class that converts an incoming flat file to XML and passes it to a payment system. If … Continue reading

Posted in Programming | Tagged | Leave a comment

My favourite article by Raymond Chen: Feature-coloured glasses

(You’ll just have to forgive me for my spelling in the title… I speak English, not American.) The other day, I noticed with great sadness, on Raymond Chen’s blog, that Technet Magazine has been retired, and thus so has his … Continue reading

Posted in Programming | Tagged | Leave a comment

The dark side of SCRUM: Two more things not to say to testers

I had a new year’s resolution to be nice to people, but sadly it didn’t last quite as long as I wanted. Some context: I’ve been working on a debit order system, where files with the banking details and so … Continue reading

Posted in Programming, Work | Tagged | Leave a comment

How to get the application’s directory in C#

Just a short one today… I always used to forget how to get the path of the directory that my code is running in, and although I did remember it eventually, I also recall that along the way, I found … Continue reading

Posted in Programming | Tagged , | Leave a comment

On the importance of logging exceptions

It’s been a while since I wrote a programming blog-post, so here goes… We developers like to think that our code is perfect, that there are no bugs. But there are always bugs. And even in cases where our code … Continue reading

Posted in Programming | Tagged , | Leave a comment

Idiot programmers are more common than you think

It’s always amused me that there are so many stupid programmers here in Johannesburg since they made job interviews really easy for me. But I never realized that that, unlike the stupid programmers here who can’t even write code, there … Continue reading

Posted in Programming | Tagged , | Leave a comment