Archive for June, 2015

EntityException: The underlying provider failed on Open

The application I was debugging went wonky on me (“Out of Memory” exceptions, in case your curious), so I had to kill Visual Studio and start over. When I did this, I got the following unfamiliar error when trying to connect to a database using Entity Framework:

EntityException: The underlying provider failed on Open

A good resource appeared to be Stack Overflow, but those weren’t the issues I was dealing with.

I opened SQL Server Management Studio and looked at my database. There was a little triangle of doom icon next to it, with the words “Suspect”. That didn’t sound good. Visions of viruses and evil geniuses trying to take over the word on database at a time rushed into my brain.

Turns out it wasn’t that big a deal to fix. I found a great post at https://support.managed.com/kb/a398/how-to-repair-a-suspect-database-in-mssql.aspx, and that got me back up and running.

Leave a comment

That string ain’t right, MVC edition

Lurking in my web.config file for the MVC application that had been forced upon me was a line for acceptable file types:

<add key="ValidFileAttachmentTypes" value=".xls,.xlsx,.doc,.docx,.txt,.pdf" />

In addition to its more useful features, I wanted to display this string in my view to show users who would be uploading their horrid little files (I jest, sort-of) to the system.

So I write this terribly complex code to do so:

@Html.Label("Valid file types are: " + ConfigurationManager.AppSettings["ValidFileAttachmentTypes"])

And what’s its output: pdf

Watchoo talkin’ ’bout, Willis? That ain’t be what I wanted. (Reading that sentence makes want to 1) Watch “Diff’rent Strokes” and 2) Listen to “Word Crimes” by Weird Al Yankovic).

The solution was to use the string Replace method and replace the periods with empty spaces:

@Html.Label("Valid file types are: " + ConfigurationManager.AppSettings["ValidFileAttachmentTypes"].Replace('.',' '))

Tout va bien.

Leave a comment

Why is my model null in the view?

I had created the Index action in my MVC controller. Nothing fancy, just something to get me started:

        public ActionResult Index() {
            Role role = new Role();
            role.Activities = new List<Activity>();
            role.Activities.Add(new Activity(1, "Run"));
            role.Activities.Add(new Activity(2, "Walk"));
            role.Activities.Add(new Activity(3, "Sprint"));
            return View();
        }

Then in my view, I did the simple action of looping through the activities:

@model Web.Models.RoleMode
@using (Html.BeginForm("New", "Roles", FormMethod.Post, new { @class="form-horizontal"})) {
    <h1>Roles</h1>
    foreach (var item in Model.Activities) {
        <p>hi</p>
    }
}

However, I was getting an “Object reference not set…” on the Model.

The problem was that I wasn’t passing the model from the controller to the view. See that last line in the controller:

      return View();

What I needed to do was add the model:

      return View(role);

That fixed it.

Leave a comment

Authorization attribute not working in MVC

I had been forced to use MVC, and needed to add an authorization filter to a controller action. No worries:

[Authorization]
public ActionResult View(int itemId) {
 …
}

And my actual filter code:

    public class AuthorizationAttribute: AuthorizationFilterAttribute {
        public override void OnAuthorization(HttpActionContext actionContext) {
            …
        }
    }

But, my breakpoint inside the OnAuthorization method wasn’t firing.

Turns out that I was using the wrong AuthorizationFilterAttribute! I needed to use the one in the System.Web.Mvc namespace, not the one lurking in System.Web.Http. So my corrected code looked like so:

    public class AuthorizationAttribute : System.Web.Mvc.AuthorizeAttribute {
        protected override bool AuthorizeCore(HttpContextBase httpContext) {
            …
            return true;
        }
    }

1 Comment