Skip to content

Instantly share code, notes, and snippets.

//Get a file and save as blob to a db
//Upload from file .png
string lFileName = @"C:\Inetpub\wwwroot\welcome.png";
Image lOriginalImage = Image.FromFile(lFileName);
//ImageFormatConverter lConv = new ImageFormatConverter(); //Use to convert to different formats
MemoryStream lMs = new MemoryStream();
lOriginalImage.Save(lMs, ImageFormat.Png);
Byte[] lBuffer = lMs.GetBuffer();
lMs.Close();
SqlConnection lConn = new SqlConnection(fConn);
/// <summary>
/// Performs a deep compare of an object to another of the same type.
/// Each property will be compared only if it is primitive, string, or date.
/// Otherwise DeepCompare is recursviely called on the two values for the the properties.
/// Only public properties that are declared on the concrete type given are compared
/// (ie BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).
/// </summary>
/// <param name="thisValue">the instance of the object that DeepCompare was invoked on</param>
/// <param name="compareTo">the object to compare <see cref="thisValue"/> with</param>
/// <returns>true if they are deemed to be equal, otherwise false.</returns>
///<summary>Deserialise an xml file into a type instance with event
///validation using lambda expressions.
///</summary>
public static void ReadFromTestDataXmlFile() {
// Read serialised metadata describing the service and methods to call
var serializer = new XmlSerializer(typeof(TestJobList));
var stream = new FileStream("TestData.xml", FileMode.Open);
var reader = XmlReader.Create(stream);
var events = new XmlDeserializationEvents() {
OnUnknownAttribute = (o, e) => Logger.Log("Unknown attribute found {0}, line {1} at char {2}",
/// <summary>
/// An extension class for XElement. Compare to XElements for equality.
/// </summary>
public static class XElementExtension {
/// <summary>
/// Compares two elements and their descendants contain the same elements and those elements have the same value. Useful for verifying deserialisation. xml attributes are ignored.
/// Order of siblings is also ignored.
/// </summary>
/// <param name="thisValue">The instance of XElement</param>
/// <param name="compareTo">the XElement to compare</param>
///<summary>Generic method sample signature with restrictions</summary>
private static T GetTestDataInstance<T>() where T : class, new() {
var root = GetTestDataInstance(typeof(T));
return root as T;
}
///<summary>Upload a file using the Asp.Net 2.0 Upload control.
///Requires a button to which this handler responds to Onclick.
///</summary>
protected void UploadBtn_Click(object sender, EventArgs e) {
try {
if (FileUpload1.HasFile) {
bool failure = false;
if (!FileUpload1.FileName.ToLowerInvariant().EndsWith(".xml")) {
failure = true;
App.Config
<system.diagnostics>
<switches>
<add name="TraceLoggerOn" value="0" />
</switches>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="D:\Logs\TraceLogger.log" traceOutputOptions="DateTime, ThreadId" >
<filter type="System.Diagnostics.EventTypeFilter" initializeData="Information" />
</add>