Archive for June, 2013

“Object reference not set to an instance of an object” error in a test project

I was doing what all the cool kids were doing these days, test-driven development. Just the name sparks excitement and intrigue, and makes me want to travel to exotic locations sipping martinis that have been shaken, not stirred. But instead, I’ll probably sit here and look at cube walls.

In my test project, I wrote a test to verify that my connection string exists:

public void Verify_connection_string_exists()

Beautiful. But when I ran my test, I got this message: Object reference not set to an instance of an object.

I tried not to take this personally. After all, unit tests generally don’t have a dislike for an individual programmer, they hate all programmers equally. In this case, I had the minor issue of not having an App.config file in my test project, so the ConfigurationManager class had nothing to pull from.

So, I dragged and dropped the web.config file from the web project I was testing into my test project and renamed it App.config in test project.

And that was it. Once I ran the code again, my simple mistake was erased, never to be heard from again until I forget to include my config file in my project and need this post to remind me.

Leave a comment

Comparing rows from disparate tables in VB.NET and SSRS

I love using the word “disparate”, by the way.

I needed to compare two fields returned from a SQL query to determine which of the two fields occurred later. The fields were text, and were in the format “Consulting Services – 6/3/2013”. I had two different tables that this data was being returned from, Products and Services. (Yes, the names have been changed to protect the innocent.) And I needed to show this data in a SSRS report.

Here is the VB.NET function I used inside Reporting Services to compare the dates of these two fields. I have added plenty of comments, because everyone loves over-commented code:

Public Function GetLatestProductOrService(ByVal Product As String, ByVal Service As String) As String

        ‘ In SSRS, if the fields are empty, they are sent as Nothing.
If Product Is Nothing Then Product = String.Empty
If Service Is Nothing Then Service = String.Empty

        ‘ Get the space right before the date portion of the string
Dim ProductSpace As Integer = Product.LastIndexOf(” “)
Dim ServiceSpace As Integer = Service.LastIndexOf(” “)

       ‘ If neither products nor services were returned, then don’t display anything
If ProductSpace < 0 AndAlso ServiceSpace < 0 Then
Return String.Empty
End If

       ‘ If there is a value for the product, strip out the date from the string. If there was no service passed to the function,

       ‘no need to go further, so return the product
Dim ProductDate As String = String.Empty
If ProductSpace > -1 Then
ProductDate = Product.Substring(ProductSpace, Product.Length – ProductSpace)
If ServiceSpace < 0 Then
Return Product
End If
End If

       ‘ Like above, just for services
Dim ServiceDate As String = String.Empty
If ServiceSpace > -1 Then
ServiceDate = Service.Substring(ServiceSpace, Service.Length – ServiceSpace)
If ProductSpace < 0 Then
Return Service
End If
End If

Dim LastUpdateDate As String = String.Empty

       ‘ If both products and services were passed to the function, see which was processed last
If CDate(ProductDate) > CDate(ServiceDate) Then
LastUpdateDate = Product
LastUpdateDate = Service
End If

Return LastUpdateDate

End Function

Calling this beautiful function through VB.NET code is simple:

Dim Result As String = GetLatestProductOrService(Nothing, “Widget – 06/17/2013”)

Calling it in Reporting Services is not much harder. I just added an expression to the TextBox where I wanted the text to appear:

=Code.GetLatestProductOrService(Fields!LastProduct.Value, Fields!LastService.Value)



Leave a comment

Hiding a link in ASP.NET MVC 4

A quick tip about visibility for y’all just starting out with MVC. I wanted to hide the “Delete” link on my view from all users who did not create the item being viewed. I was using the default authentication / authorization code that comes with MVC 4. I compared the current Identity name of the user to the user who created the item being viewed, showing the link if they were authorized to delete it:

@model IEnumerable<Park>
@Html.DisplayNameFor(model => model.Name)

@foreach (var item in Model) {
@Html.DisplayFor(modelItem => item.Name)
    @if (User.Identity.Name == item.CreatedByUser)
         { @Html.ActionLink(“Delete”, “Delete”, new { id=item.ParkID }) }


, ,

Leave a comment