views:

230

answers:

2

I'll just jump right into it. I have a model called "Request" which I created using LINQ to SQL. I have another model called "Comment". A "Request" can have many "Comments".

In my controller I have this code:

public ViewResult Details(int? id)
    {
        return View(requestService.GetRequest(id));
    }

In my view I have this markup (edited for brevity)

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Models.Request>" %>
 <p>
        CustomerNumber:
        <%= Html.Encode(Model.CustomerNumber) %>
    </p>
    <p>
        CustomerName:
        <%= Html.Encode(Model.CustomerName) %>
    </p>

    ... etc ...


    <table id="comments">
    <thead>
        <tr>
            <th>Date</th>
            <th>User Name</th>
            <th>User IP</th>
            <th>Comment</th>
        </tr>
    </thead>
    <tbody>
    <% foreach (var comment in Model.Comments) { %>
        <tr>
            <td><%= Html.Encode(comment.CommentDate) %></td>
            <td><%= Html.Encode(comment.CommentUserName) %></td>
            <td><%= Html.Encode(comment.CommentIP) %></td>
            <td><%= Html.Encode(comment.Comment1) %></td>
        </tr>
    <% } %>
    </tbody>
</table>

Ok, here's what's happening. When I call the Details method/action, it uses my service layer to go get the request that the user wants to see just fine. However, when it goes to loop over the Comments associated with the Request, it throws an "Invalid Cast Exception" and dies. This code was working 100% correct two days ago, and no changes were made to this portion of the project (other files, but not this stuff).

I have tried to find the casting problem and I'm at a loss. Please help me, as I think this is my last obstacle to overcome before my project can really take off. If you need anymore information, let me know.

A: 

Do you have unit tests written for GetRequest? If not, I suggest you write one to see what happens when that method is called.

At the very least, set a breakpoint and set into that method to make sure that the Comments collection is being set.

Is CommentDate a DateTime? You might want to do a .ToString() on it. Are any of the members of Comment coming back null? Lots of stuff to check for. Lots of great opportunities for automated unit tests here. :)

Good luck!

Terry Donaghe
A: 

Well, after walking away from this project for awhile after working on others, I examined it with fresh eyes and I found the problem.

I generated my model against the "production" database and all of its data relationships. When I got to the portion of the project where I was going to begin actually writing data to the database, I switched to the test database to do all my work. What I did not realize was that the data type for the primary key on the production "Comments" table was different from the test "Comments" table (why? I don't know....) They were both integers, but the prod version was "bigint" and the dev version was "int", so it was hard to spot.

So, since it was possible for truncation to occur, it freaked out and threw an InvalidCastException (which is perfectly logical in this case). I guess it just goes to show you that a little time spent apart from a project can do you some good. Anyways, hope my misfortune can help others avoid theirs.

Robert Iver