bool currentConfirmValue = attendee.IsConfirmed.HasValue ? (bool)attendee.IsConfirmed : false; //bool? to bool, treat null as false
Active Directory Coding – “Information about the domain could not be retrieved (1355)”
When moving to a new implementation of Active Directory – an old piece of code that had been working for ages stopped. This was around using the “System.DirectoryServices.AccountManagement” namespace specifically adding a user to an AD Group now suddenly returned this error: “”Information about the domain could not be retrieved (1355)”.
Why? Because the server doing the provisioning was not joining to the new AD domain. Since joining was not an option – I had to find a workaround for the code. Thank you for martijnh1 for this excellent post on a decent workaround, relying on the old System.DirectoryServices library.
WebAPI: Multiple actions were found that match the request
Most CRUD WebAPI’s include a POST, a GET, a PUSH and a DELETE.
Recently when trying to call a POST in my WebAPI I got this error message:
“Multiple actions were found that match the request”
Even though I clearly had only one POST, one GET, one PUSH and one DELETE. I couldn’t understand why it was telling me multiple actions were matched against my request.
Turns out, the problem was I had another public method defined in the WebAPI controller. This was actually just called by my normal POST operation – but because it was defined as public the WebAPI’s routing gets confused. Changing the method’s identifier to internal resolved the problem.
The lesson learned here is, unless it’s a POST, GET, PUSH, DELETE or some other valid public facing WebAPI action – make sure you don’t use the public identifier for any other methods you might have in the controller.
Clear out corrupt BITS Jobs – 3 things to try
I was recently faced with the problem of writing an application responsible for uploading files consisting of several Gigabytes over the internet and we decided to integrate with Microsoft’s BITS technology (Background Intelligent Transfer Service). That’s because there’s a 2GB limit in terms of what you can send over http, so you need some technology to slice up a file in smaller bits and transmit each of these bits and then re-assemble them server side. For this sort of thing, BITS is perfect. Coincidentally it’s the same technology that Microsoft uses to push updates to your machine. There is a .NET wrapper that you can use https://sharpbits.codeplex.com/ which makes integration with BITS a lot simpler.
One frustration in dealing with BITS as a developer though – is that while you’re debugging your application, you often end up with BITS Jobs in a “corrupt” state – they’re neither active nor paused nor cancelled. Getting rid of these corrupt jobs can be an irritation as not even rebooting your machine will get rid of them!
There are three things you can try.
1. Clear out via command prompt
The easiest thing is to run your standard command prompt as an administrator and run this command:
bitsadmin /reset /allusers
That should do it. If that doesn’t work, however,
2. Clear out via Powershell
try firing the next commands from Powershell:
Import-module bitstransfer
Get-bitstransfer –allusers
Get-bitstransfer –allusers | remove-bitstransfer
Most of the time, either one of the two options should do the trick. But at times, a corrupt job will be stubborn and you need to take stronger measures to remove the sucker. In which case I recommend the following:
3. Hard Delete
Microsoft stores BITS jobs here:
C:\ProgramData\Microsoft\Network\Downloader
So
1. Stop BITS service from your windows services in control panel
2. Delete all the .DAT files from the location above.
3. Start your BITS service again.
Reading from database – null casting
An on-going pesky problem when reading from a database in .NET either via a DataReader or DataSet or some other means is having to cast to a data type, and when you get back a type of DBNull you always require some code to handle it. What I mean is, if I have a variable of type Int? doing this
MyIntVariable = (int)DataReader[“MyColumn”];
will error out if “MyColumn” comes back with a NULL value. It’s always possible to write some code to handle it, but it’s nice to get it down to a simple one-liner. Luckily using a combination of the “as” keyword for our casting and the null coalescing operator (??) we can make it as simple and painless as possible in one line of code.
//handle possible null value
MyIntVariable = DataReader[“MyColumn”] as int? ?? null;
Taking a collection as a stored procedure parameter
Historically passing a collection as a stored procedure parameter has always been hard. Out of habit since the Sql Server 2005 days I typically passed my collection as a structured piece of XML, which I would then interrogate on the SQL side. This is a very painful and cumbersome approach, and when I recently encountered the problem again I wondered whether there was not perhaps a more elegant way of doing this. Turns out since Sql Server 2008, you can create a user defined type and then pass your collection as a DataTable from .NET. This is much easier and far more streamlined. Here is a great post on that very subject
Testing RESTful services and WEBApi
3 good ways:
1. Unit testing – consuming service via c#
http://www.asp.net/web-api/overview/testing-and-debugging/unit-testing-with-aspnet-web-api
2. Via the Fiddler tool
http://jasonhall.blogs.sqlsentry.net/2014/05/testing-restful-web-apis-with-fiddler.html
3. Via a Chrome Extension called “REST Console”
The configuration section cannot be used at this path – an IIS install “Gotcha”
When trying to run an ASP.net site from the localhost of my new laptop – I got the following error: “The configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault=’Deny’), or set explicitly by a location tag with overrideMode=’Deny’ or the legacy allowOverride=’false.”

