2014/08/31

Serialize / deserialize to xml extension methods in C#

public static T Deserialize<T>(this T obj, string xmlFilePath)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
StreamReader reader = new StreamReader(xmlFilePath);
obj = (T)serializer.Deserialize(reader);
reader.Close();
return obj;
}
public static void Serialize<T>(this T obj, string filePath)
{
XmlSerializer writer = new XmlSerializer(typeof(T));
StreamWriter file = new StreamWriter(filePath);
writer.Serialize(file, obj);
file.Close();
}
view raw gistfile1.cs hosted with ❤ by GitHub

2014/08/28

How to convert string to enum in C# ?

order.Status = (OrderStatus) Enum.Parse(typeof(OrderStatus), "paid");
view raw gistfile1.cs hosted with ❤ by GitHub

2014/08/26

How to create an element with attributes and a value but no sub-element using XmlSerializer ?

http://stackoverflow.com/questions/3524224/using-xmlserializer-to-create-an-element-with-attributes-and-a-value-but-no-sub

2014/08/24

Sending email using gmail smtp from Azure Cloud Service

Error:
System.Net.Mail.SmtpException: The SMTP server requires a secure connection
or the client was not authenticated. The server response was:
5.5.1 Authentication Required. Learn more at
view raw gistfile1.cs hosted with ❤ by GitHub
Add to web.config:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network defaultCredentials="false" enableSsl="true" host="smtp.gmail.com" port="587" userName="xxxxxxx@gmail.com" password="xxxxxxxxxxx"/>
</smtp>
</mailSettings>
</system.net>
view raw gistfile1.cs hosted with ❤ by GitHub
http://stackoverflow.com/questions/11018233/cannot-use-gmail-smtp-from-azure-cloud-service

How to initialize Dictionary with default values in C# ?

public static Dictionary<int, string> GetProductsResponseErrors = new Dictionary<int,string>()
{
{ 0, "Operation success" },
{ 1, "Operation failed: invalid login or password." },
{ 2, "Operation failed: access denied." },
// ..
};
view raw gistfile1.cs hosted with ❤ by GitHub

2014/08/22

Visual Studio 2013 - 'Operation Could Not Be Completed' when trying to open any View ?

I had two <appSeettings> nodes in config file.
Make only one node.
Maybe you will need restart of VS or system.

How to backup SqlServer database ?

1/ Backup file: Right click on Database -> Task -> Backup ...
2/ Script with structure and data: Right click on Database-> Task -> Generate scripts -> On 3rd step of wizard choose 'Advanced' button and select 'Types of data to script':'Schema and data'.


How to bind MVC model to different table ?

public class ApplicationDBContext : DbContext
{
public DbSet<ProductsModel> Products { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ProductsModel>().ToTable("Products");
// otherwise EF assumes the table is called "ProductsModel"
}
}
view raw gistfile1.cs hosted with ❤ by GitHub

Fatal error: Class 'SoapClient' not found in ..

1/ Open php.ini.
2/ Uncoment line:
from: ;extension=php_soap.dll
to: extension=php_soap.dll
3/ Restart php.

How to read config section from configuration file in C# ?

App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="GateInvocationMax" value="10"/>
</appSettings>
</configuration>
view raw gistfile1.cs hosted with ❤ by GitHub
Class that reads config file.
Remember to add System.Configuration to Project references !
using System.Configuration;
namespace Interlook.Common
{
public class ConfigManager
{
public static int GateInvocationMax
{
get
{
int gateInvocationMax = 10;
int.TryParse(ConfigurationManager.AppSettings["GateInvocationMax"], out gateInvocationMax);
return gateInvocationMax;
}
}
}
}
view raw gistfile1.cs hosted with ❤ by GitHub

2014/08/20

Lambda expression, return new elements of ToList() method

IEnumerable<UserModel> users = db.Users.Select(it => new UserModel() {
Email = it.Email,
ForeName = it.ForeName,
Id = it.Id,
ModificationDate = it.ModificationDate,
//..
}).ToList();
view raw gistfile1.cs hosted with ❤ by GitHub

2014/08/19

2014/08/18

How to generate random password, random string in C# ?

public static string GenaratePassword(int size = 8)
{
string allowedChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
StringBuilder builder = new StringBuilder(size);
Random rnd = new Random(Environment.TickCount);
for (int i = 0; i < size; ++i)
builder.Append(allowedChars[rnd.Next(allowedChars.Length)]);
return builder.ToString();
}
view raw gistfile1.cs hosted with ❤ by GitHub

