Skip to content

Instantly share code, notes, and snippets.

View danielgreen's full-sized avatar

Daniel Green danielgreen

  • Glasgow, United Kingdom
View GitHub Profile
@danielgreen
danielgreen / AppContainer.cs
Last active May 27, 2016 06:24
Suggested Unit Of Work facade to wrap around the Entity Framework ObjectContext. May require adjustment for use with DbContext in latest versions on Entity Framework. Includes a mechanism to perform concurrency checks on entities based on timestamps.
// http://dillieodigital.wordpress.com/2011/10/12/automatic-user-and-time-stamping-in-entity-framework-4/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Objects;
using System.Web;
using MyApp.BLL;
namespace MyApp.DAL
@danielgreen
danielgreen / OrphanedUsers.sql
Created June 12, 2013 16:11
SQL script to identify orphaned database users (where the SQL Server login or Windows login is missing)
DECLARE @cmd1 VARCHAR(MAX)
DECLARE @cmd2 VARCHAR(MAX)
IF EXISTS (SELECT object_id FROM tempdb.sys.objects WHERE name like '#OrphanUsers%')
DROP TABLE #OrphanUsers
CREATE TABLE #OrphanUsers
(
UserName VARCHAR(50) NULL,
userSID VARBINARY(85) NULL,
dbName VARCHAR(50) NULL
@danielgreen
danielgreen / DateTimeExtensions.cs
Created June 13, 2013 10:33
DateTimeExtensions - useful extension methods for date processing
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Globalization;
using System.Linq;
using System.Text;
namespace Utility
{
// http://dotnetslackers.com/articles/aspnet/5-Helpful-DateTime-Extension-Methods.aspx
@danielgreen
danielgreen / BaseConfigurationElementCollection.cs
Created June 13, 2013 10:43
Code to create a custom configuration section with child collections
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Config
{
public abstract class BaseConfigurationElementCollection<TKey, TElement>
@danielgreen
danielgreen / Readme.md
Created June 13, 2013 11:06
Check for unsaved changes on a page

Develop an unobtrusive approach - if a form has the data-warn-unsaved="true" attribute then attach change event handlers to its fields. Do this for all such forms on the page. The handlers can set data-unsaved-changes="true" on the form.

Set window.onbeforeunload to a function that checks for any forms with data-unsaved-changes="true". If any such forms are found, return some text to be displayed as a prompt to the user.

When a form's contents are saved successfully, we want to set data-unsaved-changes="false". Define an event that can be invoked upon a successful save, in order to set the attribute's value?

Also see http://codethug.com/2013/02/01/knockout-binding-for-onbeforeunload/

@danielgreen
danielgreen / Readme.md
Created June 13, 2013 11:08
MVC tips - don't return a View from an HTTP Post action

A Post action should redirect to a Get action rather than returning a View. This avoids the annoyance of the browser displaying the "do you want to resubmit the form" message if the user tries to refresh the page after the Post.

@danielgreen
danielgreen / BlockPageOnSubmit.js
Last active December 18, 2015 17:59
An unobtrusive way to block the page on form submission. Requires jQuery Validation to be active on the form (either directly or via unobtrusive validation), and the submithandlerfix to be applied (https://gist.github.com/danielgreen/5669244). If jQuery Validation is not active on the form then we need another solution e.g. just bind to onsubmit
$("form[data-block-page-on-submit]").each(function () {
$(this).validate().settings.submitHandler = function () {
window.BlockPage();
return true;
};
});