Hints and Tips

“Cannot perform runtime binding on a null reference”

The other day I was testing MVC some code, and after my form submission my application kept on coming back with an exception:

Cannot perform runtime binding on a null reference

Ok cool, I thought – I hadn’t set a variable properly.  Confusion reigned when I stepped through the code, just before the model was returned to the view and found, lo and behold – the variable in error is NOT null and was very much a valid data item.

What?  Why would the compiler tell me that my variable (using the Html.Hidden MVC Helper method) was empty when it clearly wasn’t.

So then I commented out the code, only for it to happen on the next line.


After a few minutes of asking my PC “What?” and “why?”, I realised that the debugger was telling me lies, and that there was an issue with a null reference – just not in that point.

Turns out I’d added a ViewBag data item in the GET method, but in the POST method I hadn’t re-populated it.  This was an IEnumerable which was the basis of a list, hence the reason why it was needed when the view was returned to the model.

Once that was rectified, all was right in the world.  Phew.

Moral of the story – Don’t believe everything you read, not even .NET debugger notices!


By |August 21st, 2014|Hints and Tips|Comments Off on “Cannot perform runtime binding on a null reference”

Redirect to different page using Contact Form 7

Today I had a request from a client who wanted to redirect users who submitted a particular form on their WordPress website to a different page on the site.  The reason for this is they had quite a few people who filled out the very large form, didn’t notice an error and then left thinking it had all worked.

You’d think it’s pretty easy right?  This proved to be a bit more complex than first thought.

Research showed adding this into the “Additional Settings” part of the Contact Form would just do the job:

It didn’t.  That led me up the garden path of JQuery installations and references within the site, all of which were fine.

Then I stumbled across this solution, which did the business in a matter of minutes.

Step 1

Add the following to your Theme Functions file (functions.php).  This adds code to the post-mail-sending process of the contact form.

Step 2
In the contact form – go to the Additional Settings (right down the very end of the page) and add in this line:

(Naturally you would replace the part in red with the URL you want to redirect to).

Then – head to your contact form page on the website and try it out.  For me it worked first time!


Nested Forms using HTML5

So many times I’ve built software with a single form, and plenty of AJAX buttons perform other key functions.  This can be done by adding buttons which perform a task different to the main form, and posting using AJAX to perform the data changes.

It works well.  But it does require a bit of extra work to advise the user that yes, they did actually click on the button and yes, the website is actually working.  Disabling the button, showing an in-progress indicator – there are many choices here.

The number of times I’ve thought – wouldn’t it be great if we could just setup a nested form and perform a nice easy submit that does the job.

Well – now you can.  Thanks to the wonder of HTML5.  And it’s so much easier than you’d think.

Let’s say we’ve got this setup:
<form method=”post” action=”/action/save”>
<input id=”id” name=”id” value=”someid” />
<input type=”submit” value=”Save” />
<input type=”submit” value=”Delete” />
One form – a save button and a delete button.  The setup above causes some problems, because two submit buttons means both will submit the absolute-parent form (/action/save).  We can add further parameters in on the button to see which was called, but really – why?

Especially when you can do this using HTML5:
<form method=”post” action=”/action/save” id=”save-form”>
<input id=”id” name=”id” value=”someid” />
<input type=”submit” value=”Save” form=”save-form” />
<input type=”submit” value=”Delete”  form=”delete-form” />
 <form method=”post” action=”/action/delete” id=”delete-form”>
<input id=”id” name=”id” value=”someid” />
So easy – add a “form” attribute to the submit buttons in the parent form.  Then – outside the parent form, setup a small form with just the controls required to post to the action.  Wire up the “form” attribute to the ID of the relevant form, and bingo – you’ve now got multiple submit buttons posting correctly to the relevant […]

Repeating Windows batch commands

Often I need to run an installation script on a database, and then run a series of repeated commands to get data up to date (for example, pre-set a number of rows in a database for timesheeting or creating a number of default entries).  If you’ve got an executable that you need to run a number of times, you can use this command to do it.

for /l %x in (1, 1, <repeat-n-times>) do <command>

where <repeat-n-times> is the number of times you want to repeat, and <command> is the dos command you want to run.

For example, if I wanted to repeat the time 20 times in a row (not sure why this would be useful – but good as an example!):

for /l %x in (1, 1, 20) do echo %date% %time%

If you’re running it in a batch file – then you’ll need two % symbols instead of one, ie

for /l %%x in (1, 1, 20) do echo %%date%% %%time%%

If you have multiple commands for each iteration of the loop, do this:

for /l %x in (1, 1, 20) do (
echo %x
copy %x.txt %x.txt.%x


Welcome to our Blog

Welcome to the Viva IT Services blog!

We’ll post great little tips and techniques for building software, as well as some news around the IT world that makes us sit up and take notice.

If you’re a tech-head interested in software development, make sure you keep any eye here and also like us on Facebook!

If you’ve got any questions or you would like us to post something about a particular point related to software development, make sure you contact us here.