Skip to content

Instantly share code, notes, and snippets.

View Krummelz's full-sized avatar

Jacques Dés Prés Krummelz

  • Durban, South Africa
View GitHub Profile
@Krummelz
Krummelz / iTextSharp
Created August 11, 2017 07:14
Sample iTextSharp code that can be used to prevent content cutting over to the next page and being cut in half.
// The problem is when you have a table,
// which gets cut in half at the botto of one page,
// and the other half shows up at the top of the next page.
// To prevent this, you can wrap your table in a container table (with a single cell)
PdfPTable containerTable = new PdfPTable(1);
containerTable .WidthPercentage = 100;
PdfPCell containerTableCell = new PdfPCell();
@Krummelz
Krummelz / Useful C# Extension Methods
Created September 12, 2013 07:26
Useful C# Extension Methods
/// <summary>
/// This will return a camel-cased string, so that it reads as a normal string, by adding a space before each capital letter.
/// </summary>
public static string SpaceOutCamelCasing(this string theWord)
{
char[] temp = theWord.ToCharArray();
string Result = "";
foreach (char c in temp)
if (c.ToString() == c.ToString().ToUpper())
@Krummelz
Krummelz / GridView to CSV Export Utility
Created September 12, 2013 07:23
A utility class to export an ASP.NET GridView control with its contents to a CSV file.
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
/// <summary>
/// Exports a GridView with its contents to a CSV file
/// </summary>
public class GridViewExportUtil
{
@Krummelz
Krummelz / Serialize ANTHING to XML
Created August 29, 2013 09:33
Courtesy of @Dominic_ZA
public static XElement Serialize(this object o)
{
XElement e = new XElement("property");
PropertyInfo[] properties;
Type _t = o.GetType();
properties = _t.GetProperties();
if (properties.Length == 0) //This implies that it is a VALUE TYPE
e.SetValue(o);
@Krummelz
Krummelz / Useful C# Extension Methods
Created August 29, 2013 09:28
Everyone loves extension methods!
public static bool IsNumeric(this string s)
{
double d = 0.0;
return (double.TryParse(s, out d));
}
public static int? ToIntegerNullable(this string s)
{
int i = 0;
if (int.TryParse(s, out i))
@Krummelz
Krummelz / C# image processing, useful for resizing images and making thumbnails
Created August 29, 2013 09:27
Just call "ProcessImageAndItsThumbnail" with your newly uploaded image's URL, and it will resize it for you, and make a thumbnail as well. Just remember to set the sizes you want for these new images.
protected void ProcessImageAndItsThumbnail(string url)
{
//make memory streams to work with the image bytes
MemoryStream imageStream = new MemoryStream();
MemoryStream thumbStream = new MemoryStream();
//using, so resources get disposed automagically
//using the uploaded image (which might be huge)
using (System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(url)))
{
from p in PersonOrders
// put your where clause here
group p by p.PersonID into grp //could be grouped by anything, Grouping on Guid ID's is not a good idea
let MaxOrderDatePerPerson = grp.Max ( g=>g.OrderDate )
from p in grp
where p.OrderDate == MaxOrderDatePerPerson
select p
@Krummelz
Krummelz / Javascript function to round decimal values
Created August 29, 2013 09:19
Call the method like so: onkeypress="return inputLimiter(event, 'Numbers', this.value, 5);"
function roundNumber(number, decimal_points) {
if (!decimal_points) return Math.round(number);
if (number === 0) {
var decimals = "";
for (var i = 0; i < decimal_points; i++) decimals += "0";
return "0." + decimals;
}
var exponent = Math.pow(10, decimal_points);
var num = Math.round((number * exponent)).toString();
@Krummelz
Krummelz / Javascript Character Input Restriction & Length Limiter
Created August 29, 2013 07:15
Restricts the characters that are entered into a textbox. Also limits the length of the value to 5. Eg: <input type="text" onkeypress="return inputLimiter(event,'Numbers', this.value)" />
function inputLimiter(e, allow, value) {
var AllowableCharacters = '';
if (allow == 'Letters') { AllowableCharacters = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; }
if (allow == 'Numbers') { AllowableCharacters = '1234567890'; }
if (allow == 'NameCharacters') { AllowableCharacters = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-.\''; }
if (allow == 'NameCharactersAndNumbers') { AllowableCharacters = '1234567890 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-\''; }
var k;
k = document.all ? parseInt(e.keyCode, 10) : parseInt(e.which, 10);
if (k != 13 && k != 8 && k !== 0) {
if ((e.ctrlKey === false) && (e.altKey === false)) {