Skip to content

Instantly share code, notes, and snippets.

@Dynyx
Dynyx / _layout.cshtml
Created June 4, 2012 12:56
Default _layout.cshtml
<!--
Requires the following Nuget packages:
1. MVCFlash
2. JQueryUI (combined)
3. Fancybox
-->
<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title</title>
@Dynyx
Dynyx / IsNumeric.cs
Created June 4, 2012 13:17
IsNumeric extension method
public static class StringExtensions
{
public static bool IsNumeric(this String str)
{
double temp;
return double.TryParse(str, out temp);
}
}
@Dynyx
Dynyx / ExceptionLogging.cs
Created June 4, 2012 13:03
Logging exceptions
// The Logger Class
public static class Logger
{
public static void Log(string message)
{
File.AppendAllText("C:\\MyLogFile.txt", DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + ":\t" + message + Environment.NewLine);
}
}
// Usage
@Dynyx
Dynyx / Age.cs
Created June 4, 2012 13:07
Calulate age from a given date
DateTime now = DateTime.Today;
int age = now.Year - bday.Year;
if (bday > now.AddYears(-age)) age--;
@Dynyx
Dynyx / ResetTableIdentity.sql
Created June 4, 2012 12:58
Reset table identity
--Reset identity to 0
DBCC CHECKIDENT('TableName', RESEED, 0)
@Dynyx
Dynyx / HandyRegexExpressions.txt
Created June 4, 2012 13:00
Handy regex expressions
//replace all but links
/<(?!\/?a(?=>|\s.*>))\/?.*?>/g
//validate url
/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \?=.-]*)*\/?$/
//Validate US phone number
/^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/
//Test if a password is strong
@Dynyx
Dynyx / CustomValidation.cs
Created June 4, 2012 13:08
Custom data validation using data annotations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
[MetadataType(typeof (FooBarMetaData))]
public class FooBar
{
// Insert FooBar related properties, methods, etc. here
@Dynyx
Dynyx / EventLog.cs
Created June 4, 2012 13:03
Log exceptions to the event log
public static void LogException(string ErrorDescription)
{
// The name of our log in the event logs
string Log = “AspNetError”;
// Check to see fi the log for AspNetError exists on the machine
// If not, create it
if ((!(EventLog.SourceExists(Log))))
{
@Dynyx
Dynyx / Delegate.cs
Created June 4, 2012 13:16
Delegate example
delegate string DelegateTest(string str);
DelegateTest delTest = new DelegateTest(Class1.Hello);
Console.WriteLine(delTest("LOL"));
class Class1{
...
public static string Hello(string msg) {
return "Hello " + msg;
}
@Dynyx
Dynyx / EnumEnumerator.cs
Created June 4, 2012 13:06
Enum enumerator
Array lstProvider = System.Enum.GetValues( typeof(CommonShared.EnumProvider));
foreach (CommonShared.EnumProvider enProvider in lstProvider)
{
/// Do something
}