Image to byte array ... Reveal Code
public static byte[] imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
Gets the first day of the month from the given date.
Gets the last day of month from the given date. ... Reveal Code
public static class MonthExtensions
{
///
/// Gets the first day of the month.
///
/// The given date.
/// the first day of the month
public static DateTime GetFirstDayOfMonth(DateTime givenDate)
{
return new DateTime(givenDate.Year, givenDate.Month, 1);
}
///
/// Gets the last day of month.
///
/// The given date.
/// the last day of the month
public static DateTime GetTheLastDayOfMonth(DateTime givenDate)
{
return GetFirstDayOfMonth(givenDate).AddMonths(1).Subtract(new TimeSpan(1, 0, 0, 0, 0));
}
}
DeSerialize object to xml file ... Reveal Code
private static void DeSerialize()
{
try
{
var testObject = new TestClass();
var serializer = new XmlSerializer(testObject.GetType());
var reader = new StreamReader(Filename);
var deserialized = serializer.Deserialize(reader.BaseStream);
testObject = (TestClass)deserialized;
reader.Dispose();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
Update record in database using entity framework ... Reveal Code
try
{
using (var context = new myEntities())
{
var document = context.ORDERS.Single(parameter => parameter.ID_ORDER == idOrder);
document.ORDER_STATUS = status;
context.SaveChanges();
}
}
catch (Exception ex)
{
Logger.TraceError(ex.Message);
}
C#: Calculate Age
Calculate Age ... Reveal Code
public static int GetAge(DateTime birthday)
{
DateTime now = DateTime.Today;
int age = now.Year - birthday.Year;
if (now < birthday.AddYears(age)) age--;
return age;
}
SQL: Select Duplicates
Select Duplicates ... Reveal Code
select field1,field2,field3, count(*)
from table_name
group by field1,field2,field3
having count(*) > 1
SQL: Delete N rows from table
Delete N rows from table ... Reveal Code
DELETE FROM [tbCode] WHERE [CodeID] IN (SELECT TOP N [CodeID] FROM [tbCode] order by [CodeID] desc)
If you want to create a screenshot out of your application, this is the perfect snippet suitable to your needs. ... Reveal Code
using System;
using System.Drawing;
using System.Windows.Forms;
///
/// Saves an image of the screen to the specified path.
///
///
/// Path, where output file will be saved at.
/// Path of the successfully saved image or errormessage
public string ScreenToPicture(string Location)
{
try
{
Size currentScreenSize = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Bitmap ScreenToBitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
System.Drawing.Graphics gGraphics = System.Drawing.Graphics.FromImage(ScreenToBitmap);
gGraphics.CopyFromScreen(new Point(0, 0), new Point(0, 0), currentScreenSize);
ScreenToBitmap.Save(Location);
return Location;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
C#: Serialize object to xml file
Serialize object to xml file ... Reveal Code
private static void Serialize(TestClass testObject)
{
try
{
var serializer = new XmlSerializer(testObject.GetType());
var writer = new StreamWriter(Filename);
serializer.Serialize(writer.BaseStream, testObject);
writer.Close();
}
catch (Exception)
{
throw new Exception();
}
}
Enterprise Library Database Extensions ... Reveal Code
public static class DatabaseExtensions
{
public static void WithConnection(this MSDB.Database thisObj, Action action)
{
using (var connection = thisObj.CreateConnection())
{
connection.Open();
action(connection);
}
}
public static T WithConnection(this MSDB.Database db, Func action)
{
using (var conn = db.CreateConnection())
{
conn.Open();
return action(conn);
}
}
public static void WithinTransaction(this MSDB.Database thisObj, Action action)
{
thisObj.WithConnection(conn => conn.WithinTransaction(action));
}
public static T WithinTransaction(this MSDB.Database db, Func action)
{
return db.WithConnection(
conn =>
{
using (var transaction = conn.BeginTransaction())
{
var result = action(transaction);
transaction.Commit();
return result;
}
});
}
public static void WithTextCommand(this MSDB.Database db, string sql, Action action)
{
db.WithConnection(conn =>
{
using (var cmd = conn.CreateTextCommand(sql))
{
action(cmd);
}
});
}
public static T WithTextCommand(this MSDB.Database db, string sql, Func action)
{
return db.WithConnection(conn =>
{
using (var cmd = conn.CreateTextCommand(sql))
{
return action(cmd);
}
});
}
public static T ExecuteScalar(this MSDB.Database db, string sql, Action action)
{
using (var conn = db.CreateConnection())
{
conn.Open();
return conn.ExecuteScalar(sql, action);
}
}
public static T ExecuteScalar(this MSDB.Database db, string sql)
{
return db.ExecuteScalar(sql, cmd => { });
}
public static T ExecuteScalar(this MSDB.Database db, Action action)
{
return db.ExecuteScalar(string.Empty, action);
}
public static int ExecuteNonQuery(this MSDB.Database db, string sql, Action action)
{
using (var cmd = db.GetSqlStringCommand(sql))
{
action(cmd);
return db.ExecuteNonQuery(cmd);
}
}
public static int ExecuteNonQuery(this MSDB.Database db, Action action)
{
return db.ExecuteNonQuery(string.Empty, action);
}
}