Using PATINDEX for Regular Expressions in T-SQL
PATINDEX('[^0-9]%',@Input)
The above example would check that the variable @Input does not begin with a numeric character.
Labels: Code Snippets, T-SQL
PATINDEX('[^0-9]%',@Input)
Labels: Code Snippets, T-SQL
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
// Increment Page Index
GridView1.PageIndex = e.NewPageIndex;
// Need to rebind or page does not change
GridView1.DataSource = MyDataSet;
GridView1.DataBind();
}
Labels: Code Snippets, Web Development
private static void SetPixelExample(ref Bitmap bitmap)
{
int CurScanLine = 0;
Rectangle Rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
BitmapData TempBitmapData =
bitmap.LockBits(Rect, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
while (CurScanLine < bitmap.Height)
{
for (int x = 0; x < bitmap.Width; x++)
{
SetPixelNew(ref TempBitmapData, x, CurScanLine, Color.Red);
}
CurScanLine++;
}
bitmap.UnlockBits(TempBitmapData);
}
private static unsafe void SetPixelNew(ref BitmapData bitmapData, int x, int y, Color colour)
{
byte* PixelPointer = null; // Pointer to our pixel data
int ColourPlanes = 3;
PixelPointer = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
PixelPointer[ColourPlanes * x] = colour.B;
PixelPointer[(ColourPlanes * x) + 1] = colour.G;
PixelPointer[(ColourPlanes * x) + 2] = colour.R;
}
Labels: Code Snippets

private void TestGetPixelMethods()
{
Bitmap B = (Bitmap)Bitmap.FromFile(@"C:\MyImage.jpg");
// Execute and Time Managed Code
DateTime StartTime = DateTime.Now;
GetAllPixelsSafe(B);
DateTime FinishTime = DateTime.Now;
TimeSpan Duration = FinishTime - StartTime;
Debug.WriteLine(string.Format("GetAllPixelsSafe : {0} ", Duration));
// Execute and Time unsafe code
StartTime = DateTime.Now;
GetAllPixelsUnsafe(B);
FinishTime = DateTime.Now;
Duration = FinishTime - StartTime;
Debug.WriteLine(string.Format("GetAllPixelsUnsafe: {0} ", Duration));
Debug.WriteLine(string.Empty);
}
/// <summary>
/// Returns a list of Colors representing each pixel in the passed image
/// </summary>
private List<Color> GetAllPixelsSafe(Bitmap b)
{
List<Color> ColorList = new List<Color>();
for (int x = 0; x < b.Width; x++)
{
for (int y = 0; y < b.Height; y++)
{
ColorList.Add(b.GetPixel(x, y));
}
}
return ColorList;
}
/// <summary>
/// Returns a list of Colors representing each pixel in the passed image
/// Uses unsafe code
/// </summary>
private unsafe List<Color> GetAllPixelsUnsafe(Bitmap b)
{
Rectangle Rect = new Rectangle(0, 0, b.Width, b.Height);
BitmapData TempBitmapData =
b.LockBits(Rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
byte* PixelPointer = null; // Pointer to our pixel data
int ColorPlanes = 3; // 3 Color Planes in a 24bpp image
List<Color> ColorList = new List<Color>();
for (int x = 0; x < b.Width; x++)
{
for (int y = 0; y < b.Height; y++)
{
PixelPointer = (byte*)TempBitmapData.Scan0 + y * TempBitmapData.Stride + ColorPlanes * x;
ColorList.Add(Color.FromArgb(PixelPointer[2], PixelPointer[1], PixelPointer[0]));
}
}
b.UnlockBits(TempBitmapData);
return ColorList;
}

Labels: Code Snippets
protected void Button1_Click(object sender, EventArgs e)Also ensure that string passed into the "FindControl" function matches the name of the control you are looking for. Not that I'd be foolish enough to make such an elementary typo, of course.
{
bool CheckedValue;
foreach (RepeaterItem Item in Repeater1.Items)
{
CheckBox CB = (CheckBox)Item.FindControl("MyCheckBox");
CheckedValue = CB.Checked;
}
}
Labels: Code Snippets, Web Development
<configuration>
<appsettings>
<add key="MagicNumber" value="3">
</add>
</appsettings>
</configuration>
void GetAppSettings()
{
int MagicNumber =
Convert.ToInt32(ConfigurationManager.AppSettings["MagicNumber"]);
}
Labels: Beginner, Code Snippets
private string XORString(string inputString, byte key)
{
ASCIIEncoding Encoder = new ASCIIEncoding();
byte[] ByteArray = Encoder.GetBytes(inputString);
for (int i = 0; i < ByteArray.Length; i++)
{
ByteArray[i] ^= key;
}
return Encoder.GetString(ByteArray);
}
private void BruteForceXORString(string inputString)
{
ASCIIEncoding Encoder = new ASCIIEncoding();
byte XORVal = 0;
byte[] ByteArray;
while (XORVal < 255)
{
ByteArray = Encoder.GetBytes(inputString);
for (int i = 0; i < ByteArray.Length; i++)
{
ByteArray[i] ^= XORVal;
}
Debug.WriteLine(string.Format(
"{0}: {1}",
XORVal,
Encoder.GetString(ByteArray)));
XORVal++;
}
}

Labels: Code Snippets, Tutorials
private bool TestBit(ushort bit, uint mask)
{
// AND
return (mask & bit) == bit ? true : false;
}
bool Result = TestBit(8, MyValue);
private void SetBit(ushort bit, ref uint mask)
{
// OR
mask = (mask | bit);
}
private void SetBit(ushort bit, ref uint mask)
{
// OR
mask |= bit;
}
private void ToggleBits(uint key, ref uint value)
{
// XOR parameters together
value ^= key;
}
private void PerformOp()
{
uint Value = 7;
uint Key = 2;
ToggleBits(Key, ref Value);
}
private void ShiftLeft(ref uint value, ushort numberOfBitsToShift)
{
// Shift bits left
value <<= numberOfBitsToShift;
}
Labels: Code Snippets, Tutorials
public class Car
{
public enum MakeEnum
{
AlfaRomeo,
AstonMartin,
BMW
}
[Description("Make of Car")]
public MakeEnum Make { get; set; }
[Description("Sticker Price")]
public int Price { get; set; }
[Description("Year of Manufacture")]
public int Year { get; set; }
[Browsable(false)]
[Description("Target Price")]
public int TargetPrice { get; set; }
[Description("Primary Car Colour")]
public Color Colour { get; set; }
[Description("Vehicle description")]
public string Description { get; set; }
[Browsable(false)]
public string DealerNotes { get; set; }
}
private Car CreateCar()
{
Car C = new Car()
{
Make = Car.MakeEnum.AlfaRomeo,
Colour = Color.Red,
Price = 3000,
Year = 1998,
TargetPrice = 2700,
Description = "One lady owner.",
DealerNotes = "Total wreck, offload ASAP!"
};
return C;
}
private void Form1_Load(object sender, EventArgs e)
{
Car C = CreateCar();
propertyGrid1.SelectedObject = C;
}
Note that the Colour can be changed via the use of a ColorPicker, without any further coding being required. Likewise, the Car.Make property can be set by selecting from the pre-populated dropdown, which contains all the valid makes in MakeEnum. Labels: Code Snippets
When I don't tend to work with particular types on a regular basis, the knowledge of how to manipulate them has the nasty habit of slowly trickling out of one ear. Over time, I'm left with only a faint inkling of how to do what I wanted. In particular, converting between strings, byte arrays and char arrays is something that just doesn't want to sink in with any kind of permanence.
So, if only for my own sanity, here's how to convert between strings, char arrays and byte arrays. The examples provided use UTF-8 Encoding, you should be careful to select the encoding that is relevant to your own code. For example,you might want to use the ASCIIEncoding class.
If you're not sure which encoding you need, I highly recommend you read this!
private static void DoConversions()
{
string MyString = "Meerkat";
char[] CharArray;
byte[] ByteArray;
string NewString;
UTF8Encoding Encoder = new UTF8Encoding();
// Convert String to Char Array
CharArray = MyString.ToCharArray();
Console.WriteLine(CharArray);
// Convert String to Byte Array
ByteArray = Encoder.GetBytes(MyString);
foreach (byte b in ByteArray)
{
Console.Write(b + ",");
}
Console.WriteLine();
// Convert Char Array to Byte Array
ByteArray = Encoder.GetBytes(CharArray);
foreach (byte b in ByteArray)
{
Console.Write(b + ",");
}
Console.WriteLine();
// Convert Byte Array to String
NewString = Encoder.GetString(ByteArray);
Console.WriteLine(NewString);
// Convert Char Array to String
NewString = new string(CharArray);
Console.WriteLine(NewString);
// Convert Byte Array to Char Array
CharArray = Encoder.GetChars(ByteArray);
Console.WriteLine(CharArray);
// Convert a subset of a string to a char array
CharArray = MyString.ToCharArray(4, 3);
Console.WriteLine(CharArray);
}
Labels: Code Snippets
private bool IsValidHamsterQuantity(int hamsterQty)
{
if (hamsterQty > 7)
return false;
return true;
}
private bool IsValidHamsterQuantity(int hamsterQty)
{
return hamsterQty > 7 ? false : true;
}
private int GetData(DataRow DR, string column, int mod)
{
return (int)DR["Static"] == 1 ? (int)DR[column] + mod : ((int)DR["Var"] * (int)DR["Var2"]) + mod;
}
Labels: Code Snippets
[Flags]
public enum EntityFlags
{
NoEffect, // 0000
IsVisible, // 0001
IsSelectable, // 0010
IsCollidable, // 0100
IsDestroyable, // 1000
}
Labels: Code Snippets


Labels: Code Snippets, Social Considerations



Labels: Code Snippets
using (SolidBrush SB = new SolidBrush(Color.Red))
{
// Code
}
SolidBrush SB = new SolidBrush(Color.Red);
Labels: Code Snippets