Skip to content

Instantly share code, notes, and snippets.

@kristofclaes
kristofclaes / Custom 404-message
Created April 6, 2011 09:33
How I wired up the custom 404-message. IE and Chrome never showed my page because of browser settings. Took me a while to figure that one out :(
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404" />
<error statusCode="404" path="error404.htm" />
</httpErrors>
</system.webServer>
@kristofclaes
kristofclaes / gist:1008780
Created June 5, 2011 08:21
Retrieving next and previous records with Simple.Data
// This code works, but somehow it looks like there is a better and more elegant solution for it.
IEnumerable<Models.Photo> previous = DB.Photos
.FindAll(DB.Photos.Published == true && DB.Photos.DatePublished < currentPhoto.DatePublished.Value)
.OrderByDatePublishedDescending()
.Take(1)
.Cast<Models.Photo>();
int? previousPhotoId = null;
Models.Photo previousPhoto = previous.FirstOrDefault();
@kristofclaes
kristofclaes / gist:1008800
Created June 5, 2011 08:47
Another way of retrieving next and previous records with Simple.Data
// This also works, but again, it doesn't look very elegant
int? previousId;
try
{
previousId = DB.Photos
.FindAll(DB.Photos.Published == true && DB.Photos.DatePublished < currentPhoto.DatePublished.Value)
.OrderByDatePublishedDescending()
.Take(1)
.Select(DB.Photos.Id)
@kristofclaes
kristofclaes / gist:1016152
Created June 9, 2011 06:02
How one of my ex-colleagues decided to fill a dropdownlist with hour values from "08:00" to "18:00"
// How one of my ex-colleagues decided to fill a dropdownlist with hour values from "08:00" to "18:00"
for (int i = 8; i <= 18; i++)
{
ListItem li = new ListItem();
li.Value = i.ToString();
TimeSpan ts = DateTime.Now.AddHours(i) - DateTime.Now;
DateTime dtTemp = new DateTime(ts.Ticks);
@kristofclaes
kristofclaes / gist:1017271
Created June 9, 2011 17:43
Getting the previous photo
// When I run this code when no previous photo should be found, I get
// an exception saying "Query returned multiple rows; cannot return
// scalar value.". This is strange because it shouldn't return _any_
// values and because I use ".Take(1)" to make sure only one row is
// returned.
string previousSlug = DB.Photos
.Query()
.Select(DB.Photos.Slug)
.Where(DB.Photos.Published == true && DB.Photos.DatePublished < photo.DatePublished.Value) // There is no published photo with a smaller DatePublished than the one I check against
@kristofclaes
kristofclaes / gist:1109126
Created July 27, 2011 10:43
I don't like this, but I don't want to add too much business logic to a stored procedure either. How can this be improved?
var context = new DbContext();
foreach(var itemId in itemIds) // on average about 10 items
{
var item = new Item();
item.ForeignKey = itemId;
item.SomeProperty = someValue;
foreach(var subItemId in subItemsIds) // on average about 20 subitems
{
@kristofclaes
kristofclaes / gist:1197460
Created September 6, 2011 12:54
Look at the code I have to work with...
DateTime date = Convert.ToDateTime(xn["date"].InnerText);
string dateString;
dateString = date.Day + "-";
if (date.Month < 10)
{
dateString += "0";
}
dateString += date.Month + "-" + date.Year;
@kristofclaes
kristofclaes / 01-Entities.cs
Created November 2, 2011 08:30
Doing two joins for one table?
public class Parent
{
public int ParentId { get; set; }
public int Name { get; set; }
public string Description { get; set; }
public string ExtraInfo { get; set; }
public string AnotherExtraInfo { get; set; }
public string AndAnotherExtraInfo { get; set; }
public virtual ICollection<Child> Children { get; set; }
@kristofclaes
kristofclaes / 01-Example.cs
Created November 9, 2011 09:38
Removing a child entity
var context = new MyContext();
var parent = context.Parents.Include("Children").SingleOrDefault(x => x.ParentId == 1);
var child = parent.Children.First();
var parent.Childred.Remove(child); // Is this step necessary?
var context.Childred.Remove(child); // Or is this alone sufficient?
context.SaveChanges();
@kristofclaes
kristofclaes / HomeController.cs
Created June 25, 2012 07:37
HomeController.cs
using System;
using System.Web.Mvc;
namespace MvcApplication21.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new Models.TestModel { DateNaissance = DateTime.Now });