How to determine mssql version ?

select @@VERSION

2014/08/15

How to add / change search engine to IE11 ?

Click magnifier on left side of the url box. Choose appropriate option.

2014/08/13

Sql server connection strings

http://www.connectionstrings.com/sql-server-2012/

Spf record example


Record SPF (Sender Policy Framework) allows you to check the host SMTP mail servers, the server sends e-mails from a given domain is authorized to do. It is in fact plain text record. To ensure compatibility with older implementations of servers / clients DNS domain should be 2 SPF records that contain exactly the same value, one type of SPF, and the second TXT.
 
Suppose, for example, that your domain example.com uses Gmail. You create an SPF record that identifies the mail servers as authorized Google Apps mail servers for that domain. When the recipient's mail server receives a message sent from the account user @ example.com, you may check the SPF record for the domain example.com, to determine if the message comes from an authorized mail server. If the message comes from a server other than the Google Apps mail servers listed in the SPF record, the recipient's mail server can reject it as spam.
If your domain has no SPF record, messages from users from that domain can be rejected by some of the recipients domain because it is not possible to check whether the message came from an authorized mail server.

Qualifiers


The qualifier is a single sign placed before the definition of the mechanism:

Qualifier Description
"+" - PASS indicates that the data source is authorized to send mail from the domain. If prior to the determination of the mechanism does not have a qualifier, it is assumed that it is "+".
"?" - NEUTRAL This source will be treated as if it was not him at all ...
"~" - SOFTFAIL Maile from this source will be rejected or accepted, but marked as SPAM.
"-" - FAIL Messages from this source will always be rejected.

Mechanisms

Immediately after the qualifier is the "mechanism" that is, to identify the source:

Mechanizm Description
ip4: 192,168,137,101 IPv4 Address - 192,168,137,101
ip6: 2001: db8 :: 1428: 57ab Ipv6 Address - an Ipv6: 2001: db8 :: 1428: 57ab
a Each record for a domain
mx Each SMTP server accepting mail for this domain
all Each host on the Internet

Examples

a/
example.com. IN TXT "v = spf1 mx -all"
Each SMTP server is accepting mail for the domain przykladowa.local. may also be sent. Mail coming from other hosts across the Internet will be rejected.

b/
example.com. IN TXT "v = spf1 a -all"
Mail originating from any host in the domain will be accepted. On the other rejected.

c/
example.com. IN TXT "v = spf1 ip4:192,168,137,101 -all"
Incoming IP 192,168,168,137,101 will be accepted. Other messages rejected.
d/
example.com. IN TXT "v = spf1 a mx ip4:192,168,137,202 -all"
Incoming mail from any host in the domain, the SMTP server receiving the mail and IP 192,168,137,102 will be accepted. The remainder will be rejected.
e/
example.com. IN TXT "v = spf1 mx -ip4:192,168,137,101 -all"
Incoming mail from any SMTP server receiving the mail, bypassing the host IP 192,168,137,101 will be accepted. The remainder will be rejected.

Notes


Full syntax: http://www.openspf.org/SPF_Record_Syntax
Validator: http://www.kitterman.com/spf/validate.html
Generator: http://www.spfwizard.net/
How to add SPF in your domain: http://kb.mediatemple.net/questions/658/How+can+I+create+an+SPF+record+for+my+domain%3F#gs

2014/08/11

Simple url construction example

http(s)://www.domain.net/site.php?arg1=val1&arg2=val2#label1

http(s) - protocol
www.domain.net - server address
site.php - document name (optional)
arg1,arg2 - url arguments passed to site (optional)
label1 - anchor, it points where to jump on site (optional)

How to check your google search history ?


If you have google account and are logged in, google stores your search results in history.
Archive stores results even from some years ago.
To check your history visit:
https://history.google.com/history/
or where you were:
https://maps.google.com/locationhistory                  



2014/08/09

How to add and remove programs from startup windows 8 ?

Disable / enable program on startup Wndows 8

(right click) Windows Toolbar -> (select) Task Manager -> (select) Startup -> (right click) application -> (select) disable / enable




Add programs to startup Windows 8

From Run box run: 
%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup

or type in Windows explorer:
C:\Users\(your domain name)\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

or look here.

2014/08/04

How to force file update in TFS ?



1. Find file on drive and replace it with a new version


 2. Open Team Explorer -> Source Control and find it in archive
 3. Click right on file and select "Check out for edit"

4. Confirm changes as shown..

5. Now TFS knowsthat file needs to be updated in repository

6. Check In pending changes