Trick-or-Treat Welcome Sign

October 22nd, 2010 by

Here is a handy sign, you can put up on your door, wall or wherever you like, to let Trick-or-Treaters know you’re willing to go along with the whole Halloween vibe.

Click on the URL below, download the pdf and print it…simple.
Click Here To Download The Halloween Welcome Sign

Retrieve e-mail from Active Directory using C#

July 30th, 2009 by

There comes a time in every developer’s life where they need to retrieve something out of Active Directory to use within their project. In my case it’s an Intranet I am building for the company I work for.
Retrieving information from Active Directory isn’t difficult it’s just difficult to find a decent resource that explains exactly what to do to get that information.
I’ve decided to share a couple of handy methods that I use to retrieve info from AD all the time.
This is going to be a multi-part post so I can split out the different methods that I have created.
The first one I’m going to discuss is retrieving e-mail from AD, we use AD extensively to store user data for the Intranet, we store everything from Extension number and Employee numbers to personal mobile numbers and speed dials, so we need an easy way to get this data out as quickly as possible in code.
I have a class that just handles AD queries, the email method I’m going to show you how to retrieve the email of the person that is currently logged in to the intranet.
I’ll explain the code below:

public string getEmail()
{
string login = HttpContext.Current.User.Identity.Name;
string domain = login.Substring(0, login.IndexOf('\'));
string userName = login.Substring(login.IndexOf('
\') + 1);
DirectoryEntry domainEntry = new DirectoryEntry("LDAP://" + domain,"username", "password”);
DirectorySearcher searcher = new DirectorySearcher(domainEntry);
searcher.Filter = string.Format(
“(&(objectCategory=person)(objectClass=user)(sAMAccountName={0}))”,
userName);
SearchResult searchResult = searcher.FindOne();
DirectoryEntry entry = searchResult.GetDirectoryEntry();
string email = entry.Properties[“mail”].Value.ToString();
return email;
}

• First three lines are pretty self explanatory, we are getting the full name of the currently logged in user, lines two and three basically split that full name down into the domain and username.
• Line line 4 we are creating a “connection string” to AD
• We then create a directory searcher to enable us to parse filters to it to search AD for what we want and where it is in AD.
• You will notice that we parse the username through in the filter to restrict the searching to only the logged in user.
• We can use this exact method to pull anything out of Active Directory by just changing the property name to pull out of AD i.e.
o

string email = entry.Properties[“mail”].Value.ToString();

o

String username = entry.Properties["sAMAccountName"].Value.ToString();

o

Guid objectGuid = new Guid(entry.NativeGuid);

In part 2 I’ll be showing you how to pull all the groups a person belongs to out of AD.

Splitting Full Name

March 31st, 2009 by

Another code snippet I’m constantly using is this one that splits a full name into first name and last name.
It finds the first space in the string and everything before the space is the first name and everything after space is the last name.

Code:

select
substring(ltrim(FullName), 1, coalesce(nullif(patindex('% %', ltrim(FullName)) - 1,-1), len(FullName))) as FirstName,
ltrim(substring(ltrim(FullName), nullif(patindex('% %', ltrim(FullName)), 0), len(FullName))) as LastName

Date Formats Overview

March 25th, 2009 by

I’m constantly using this as a reference for the checking out the different date formats.
Hope its useful to other people.

Code:

select convert(char(20),getdate(),DateFormatNumber)
Sample Date Date Format Number
04/05/2000 101
2000.04.05 102
05/04/2000 103
05.04.2000 104
05-04-2000 105
05 Apr 2000 106
Apr 05, 2000 107
11:33:24 108
Apr 5 2000 11:33:24 109
04-05-2000 110
2000/04/05 111
20000405 112

Active Directory Linked Server

March 25th, 2009 by

This article will show you how to add an active directory linked server in SQL Server 2005.
An active directory linked server can come in handy when you need to query your domain users and all other related data.
I will post an article soon on some LDAP queries you can run with your new AD Linked Server.

You can add the linked server via a query:
Code:

EXEC master.dbo.sp_addlinkedserver
@server = N'ADSI',
@srvproduct=N'Active Directory Services',
@provider=N'ADsDSOObject',
@datasrc=N'DomainController.domain.com'

Alternatively, you can do this through the Enterprise Manager Linked Server GUI:

From Blog Pics

Note:
You may need to tweak the security settings by entering a username & password. This may or may not be necessary depending on your environment.

Get SQL Server Version Information

March 11th, 2009 by

I often have to find out what version of SQL Server 2005 I’m running on a whole bunch of servers.

This code snippet achieves this by using the SERVERPROPERTY function.
Hit the link to see other arguments to use for the function.

Code:

SELECT 'SQL Server '
+ CAST(SERVERPROPERTY('productversion') AS VARCHAR) + ' - '
+ CAST(SERVERPROPERTY('productlevel') AS VARCHAR) + ' ('
+ CAST(SERVERPROPERTY('edition') AS VARCHAR) + ')'

Release                                                      Sqlservr.exe
RTM                                                             2005.90.1399
SQL Server 2005 Service Pack 1           2005.90.2047
SQL Server 2005 Service Pack 2           2005.90.3042
SQL Server 2005 Service Pack 3           2005.90.4035

Alternatively, you can use:

SELECT @@VERSION

But this doesn’t give you as much useful information or flexibility as the SERVERPROPERTY function.

Beginner LINQ to SQL N-Tier Architecture Part 2

March 6th, 2009 by

In Part 1 I created a dbml file and I’ll be using this as a DAL in the following examples.

In Part 2 I will be creating the Business Objects.

When creating business objects I often find that your BO’s (DTO’s) would mostly match your db, but BO’s are a logical way of grouping your data and not specific to how it is stored in the db, in most cases tho, it will be the same.

Create a new class, I place these classes under the Business Objects folder just to keep things organised.
Use the prop code snippet within Visual Studio 2008

Product Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AdventureWorks.Business_Objects
{
    public class product
    {
        public int? productId { get; set; }
        public string name { get; set; }
        public string productNumber { get; set; }
        public bool makeFlag { get; set; }
        public bool finishedGoodsFlag { get; set; }
        public string? color { get; set; }
        public int safetyStockLevel { get; set; }
        public int reorderPoint { get; set; }
        public decimal standardCost { get; set; }
        public decimal listPrice { get; set; }
        public string? size { get; set; }
        public char? sizeUnitMeasureCode { get; set; }
        public char? weightUnitMeasureCode { get; set; }
        public decimal? weight { get; set; }
        public int daysToManufacture { get; set; }
        public char? productLine { get; set; }
        public char? _class { get; set; }
        public char? style { get; set; }
        public int? productSubcategory { get; set; }
        public int? productModelId { get; set; }
        public DateTime sellStartDate { get; set; }
        public DateTime? sellEndDate { get; set; }
        public DateTime? discontinuedDate { get; set; }
        public Guid rowGuid { get; set; }
        public DateTime modifiedDate { get; set; }
    }
}

ProductSubcategory Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AdventureWorks.Business_Objects
{
    public class productSubcategory
    {
        public int? productSubcategoryId { get; set; }
        public int? productCategorId { get; set; }
        public string name { get; set; }
        public Guid rowGuid { get; set; }
        public DateTime modifiedDate { get; set; }
    }
}

ProductCategory Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AdventureWorks.Business_Objects
{
    public class productCategory
    {
        public int? productCategoryId { get; set; }
        public string name { get; set; }
        public Guid rowGuid { get; set; }
        public DateTime modifiedDate { get; set; }
    }
}

As you can see I have included all the properties for each corresponding table, this isn’t necessary of course, only include properties that you require in your App.

Once your Business Objects have been created we can then move onto creating the Business Logic Layer that will hold all the data retrieval code.

In Part 3 I’ll be showing you how to create the BLL for Products.

Beginner LINQ to SQL N-Tier Architecture Part 1

March 6th, 2009 by

I have adopted the following N-Tier architecture using LINQ to SQL for all my ASP.net apps. I’m not saying this is the perfect way to do a N-Tier app but I have found that it works for me.

I’m a firm believer that the Linq to Sql Data Model is a perfect Data Access Layer to build the rest of our architecture off of.

In part 1 I’m going to concentrate on the creation and setup of the DAL.

I’ll be using the Adventure Works DB for all these samples.

Firstly I create a new Linq to Sql class item in a DAL folder

Next in the server explorer create a connection to the DB, in this case it’s the Adventure Works DB.

In these examples I’m going to use the Product, ProductSubcategory and ProductCategory tables, select these tables from the table list in Server Explorer and drag them to the Object Relational Designer. You should see something like this:

We are done with our DAL…As simple as that.

In Part 2 of this series I’ll be concentrating on creating the Business Objects (DTO’s).

Get First And Last Days Of Month

March 5th, 2009 by

This is a helpful SQL code snippet I use to get certain “dates in time”.
I use it when I need to import data which is based on date ranges.

Code:

DECLARE @mydate DATETIME
SELECT @mydate = getdate()
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@mydate)),@mydate),103) , 'Last Day of Previous Month'
UNION
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@mydate)-1),@mydate),103) AS Date_Value, 'First Day of Current Month' AS Date_Type
UNION
SELECT CONVERT(VARCHAR(25),@mydate,103) AS Date_Value, 'Today' AS Date_Type
UNION
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@mydate))),DATEADD(mm,1,@mydate)),103) , 'Last Day of Current Month'
UNION
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@mydate))-1),DATEADD(mm,1,@mydate)),103) , 'First Day of Next Month'
GO

Select / Delete Duplicate Records

March 2nd, 2009 by

I use this cool and handy code snippet for finding duplicate records in a table.
It’s better than exporting the results of your query and then using Microsoft Excel and ASAP Utilities to find the duplicate data.
C’mon…I know a lot of you people do that.

Code:

SELECT * | DELETE
FROM table
WHERE IDfield IN (SELECT IDfield
                      FROM table
                      WHERE EXISTS(SELECT NULL
                                   FROM table AS tmptable
                                   WHERE table.field1 = tmptable.field1
                                   [AND table.field2 = tmptable.field2
                                   [AND]]
                                   HAVING table.IDfield > MIN(tmptable.IDfield)))

Search
Categories