Blog Archives

Thinking Outside The Box


I could really write a few things with that title. As developers we often take the path of least resistance or of most familiarity. Usually this is good because we are using best practices or some other coding standard. Sometimes, these well worn trails are not always the best.

Read the rest of this entry

My Json Date Lost Its Offset

What Happened

My golf buddies lost in a sand trap

I was using NewtonSoft like so many of us for Json Serialization and Deserialization. In this case I was trying to make sure our DateTimes were being returned in a proper ISO8601 format, defaulting to UTC unless a flag was set. If the flag was set it would then use the offset associated with a customer’s location, never to use the browser’s offset.

Sadly it was always rendering in Postman and SoapUI in my local time zone. Even when I would debug through the code, the return Ok(jsonObject); had the expected time zone and format.

Read the rest of this entry

The Expando Object

Just some mushrooms

The Expando object has been around for a while. In fact it was in .NET 4.0, in the ancient days of 2009.

I am sure, that back then, I must have thought that it was neat. Maybe a bit like JavaScript, but I never had a use for it until recently.

Read the rest of this entry

Linq to CSV

Linq to CSV

Something Old

Something old

This post is about something old, something I should have posted about a long time ago.  A few years ago I had to use a CSV file as a data source for some process.  I had forgotten all about it until recently when I was looking through some old projects, they definitely brought back some memories.

I do remember thinking there has to be a better way to access a CSV file more like a database and have it populate an object.  I started to get my frame of mind all set to come up with a solution when I thought how dumb it is to re-invent the wheel.  So I did a search first, and found the LinqToCsv package.

Read the rest of this entry

Change Login Using Windows Authentication

Recently I had a need to allow users to change their login on an MVC site that used Windows authentication.  Of course we did not want the user to log out of their machine to do it.  The trick of course involved sending a 401 response, but how to do that and not get stuck in an endless loop.

The 401 Loop

It seemed simple enough, in fact too simple.  You just return a 401 challenge and have them move on to their previous page.  The 401 response is what presents the user with the Windows login popup.  In reality the 401 response acts like a redirect on itself, so you get two page loads and it forgets any variables you set.

Well that simply would not do. Read the rest of this entry

Using LINQ to Sort by Day of Week And Time

So a couple weeks ago I saw a question on the ASP.Net or MSDN forums.  Someone wanted to know how to use LINQ to sort by the day of the week and then by the time for a given datetime.  I did not have a chance to do anything with it, but for some reason it stuck in my head.  So, I came up with the answer, but of course I do not remember where the post was located.  The answer is very simple.

Read the rest of this entry

Viewing Your Proxy Class’ SOAP

Most of us are a bit spoiled with .NET and its lovely ability to take a WSDL (via the WSDL Tool) and create us a proxy class.  We can then use this object to work with the web service.  What if you need to actually see that XML?

Well, Microsoft has a solution.  You can use an extension method to log your SOAP.  Not sure how to do it, don’t worry about that either, Microsoft has already written it for you.

Please go to this link to read the full article.

Custom Validation Controls

Custom validation controls are a nice tool that you can use when the standard validation controls just won’t do the job.  Most examples I found out there only addressed the server side validation aspect.  Don’t get me wrong, you can do that, just use server side, but ideally you want to do both.

Trust me though, if you only want to do one side, do server side.  You will want to always do server side as a minimum just in case the user has javascript disabled or something.  Server side only is kind of old school but it gets the job done very reliably.

Read the rest of this entry

The Conditional Operator (?:)

I don’t use this thing too much, so I often forget it when I need it.  In a nut shell it is a nice one line if statement.  You can view the MS page here.

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.
The condition must evaluate to true or false. If condition is true, first_expression is evaluated and becomes the result. If condition is false, second_expression is evaluated and becomes the result. Only one of the two expressions is evaluated.

condition ? first_expression : second_expression;

Let me provide a realistic example.  Lets say you have a method to generate a random number from 1 – 4, and you want to see if the number is odd or even.

private int RandomNumber(int min, int max)
    Random random = new random();
    return random.Next(min, max);

private string NumberIs()
    string sNumberIs = (RandomNumber(1, 4) % 2 == 0) ? "Even" : "Odd";
    return sNumberIs;

Web User Control Events

Ever want to have an event for a Web User Control that the parent page can consume?  Well you can, and it is fairly simple.

I am not an expert since I just figured this out myself so here we go.

There are three basic things you need to do for each event.

  1. Create a public delegate variable in your User Control
  2. Create a public event event for the type of delegate
  3. Create a protected virtual method

So lets see some code.

//Public variables and methods
public delegate void ItemClickEvent(object sender, EventArgs e);
public event ItemClickEvent ItemClick;

//Protected Methods
protected virtual void OnItemClicked(object sender, EventArgs e)
    if (ItemClick != null)
        ItemClick(this, e);

The property in the page with this user control will be called OnItemClick.  Notice I do not have anything named OnItemClick.  The only public names are ItemClickEvent, which is a delegate and the event itself, ItemClick.  So I assume that .NET adds the On to the front of your event name.