I looked at my web.config as well as the web.config of the parent sites, but I couldn’t see any configuration settings pertaining to locking. This is how I fixed the error:
1. From the search box in the “Start” button of Windows, type “Windows features” and select the “Turn windows features on or off” option that comes up.
2. Go to “Internet Information Services” – Expand it, then expand “World Wide Web Services”, then expand “Application Development Features”. Make sure to check all the options and hit “OK”. (although I didn’t bother checking CGI). This will install necessary components to run ASP.Net sites locally that otherwise run into the above error.
The “Gotcha” here is that when you install IIS on a machine, the necessary Application Development Features” is not included by default which you might need for some ASP.Net sites. So you have to explicitly switch them on as per the screenshot above.
This step will resolve the above error, but there is a good chance that after doing this, you might get another ASP.Net error. This is because after installing IIS or IIS features, you often have to re-install .NET framework 4 on your machine as .NET and IIS “fall out of sync”. This is accomplished by running the following from the command prompt:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -ir
MVC Webrid support keyboard (ctrl, shift) actions via Jquery
I had a custom built MVC webgrid that I was using inside a CRM solution and I wanted it to behave the same way as the CRM grids, or just grids in general, by supporting the following common features users would expect from their grids:
1. When a user clicks anywhere on a row on the grid, I want the checkbox of that row to become selected also, instead of having to rely on the user having to click inside the checkbox itself.
2. The user should be able to select/deselect mutiple rows by holding down the ctrl key with a mouse-click.
3. When holding down the shift key the user should be able to select all the rows between two clicks.
I thought this would be quite a common thing to do, as surely most people would want to change their grids to support these common behaviors. But would you believe it, I couldn’t find a single comprehensive solution anywhere on the web encompassing a piece of JQuery to support all three these requirements. So I went off to develop a piece of JQuery script that does just that. To support point 1 and 2, I wrote the following code:
$(“body”).on(“click”, “.table-grid tr”, function (event) {
if (!event.ctrlKey) { //since user is not pressing down CTRL or SHIFT, clear previous selections
$(‘.table-grid input[type=”checkbox”]’).each(function () {
$(this).prop(‘checked’, false);
});
//now select relevant checkbox
$(‘:checkbox’, this).prop(‘checked’, true);
}
//CTRL + MOUSE-CLICK
else {//ctrl key behaviour:, check/uncheck based on current checkbox state
var cb = $(‘:checkbox’, this).is(‘:checked’);
if (!cb) {
$(‘:checkbox’, this).prop(‘checked’, true);
}
else
$(‘:checkbox’, this).prop(‘checked’, false);
}
});
This should work on any mvc webgrid (which just becomes a table really) provided you have a checkbox on each row of the grid. This line
$(“body”).on(“click”, “.table-grid tr”, function (event) {
just replace .table-grid with the css class used by your grid, or use # if you prefer to reference your grid by id instead e.g. “#mygridid tr”
Next, I changed my code a bit to support point 3 as well. For this, we need to know the previous mouse click so that we know where the SHIFT key operation should start its selection from. We can just use a hidden html input like so:
<input type=”hidden” id=”shiftid” />
And then extend the JQuery function like so:
$(“body”).on(“click”, “.table-grid tr”, function (event) {
var lastSelected;
//SHIFT + MOUSE-CLICK
//(must have hidden input on page with id shiftid so that something is available to log previous click to)
if (event.shiftKey) {
var last = $(‘#shiftid’).val();
if (last > 0) {
var first = this.rowIndex;
if (first > last)
first = first + 1;
var start = Math.min(first, last);
var end = Math.max(first, last);
start = start + 1;
end = end + 1;
for (var i = start; i < end; i++) {
var tr = $(‘.table-grid tr’).eq(i);
$(‘:checkbox’, tr).prop(‘checked’, true);
}
}
else //treat like normal click
{
$(‘.table-grid input[type=”checkbox”]’).each(function () {
$(this).prop(‘checked’, false);
});
//now select relevant checkbox
$(‘:checkbox’, this).prop(‘checked’, true);
lastSelected = this.rowIndex;
$(‘#shiftid’).val(lastSelected);
}
//clear out selected text caused by shift key
var sel = window.getSelection ? window.getSelection() : document.selection;
if (sel) {
if (sel.removeAllRanges) {
sel.removeAllRanges();
} else if (sel.empty) {
sel.empty();
}
}
}
//NORMAL MOUSE-CLICK
else if (!event.ctrlKey) { //since user is not pressing down CTRL or SHIFT, clear previous selections
$(‘.table-grid input[type=”checkbox”]’).each(function () {
$(this).prop(‘checked’, false);
});
//now select relevant checkbox
$(‘:checkbox’, this).prop(‘checked’, true);
lastSelected = this.rowIndex;
$(‘#shiftid’).val(lastSelected);
}
//CTRL + MOUSE-CLICK
else {//ctrl key behaviour:, check/uncheck based on current checkbox state
var cb = $(‘:checkbox’, this).is(‘:checked’);
if (!cb) {
$(‘:checkbox’, this).prop(‘checked’, true);
lastSelected = this.rowIndex;
$(‘#shiftid’).val(lastSelected);
}
else
$(‘:checkbox’, this).prop(‘checked’, false);
}
});
Javascript/JQuery stops working on MVC webgrid paging/sorting
I recently put in a simple piece of JQuery to allow users to double-click on a row in the grid to perform a particular operation. This worked fine, but whenever the grid was paged or sorted – the double-click stopped working. This is a common problem when working with JQuery and MVC because the underlying HTML of the page changes with certain operation, without a full page postback. To get around the problem, use the “on” keyword provided by JQuery, available since JQuery 1.7.1 If you are using an earlier version of JQuery, you should use the “live” keyword. In my case, to get my double-clicking working even when the webgrid changed to a new page or was resorted, I changed the code from this:
$(‘#mygridid tr’).dblclick(function() {
alert(‘Double click on row);
});
to this:
$(“body”).on(“dblclick”, “#mygridid tr”, function() {
alert(‘Double click on row);
});



