Archive for December, 2015

Using TempData to pass a Model to an MVC Controller

There may be a better way, but…

I needed to store my user information, stored in the UserProfile class and, to reference it again on a subsequent user action. Rather than making another call, I stored it in TempData. In my Login action, I pull the user information from the database:

       public ActionResult Login(LoginModel model, string returnUrl) {
                using (UsersContext db = new UsersContext()) {
                    UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
                    TempData["User"] = user;

And later, I once again reference my user database like so:

public ActionResult CoolLink(string a) {
    UserProfile model = (UserProfile)TempData["User"];

I’m sure there is a very MVC way to do it, but I just wanted to get this done and get on with other problems. You can see the TempData definition on MSDN.

Leave a comment

Calculating the last day of a month in .NET

I had a drop down list which allowed the user to select a month and year, such as “December 2015”. The value of the DLL was the start date (“12/1/2015”), but I needed to generate the end date based on the start date. So here is the little method I wrote to do that, which uses the DateTime.DaysInMonth method to get the totals days of the selected month:

private DateTime CalculateEndDate(DateTime startDate) {
    return new DateTime(startDate.Year, startDate.Month, DateTime.DaysInMonth(startDate.Year, startDate.Month));



Leave a comment

ORA-00932: inconsistent datatypes

I was seeing this error in my .NET project:

ORA-00932: inconsistent datatypes: expected - got CLOB

The problem was occurring with a simple statement

 public List<Coasters> GetCoasters(string city) {
  return = cList.Where(c => c.City == city).ToList();

The problem was that the “city” parameter was null. I added a simple parameter check to fix it:

 city = city == null ? String.Empty : city;

Leave a comment

Enabling Session variables in Web API

When trying to access Session variables in Web API, I was seeing the familiar and hated “Object reference not set to an instance of an object”. In fact, the error wasn’t  directed at any variable, it was directed at the Session class itself, HttpContext.Current.Session.

Luckily, I found Oleksandr Kulchytskyi’s article on Code Project, and that saved my bacon, and the rest of breakfast for that matter. It’s very clear, so I recommend following his instructions. I am including my implementation, which used Web API 2, partly because on the Internet, things sometimes disappear, and partly because of a modification I had to make.

I have a class that implements the required interfaces:

using System.Web;
using System.Web.Http.WebHost;
using System.Web.Routing;
using System.Web.SessionState;
namespace Coaster.Controllers {
    public class SessionableControllerHandler : HttpControllerHandler, IRequiresSessionState {
        public SessionableControllerHandler(RouteData routeData)
            : base(routeData) { }

    public class SessionStateRouteHandler : IRouteHandler {
        IHttpHandler IRouteHandler.GetHttpHandler(RequestContext requestContext) {
            return new SessionableControllerHandler(requestContext.RouteData);

The one change from Oleksandr’s instructions is the RouteConfig.cs file, since I believe I’m using a later version that he was:

            name: "PagingApi",
            url: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        ).RouteHandler = new SessionStateRouteHandler();

Here is where I should include the obligatory comment about Web API, RESTful services, and Session variables, but I won’t. Adding Session variable to Web API made the most sense for improving application performance and making users happy. So there.

Leave a comment

ORA-12154: TNS:could not resolve the connect identifier specified

I had been happily using my application locally for some time, but it was time to deploy it for user testing, and we know how things go wrong when that happened. After publishing my MVC application, I starting getting this error:

 ORA-12154: TNS:could not resolve the connect identifier specified

Research suggested a connection string problem. In my web.config, I had a connection string similar to this:

 <add name="ConnString" connectionString="DATA SOURCE=ScottDb;PASSWORD=blah;USER ID=scott" />

What I needed to do was configure it to look like an entry in the tnsnames.ora file, like so:

 <add name="ConnString" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=Some-Host)(PORT=6000))(CONNECT_DATA=(;User Id=scott;Password=blah;" />

If your not sure where your tnsnames.ora file is located, you can try using the tnsping command to locate it. When I ran the command “tnsping ScottDb”, the file location and connection string was returned, so that may help you.

Leave a comment

POST command fails to Web API

I had been successfully making GET calls using jQuery to my Web API service, but when I tried a POST, it failed, the request never reaching the breakpoint in my C# code. After using the IE developer tools, I saw that it was pointing to “/sites/apps/MyPost”, and it should have been pointing to “localhost/MyPost”. On the HTML page, there was this line:

 <base href="/sites/apps/" />

I commented-out that line, and the POST command was successful.

Leave a comment