 |
The easiest Hex string to/from byte array I've found
Paul Hayman (433 views)
The easiest Hex string to/from byte array I've found
When looking for a utility function to convert from a hex string to a byte array, I found this example .. works a treat
http://programmerramblings.blogspot.com/2008/03/convert-hex-string-to-byte-array-and.html
Code
#c#/// <SUMM
|
 |
Instanciating an object from it's type name
Paul Hayman (278 views)
Instanciating an object from it's type name
Instanciating a control from it's type name is easy..
#c#string typeName = "YourNameSpace.YourObject";
#c#
#c#Type assType = Type.GetType(typeName);
#c#object dynamicControl = Activator.CreateInstance(assType);
One thing you may run into is t
|
 |
Returning the first few items from an Array list or IEnumerable collection
Paul Hayman (380 views)
Returning the first few items from an Array list or IEnumerable collection, like T-Sql top()
I needed to bind the first 5 items in a List<T> to a repeater. Having recently discovered the .Distinct() extension, I had a look through the other methods attached to my List<T> and found .Take(int)
|
 |
Returning a Distinct list of values from an array or IEnumerable cllection
Paul Hayman (415 views)
Returning a Distinct list of values from an array or IEnumerable collection
I recently had a list of Guids in a string which was returned from a group of checkboxes with identical names. I wanted to itterate through Guids and process each one only once. Basicaly, ignoring duplicates.
I didn'
|
 |
Converting a String to a ByteArray in c#
Paul Hayman (810 views)
Converting a String to a Byte Array in c#
Very simple.. here's how to convert a String to a Byte Array in c#
#c#byte[] yourByteArray = Encoding.ASCII.GetBytes("your string");
|
 |
Reading a key from the registry in c#
Paul Hayman (661 views)
Reading a key from the registry in c#
The following c# code shows how to read a key value from the registry.
#c#Registry.LocalMachine.OpenSubKey("SOFTWARE", true);
#c#RegistryKey masterKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\yourapp\\yourkey");
#c#string value = "";
#c#if (mas
|
 |
Specifying an originator IP Address on a WebService Request using ServicePoint and HttpWebRequest
Paul Hayman (939 views)
Specifying an originator IP Address on a WebService Request using ServicePoint and HttpWebRequest
If you're running multiple IP Addresses on the same NIC in Windows 2003 server and you need a webservice call to originate from a certain one, you'll probably have found the same thing I did. .net
|
 |
Regex match html content without screwing up the tags
Paul Hayman (1658 views)
Regex match html content without screwing up the tags
When needing to highlight words in a string containing HTML we found we soon ran into problems when the word we were searching for appeared in the middle of a tag..
Imagine the example:
#v#<a href="geekzilla.aspx">you searched for geek
|
 |
Excellent IIS Metabase Helper
Paul Hayman (441 views)
Excellent IIS Metabase Helper
Found this : http://agramont.net/blogs/provware/default.aspx
http://agramont.net/blogs/provware/archive/2006/07/31/Provware_3A00_-Getting-Started-_2800_Build-001_2900_.aspx
Has the potential to be an excellent Metabase wrapper.
|
 |
Getting the Virtual Path of a Request in c#
Paul Hayman (2022 views)
Getting the Virtual Path of a Request in c#
This simple bit of code will get you the '''Virtual Path''' of your current request.
#c#public static string GetVirtualPath(string url)
#c#{
#c# if (HttpContext.Current.Request.ApplicationPath == "/")
#c# {
#c# return "~" + url;
#
|
 |
Sorting an XML document in C# using XSL
Paul Hayman (4198 views)
Sorting an XML document in C# using XSL
I needed to sort some XML in C# before itterating through the document. In the end I used the XslCompiledTransform to apply an XSL stylesheet to the XML document. Works very quickly. For reference, this is how I did it.
The code
The following uses
|
 |
Convert to and from Hex
Paul Hayman (1145 views)
Convert to and from Hex
Convert from an int to Hex
#c#String result = String.Format("{0:x2}", 255)
Convert from Hex to an int
#c#int result = int.Parse("FF", System.Globalization.NumberStyles.HexNumber);
|
 |
"This row already belongs to another table" Error
Paul Marshall (34605 views)
"This row already belongs to another table" Error
#$#<a href="http://www.dotnetkicks.com/kick/?url=http://www.geekzilla.co.uk/View37EB5230-5B79-4D00-800C-52D7A46CFB15.htm"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.geekzilla.co.uk/View37EB5230-
|
 |
Why we use C# for .NET development
Dave Howard (10900 views)
Why we use C# for .NET development
#$#<a href="http://www.dotnetkicks.com/kick/?url=http://www.geekzilla.co.uk/View026313AC-108F-4EA1-90A2-5E5BE0DEE0F2.htm"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.geekzilla.co.uk/View026313AC-108F-4EA1-90A2-5E
|
 |
Bug in VS2005? Using Typed DataSets with SQL 2005 and stored procedures
Dave Howard (6664 views)
Bug in VS2005? Using Typed DataSets with SQL 2005 and stored procedures
#$#<a href="http://www.dotnetkicks.com/kick/?url=http://www.geekzilla.co.uk/View72C81AA0-44A5-4E2C-8903-B1EA7D7643EA.htm"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.geekzilla
|
 |
