Tag Archives: c#

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

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

A SQL helper class that allows executing a query or running a stored procedure, with or without dynamic parameters

A while back I wrote about my extension method to copy a non-generic collection to an array. A commenter pointed out that it isn’t really necessary. Oh well… here is some code that uses it… (If you use this code, … Continue reading

Posted in Database, SQL | Tagged , | 1 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

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

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

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 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

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

Fun with c# code – A disabled ProgressBar

I just noticed this post somehow made the top posts list on my recovery blog, so here is an updated version. (Links updated and code repasted; otherwise it is as is from the original.) Incidentally, the code works just fine … Continue reading

Posted in Programming | Tagged , | Leave a comment

Fascinating stuff – found a copy of Stephen Toub’s Managed ThreadPool that used to be hosted on the now defunct GotDotNet.com

I’m still waiting for our sprint planning meeting to start… it’s only at 2PM… so I have spent the whole day reading interesting articles; mostly programming-related. I’ve been feeling more than a little disappointed that the push on all the … Continue reading

Posted in Programming | Tagged , | Leave a comment

Convert a FileStream opened in synchronous mode to one in asynchronous mode, in C#

Raymond Chen published an interesting blog post today explaining how to convert a synchronous file handle to an asynchronous one, via the ReOpenFile Windows API function. But that function is not implemented in the .Net BCL. Thus I wrote a … Continue reading

Posted in Programming | Tagged , , | Leave a comment

How to start an external process that is associated with a file type, and bring it’s window to the front, in C#

I found a number of answers regarding how to do this online, but none worked exactly the way I wanted. This is because I actually have one extra requirement. That is, if the file type does not have an associated … Continue reading

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

Confused by my own design – behaviour through inheritance – How not to achieve it

I wonder, do we all get to that point where our own code, written days or weeks or months or maybe even years back, makes us ask the question, “How the fuck did I do that?” Everything here refers to … Continue reading

Posted in Programming | Tagged , , | Leave a comment

Playing with Glass windows forms in C#

I’ve mentioned before that I’m a little behind the current technology, in that I am still running Windows 7. Since Aero Glass fell by the wayside in favour of what looks like a mobile user interface, this may not be … Continue reading

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

Playing with Property Grids in C# – Part 2

In Part 1 we saw how easy it is to use a PropertyGrid control for a user interface with user-configurable options. Here we expand on that a little. Customizing the appearance of an enum I mentioned that we will see … Continue reading

Posted in Programming | Tagged , , | Leave a comment

Playing with Property Grids in C# – Part 1

The PropertyGrid Control is a really handy control, well suited for any UI where the user can configure options, since it can display them in a categorized grid, with a description at the bottom for its selected property. The only … Continue reading

Posted in Programming | Tagged , , | Leave a comment

Customize the Windows 7 logon background with C#

Due to the frustration of not having shared or even written any code for a while, I went home last night and coded this little application. It’s trivial, but kind of fun. Update: And tonight, 5th June 2013, I added … Continue reading

Posted in Programming | Tagged , | Leave a comment

How to load an embedded Cursor from resources in C# (Windows Forms)

How to load an embedded cursor from resources – one that has 32-bit colour, is a trick that has eluded me for a long time, until now. Wait. What’s that, you say? The managed Cursor type does not support animated … Continue reading

Posted in Programming | Tagged , | Leave a comment

Still playing with animated Gif images in C#

On this, the last day of my present job (with nothing to do at all), and looking forward to starting the new one, where I may have very little time for blog-writing, to alleviate the boredom I’ve added a bit … Continue reading

Posted in Programming | Tagged , , | Leave a comment

Saving an animated Gif Image with c# in Windows Forms

As mentioned yesterday, you can use the GifEncoder from the Bumpkit class library to save animated Gif image files. In fact, the encoder has no dependencies on the rest of the class library, so I just copied it’s code-file into … Continue reading

Posted in Programming | Tagged , , | Leave a comment

Someone else has already implemented an animated Gif encoder

Sadly, it only shows up on page three of the Google search results for a managed animated Gif image encoder, but it is there! Check out Bumpkit. Now I no longer feel compelled to implement it… Thank goodness. Seriously, the … Continue reading

Posted in Programming | Tagged , , | Leave a comment

Bad code, baaaaad – it hurts my eyes!

There’s a downside to being obsessive, namely, you tend to obsess… Today’s obsession relates to an animated Gif encoder for Windows Forms. Since my PanBox control (PictureBox replacement) used in the application that I have based most of my articles … Continue reading

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

Numeric sort file system names in C#, like Windows Explorer

I was trying to figure out how to do this last night, getting nowhere, and can’t afford to go online the rest of this month, having spent most of my money already… Update: This code has a couple of issues. … Continue reading

Posted in Programming | Tagged , , | 1 Comment

Thorough error logging is important

I’ve been thinking about writing my next post on my ErrorNotifier class. Actually I’m too lazy to do so now, but in the meanwhile, here is an HTML email that it sent me… (from a Windows service application) I wasn’t … Continue reading

Posted in Programming | Tagged | Leave a comment

How to download a file with HttpClient in c#

Microsoft recently released a prelease of HttpClient in a portable library via Nuget. But it seems that many developers do not know how to use it. Again, the source, for this and a whole bunch of other stuff, is here: … Continue reading

Posted in Programming | Tagged , , , | 2 Comments

Handling standard media keys in your C# media player

Today’s post is probably not news to anyone… But I am sitting at work, doing nothing – bored out of my mind… I (and everyone else in this company) is being retrenched, and today, I am between job interviews. Again, … Continue reading

Posted in Programming | Tagged , , | Leave a comment

Having an async Task yield to the Windows Forms SynchronizationContext

There are issues with calling Task.Yield in Windows Forms, as explained by Stephen Toub on this forum post. Interestingly, I have the opposite problem to the guy complaining there. I have lots of async code, much of which frequently switches … Continue reading

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

Showing a form or dialog at a random location

As I wait in dreadful silence to find out whether or not this company will stay in business (and hence whether all of us will be retrenched), I find myself blogging obsessively… the value of these posts is rather dubious, … Continue reading

Posted in Programming | Tagged , | Leave a comment

Updating the Windows Forms UI from async threads

There are many ways to update your UI from async ThreadPool threads. This is just one of them; my preferred way of doing so. These are my general purpose methods, in my base form class, for updating the UI from … Continue reading

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

Creating an asynchronous timer

Here’s some interesting code I thought I might share. It’s an asynchronous timer, and allows you, in Windows Forms, to have a simple timer where you specify an interval, a condition, some code to execute on the interval, and optionally … Continue reading

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

Async all the way – No way!

I was just reading an article by Stephen Cleary on best practices in async code. I haven’t fully composed my thoughts on this topic, but this has been on my mind for a while, so I will publish it anyway, … Continue reading

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

Using async code (with await) in .Net 4.0

Recently I needed to add a UI for marketing folks here to be able to edit some pages that are presented to players on logging in to the online casino. The pages show featured promotions, games and so on. So … Continue reading

Posted in Programming | Tagged , , , | 2 Comments

Building your c# Solution with MSBuild, the easy way

Everything in this post should be obvious to any c# developer, but sadly for whatever reason, people just don’t seem to know these things. The key to improving your knowledge, and to prevent asking stupid questions on sites like StackOverflow, … Continue reading

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

Using RegionErate in Visual Studio 2012

Being a creature of habit, I have been using a Visual Studio addin called RegionErate to organize my code for several years now. It works very well up to Visual Studio 2010, which doesn’t help me since I have been … Continue reading

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

Some Extension Methods

Extension methods are, in my opinion, one of the best additions to the C# language. I’d been out of track with the various language improvements of the last few years, due to various reasons. (Playing with the language and trying … Continue reading

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

Icon Files in c# – Part 2. Writing icon files

Welcome to part 2 of my c# icon file handling code. This one is about my IconFileWriter. Again, the source, for this and a whole bunch of other stuff, is here: RomyView.zip To be honest, I didn’t spend much time … Continue reading

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

Icon Files in c# – Part 1. Reading icon files

I’ve written my own icon file reader and icon writer in c# (Windows Forms – it reads the icons into System.Drawing.Icon instances), and since the samples I found on it were sparse and mostly buggy, I’m sharing mine. Again, the … Continue reading

Posted in Programming | Tagged , , | Leave a comment

Displaying progress in the Windows 7 Taskbar

I admit, this is a bit out of date. I’m not using Windows 8 yet. But if anybody still needs to know how to display progress in the Windows 7 Taskbar, read on to find out how easy it is. … Continue reading

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

How to suspend and resume processes in C#

There is no native API, or .Net Framework method, to suspend or resume a process, but it can be done by suspending or resuming the process’ threads. I have written a post about this on my other blog, but this … Continue reading

Posted in Programming | Tagged , , | Leave a comment

C# Application to import paradox data to SQL Server

As promised… When recently tasked with rewriting an antiquated hotel reservation booking system, that uses paradox data, into C#, I was disappointed to find no free tools online that can import the paradox tables into SQL Server. Thus to save … Continue reading

Posted in Programming | Tagged , , , , | 12 Comments