Tag Archives: net

Surface Pro 3 for General and SharePoint Development

I bought my Surface Pro 3 in April 2015 as my dedicated development computer for .NET and SharePoint development. The Surface Pro 3 has 8GB of RAM, 512GB of storage and i7 CPU. Compared to my previous desktop setup, I had 16GB of RAM, 2TB of HDD Storage, and AMD Quad Core CPU.

Surface-Pro-3 Continue reading

C#: Send an email using Google’s SMTP server.

I want to demonstrate how to write a simple console application that will use Google’s SMTP server to send an email. SMTP stands for Simple Mail Transfer Protocol which is the standard for delivering electronic mails over the Internet.

Before continuing you will need to have an email account with Google first, and if you dont please do it first. (Signup for GMail Account)

Startup Visual Studio and File->New->Project and select Visual C# Console Application Template, enter GoogleSmtpApp as the project name and continue.

In our code I am going to use the System.Net.Mail.SmtpClient class to send an email to a recipient. So in your Main method in your program.cs file create a new instant of the SmtpClient() class, to use this class you also need to enter smtp.gmail.com and port 587 in the class constructor.

var smtpClient = new SmtpClient("smtp.gmail.com", 587);

Before we can send an email there is four properties that needs to be set. UseDefaultCredentials must be set to false this will prevent default credentials sent as part of the request. DeliveryMethod must be set to Network so that the message will be sent through the network to the smtp server. Credentials must contain your Gmail username and password so that it can authenticate when connecting to the smtp server. EableSsl must be set to true so that our connection can be encrypted.

var smptClient = new SmtpClient("smtp.gmail.com", 587)
     UseDefaultCredentials = false,
     DeliveryMethod = SmtpDeliveryMethod.Network,
     Credentials = new NetworkCredential("username@gmail.com", "password"),
     EnableSsl = true

Finally from the SmtpClient class we can call the method Send(). The Send method has two overloaded method but I will be using the one where I will provided the from, recipient, subject and body as strings. Make sure you replace the appropriate parameters with your own values.

smptClient.Send("from@email.com", "recipient@email.com", "subject", "body");

If you press F5 now to run your console application and wait and check your inbox to see if you have received an email! Anyways I hope this simple demonstration can show you how easily we can use Google’s smtp server to send a basic text email.

You can get the latest source code for this project from my GitHub account here:

GitHub: Google-Smtp-App

Inno Setup: Check Windows Registry for MS .NET Framework Version

I just recently installed Inno Setup on my computer, I use to use it to package my software setup together for distribution. Inno Setup is so well designed, it has a Wizard to prepare your setup and if you want to add something tricky to your setup. You can also write code scripts to do certain things and best of all it’s free!

The language that is used for scripting is Pascal, this was one of the first language you learn when learning how to program.

Continue reading

C#: How to get your computer and user name.

If you want to get your computer and user name from Windows in C# try this by using the WindowsIdentity class from System.Security.Principal namespace:

<span style="color: blue">using </span>System;
<span style="color: blue">using </span>System.Collections.Generic;
<span style="color: blue">using </span>System.Linq;
<span style="color: blue">using </span>System.Text;
<span style="color: blue">using </span>System.Security.Principal;

<span style="color: blue">namespace </span>ConsoleApplication1
    <span style="color: blue">class </span><span style="color: #2b91af">Program
        <span style="color: blue">static void </span>Main(<span style="color: blue">string</span>[] args)
            System.<span style="color: #2b91af">Console</span>.Out.WriteLine(<span style="color: #2b91af">WindowsIdentity</span>.GetCurrent().Name);

Sorting GridView using IComparer and Reflection

Recently I just did a small job for a client online using IComparer interface to perform sorting on a GridView control.

First I need to create class called Person. This class is going to contain the following properties: FirstName, LastName, Age.

public class Person
    private string _firstName;
    private string _lastName;
    private int _age;

    public string FirstName
        get { return _firstName; }
        set { _firstName = value; }

    public string LastName
       get { return _lastName; }
       set { _lastName = value; }

    public int Age
       get { return _age; }
       set { _age = value; }

Now I need to create a class called PersonComparer that will inherit from the IComparer interface. This comparer class will be use with the Sort() method from a List collection.

 using System.Collections;
 using System.Collections.Generic;
 using System.Reflection;
 using System;

 public class PersonComparer:IComparer
     private string _sortBy;

     public string SortBy
         get { return _sortBy; }
         set { _sortBy = value; }

     public int Compare(Person x, Person y)
         int result = 0;
         string propName = string.Empty;
         string sortDirection = "ASC";

         if (_sortBy.Contains(" DESC"))
             propName = _sortBy.Replace(" DESC", string.Empty).Trim();
             sortDirection = "DESC";

         if (_sortBy.Contains(" ASC"))
             propName = _sortBy.Replace(" ASC", string.Empty).Trim();
             sortDirection = "ASC";

         // Get type of Person Object
         Type t = typeof(Person);

         // Find the property name from the sortExpress passed in SortBy property.
         PropertyInfo propInfo = t.GetProperty(propName);

         if (propInfo != null)
             // Perform comparison on property value.
             result = Comparer.DefaultInvariant.Compare(propInfo.GetValue(x, null), propInfo.GetValue(y, null));

             if (sortDirection.Equals("DESC"))
                 result = -result;
         return result;

Continue reading

ASP.NET: Cache or Session

What the difference between Cache and Session variables. Cache variables are available at the application level which means that another web user can access the same cache. Session variables are available at the client level which only the client can read or write to the Session variable.

When developing a web application in ASP.NET it’s good practice to use Cache to store data that will be shared across the application. For example if you need to read from cache a list of colour names from a table from a database, you can reuse this cache when ever your page loads. This way it prevents repetitive reads from the table which can reduce web application performance.

List<string> colourList = new List<string>();

Cache["ColourList"] = colourList;

colourList = Cache["ColourList"] as List<string>;

Session variables are used for storing user specific data such as user’s details, below is an example of how it’s used.

Session["Username"] = "joe.bloggs";

string username = Session["Username"].ToString();

Make sure you understand the difference between them two and which one to use for your purpose, never store user’s details in a Cache variable because another web user can access them but then this means that the web was poorly designed.

For further reading on Cache and Session variables I came across some web sites that may be resourceful:

ASP.NET Session State Overview

ASP.NET Caching