Authenticated HTTPRequests (Using Credentials)
Dave Howard (15387 views)
Authenticated HTTPRequests (Using Credentials)
#$#<a href="http://www.dotnetkicks.com/kick/?url=http://www.geekzilla.co.uk/ViewC5E6201A-DFC1-4091-8145-5A893504C0E4.htm"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.geekzilla.co.uk/ViewC5E6201A-DFC1-
|
 |
Web service calls from javascript using ATLAS (part 2) - Complex return types
Dave Howard (4638 views)
Web service calls from javascript using ATLAS (part 2) - Complex return types
#$#<a href="http://www.dotnetkicks.com/kick/?url=http://www.geekzilla.co.uk/ViewF266ADEB-8F62-4E92-8036-1D2436C2DE3A.htm"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.g
|
 |
DateTime.ToString() Patterns
Paul Hayman (72204 views)
DateTime.ToString() Patterns
All the patterns:
||0|| MM/dd/yyyy|| 08/22/2006 ||
||1|| dddd, dd MMMM yyyy|| Tuesday, 22 August 2006 ||
||2|| dddd, dd MMMM yyyy|| HH:mm Tuesday, 22 August 2006 06:30 ||
||3|| dddd, dd MMMM yyyy|| hh:mm tt Tuesday, 22 August 2006 06:30 AM ||
||4|| dddd, dd
|
 |
Redirect a page using a Header
Paul Marshall (9467 views)
Redirect a page using a Header
Any keen surfer will be familiar with '''Affiliate Marketing''', its a huge industry with '''Google's Adwords''' driving "Pay Per Click" campaigns. '''Google''' provide the '''Adwords''' application and '''Google Analytics''' to track the effectiveness of marketi
|
 |
The null coalescing operator: ??
Paul Hayman (1972 views)
The null coalescing operator: ??
This is a new feature of c# 2.0. The null coalescing operator is a short cut for checking if a value is null and if so returning the value of the second operand. Kind of like an IIF. The syntax is as follows:
#c#string newValue = someValue ?? "default";
|
 |
Sending mail using Localhost and .Net 2
Greg Duffield (1370 views)
Sending mail using Localhost and .Net 2
I came across an old favourite today when trying to send email using the SMTP service on Local host. The error was
SMTP 550 5.7.1 Unable to relay for xxxxxx
What you need to do is change the security settings on the SMTP Service to allow your to r
|
 |
Restricting the number of rows in a dataview when binding to a repeater
Paul Hayman (5061 views)
Restricting the number of rows in a dataview when binding to a repeater
The problem
I needed to restrict the number of items bound to an '''asp:repeater'''. Basically I never wanted more than 10 items to be displayed.
Options
The right thing to do would be to restrict the number of it
|
 |
Code snippet for 'protected void MethodName(object sender, EventArgs e)' method
Paul Hayman (1056 views)
Code snippet for 'protected void MethodName(object sender, EventArgs e)' method
When adding a method to handle server events (such as OnClick) I often found myself copying the Page_Load method and changing the name. I looked for a snippet which would create this method for me but had no luck, s
|
 |
Using Regular Expressions to validate a filename in a FileUpload control
Paul Hayman (16168 views)
Using Regular Expressions to validate a filename in a FileUpload control
Here's a little code snippet I use to ensure that the file that has been uploaded is of type JPG, JPEG, PNG or GIF
#c#// check anything has been uploaded
#c#if (ThumbnailImageUpload.PostedFile.ContentLength > 0)
#c#{
|
 |
Generating a resized image with maximum image quality
Paul Hayman (1385 views)
Generating a resized image with maximum image quality
Here's some code I use for generating a resized image with no Jpeg compression.
#c#public MemoryStream GetResized(Bitmap originalImage, int width, int height)
#c#{
#c# System.Drawing.Image outputImage;
#c# MemoryStream outputStre
|
 |
Return new identity from Strongly Typed Dataset DataTable.Insert method
Dave Howard (13374 views)
Return new identity from Strongly Typed Dataset DataTable.Insert method
#$#<a href="http://www.dotnetkicks.com/kick/?url=http://www.geekzilla.co.uk/ViewF2737CEE-4FCE-4215-B8CA-74D77EF64A5F.htm"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.geekzil
|
 |
Getting on the Vista developers merry-go-round
Mark Page (1454 views)
Getting on the Vista developers merry-go-round
Trying to keep up with the various Vista Beta releases? Want to develop but not sure which version will work on your platform?
Here's a useful set of links to make sure you get the right version of the runtime, SDK, Orcas VS extensions and WCF l
|
 |
Need a high resolution timer?
Lee Keable (1196 views)
Need a high resolution timer
I like to profile my code regularly to see if any changes I make during development affect performance. In .Net 1.1 there is a distinct lack of a high resolution timer object. Here's one you can use:
#c#using System;
#c#using System.Diagnostics;
#c#using Syst
|
 |
Missing App.Path
Lee Keable (2182 views)
Missing App.Path
If you have the need to load resources (XSL stylesheets, images etc etc) at runtime, you may need to know the path of the currently executing code. In VB6 you'd just use good old App.Path, here is the .Net equivalent.
#c# using System.IO;
#c#
#c# string Location = Path.Ge
|
 |
Extending Strongly Typed Datasets
Dave Howard (3164 views)
Extending Strongly Typed Datasets
Strongly Typed Datasets are a rather nice addition to VS2005 (they don't lend themselves well to acronyms however)
You still need to create a database and tables and realationships but using STDs saves a writing huge amount of code.
If this code were gene
|
 |
Highlighting keywords in text using Regex.Replace (Perfect for SEO)
Paul Hayman (8473 views)
Highlighting keywords in text using Regex.Replace (Perfect for SEO)
Why
I needed to take some text and bold certain keywords before returning the data to the web browser to enhance my {Search Engine Optimization}http://www.kwiboo.com/SearchEngineOptimisation.aspx
Example
The followi
|
 |
Triple DES encryption wrapper
Paul Hayman (6041 views)
Triple DES encryption wrapper
Here is a handy wrapper for Triple DES encryption:
#c#using System;
#c#using System.Collections.Generic;
#c#using System.Text;
#c#using System.Security.Cryptography;
#c#using System.IO;
#c#
#c#namespace Security
#c#{
#c# /// <summary>
#c# /// Wrap
|
 |
Generic Lists are All You Will Ever Need
Greg Duffield (836 views)
Generic Lists are All You Will Ever Need
Generic lists have reduced the amount of code we have to write for our n-tier applications dramatically. Where they have been most uselful is in the definition of our Strongly Typed Object Collections.
Previously we would have created a class that inh
|
 |
Convert a DataReader to a DataTable
Greg Duffield (2905 views)
Convert a DataReader to a DataTable
When working with Datareaders one of the problems i sometimes come across is binding these to 3rd party controls. A lot of controls will not bind to a DataReader but nearly all of the alwaays bind to a DataTable. All of the functionlity already exists in the
|
 |
EasyHTTP
Barrington Haynes (2697 views)
EasyHTTP
EasyHTTP is a simple class which exposes the HTTP functionality of .NET as a number of simpler methods.
#c#using System;
#c#using System.Data;
#c#using System.Configuration;
#c#using System.Web;
#c#using System.Web.Security;
#c#using System.Web.UI;
#c#using System.Web.UI.WebCo
|
 |
CLR Stored Procedure for searching files
Paul Hayman (6342 views)
CLR Stored Procedure for searching files
I was recently asked to write a CLR stored procedure which would process a text file and return a row for each line in the file that contained text matching our search criteria.
I'd never written a CLR stored proc before so it was an interesting learn
|
 |
Writing CLR Stored Procedures in C#
Paul Hayman (10584 views)
Writing CLR Stored Procedures in C#
As SQL Server 2005 rolls out DBA's are going to be forced to learn either C# or Visual Basic or both. Until now these were client side languages and not knowing them had little impact on your job. And if you write code in these languages your going to have to
|
 |
IsGuid() (Regular Expression Guid Match)
Paul Hayman (9939 views)
IsGuid() (Regular Expression Guid Match)
A regular expression for validating a string as being a Guid is..
#c#@"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$"
#c#
Example usage
Below is a function I try to keep handy which tests
|
 |
class.Serialize()
Paul Hayman (3017 views)
class.Serialize()
Handly little function which I include in most classes. This function returns the object serialized as XML, perfect for logging etc.
#c#/// <summary>
#c#/// Serialize this object
#c#/// </summary>
#c#/// <returns>XmlDocument containing the current information serialized<
|
 |
Referencing ConfigurationManager
Barrington Haynes (3104 views)
Referencing ConfigurationManager
When I started working in VS2005, I assumed that accessing your AppSettings would be the same as it was in VS2003. On compiling, the IDE served up the following warning.
'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is ob
|
 |
Select a row in an asp:GridView without using a Select Command
Paul Marshall (77592 views)
Select a row in an asp:GridView without using a Select Command
ASP.Net's GridViews can be quite useful, but beware of binding them to huge datasets as this has an overhead on the ViewState.
Often you'll want to display a number of columns on each line and row space becomes an issue. What's
|
 |
Assigning an array of strings to a variable
Paul Hayman (1860 views)
Assigning an array of strings to a variable
Declaring a string array with hard coded data is easy.
#c#string[] newString = new string[] {"one", "two", "three"}
Passing hard coded array into method
Likewise, passing an array of hard coded data into a method is also easy..
For example
|
 |
Evaluating Expressions
Paul Hayman (2433 views)
Evaluating Expressions
You can evaluate sums in the fly using .net DataTables.
This is how you do it (C# 2005).
#c#string sum = "(1+2)/4";
#c#System.Data.DataTable evaluator = new System.Data.DataTable("temp");
#c#double result;
#c#
#c#result = (double)evaluator.Compute(sum, null);
|