Convert DataReader to DataSet in c#

Monday, 5 October 2009 15:35 by myro

I've just founded an usefull code snippet that returns a DataSet from a DataReader. As you can see, I'm passing a DbDataReader, because I prefer to work with the DbProviderFactory instead of using directly the System.Data.Sql namespace. A replace of the DbDataReader into SqlDataReader, will work exactly the same.

public static DataSet convertDataReaderToDataSet(DbDataReader reader)
{
    DataSet dataSet = new DataSet();
    do
    {
        // Create new data table
        DataTable schemaTable = reader.GetSchemaTable();
        DataTable dataTable = new DataTable();

        if (schemaTable != null)
        {
            // A query returning records was executed
            for (int i = 0; i < schemaTable.Rows.Count; i++)
            {
                DataRow dataRow = schemaTable.Rows[i];
                // Create a column name that is unique in the data table
                string columnName = (string)dataRow["ColumnName"];
                // Add the column definition to the data table
                DataColumn column = new DataColumn(columnName, (Type)dataRow["DataType"]);
                dataTable.Columns.Add(column);
            }
            dataSet.Tables.Add(dataTable);
            // Fill the data table we just created
            while (reader.Read())
            {
                DataRow dataRow = dataTable.NewRow();

                for (int i = 0; i < reader.FieldCount; i++)
                    dataRow[i] = reader.GetValue(i);

                dataTable.Rows.Add(dataRow);
            }
        }
        else
        {
            // No records were returned
            DataColumn column = new DataColumn("RowsAffected");
            dataTable.Columns.Add(column);
            dataSet.Tables.Add(dataTable);
            DataRow dataRow = dataTable.NewRow();
            dataRow[0] = reader.RecordsAffected;
            dataTable.Rows.Add(dataRow);
        }
    }
    while (reader.NextResult());
    return dataSet;
}

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   .NET
Actions:   Bookmark and Share | Permalink | Comments (1) | Comment RSSRSS comment feed

Use regular expressions to validate an email in c# using Regex

Sunday, 4 October 2009 18:32 by myro

If you need a simple method that validates an email address in c#, you are at the right place. The code snipped illustrated below, will check if the provided email address matches a regular expressionto and will determine if is a valid email or not.

public bool IsMailValid(string emailAddress)
{
    StringBuilder sb = new StringBuilder();
    sb.Append(@"^(([^<>()[\]\\.,;:\s@\""]+");
    sb.Append(@"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@");
    sb.Append(@"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}");
    sb.Append(@"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+");
    sb.Append(@"[a-zA-Z]{2,}))$");
    Regex reStrict = new Regex(sb.ToString());
    bool isStrictMatch = reStrict.IsMatch(emailAddress);
    return isStrictMatch;
}

Use it, paste it and share it!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   , ,
Categories:   .NET
Actions:   Bookmark and Share | Permalink | Comments (0) | Comment RSSRSS comment feed

Using Reflection to display object's property values in c#.

Monday, 28 September 2009 17:08 by myro

This code snippet posted below can be used to display all object's property values, by inkoing the ToString() of each property. An usefull implementation could be when you are in need of logging an object's state by writing to a log file all his properties states. 
The DisplayObjectProperties static method cycles throught the object's properties and as told before, it calls the ToString() method, which is always implemented since every class is derived from the Object class. Recuirsion is not implemented, the ToString() method will be called only at the first level.

public static class ReflectionHelper
{
    public static string DisplayObjectProperties(Object o)
    {
        StringBuilder sb = new StringBuilder();
        System.Type type = o.GetType();
        foreach (PropertyInfo p in type.GetProperties())
        {
            if (p.CanRead)
            {
                object obj = p.GetValue(o, null);
                if (obj != null)
                {
                    sb.AppendLine(String.Concat("-Property name: ", p.Name ));
                    sb.AppendLine(String.Concat("-Property value:", obj.ToString()));
                    sb.AppendLine();
                }
                else sb.Append(String.Concat(p.Name, " # Value is null"));

            }
        }
        return sb.ToString();
    }
}

Now immagine that you' ve got this sample class called PersonalAttribute:

 public class PersonalAttribute
{

    public string AttributeName { get; set; }

    public string AttributeValue { get; set; }

}

An usefull implementation could be:

public class PersonalAttribute
{

    public string AttributeName { get; set; }

    public string AttributeValue { get; set; }

    public override string ToString()
    {
        return ReflectionHelper.DisplayObjectProperties(this); 
    }

}

Now, when the ToString() method is invoked in a new PersonalAttribute's istance, the returned string is something similar to:

-Property name: AttributeName
-Property value:SiteName

-Property name: AttributeValue
-Property value:myrocode.com

Remember that using Reflection is slow compared to direct access of a property, field or method. If you are building a complex application, this could be not the best solution for you case.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   .NET
Actions:   Bookmark and Share | Permalink | Comments (0) | Comment RSSRSS comment feed

Code Snippet: Send Bulk Emails in C#

Tuesday, 15 September 2009 09:55 by myro

This small snippets can be used in your project if you need to deliver many emails using c# without caring if the receiver's email is defined in the recipients and in the Carbon Copy (Cc) or in the Blind CArbon Copy (Bcc) twice.
This method simply checks every time if an email is present in the To header is also contained in the Cc and/or in the Bcc.

Bulk emailing is also supported: just separate you emails, using a separator character (in my case is a comma ',').

public static void SendMail(string Subject, string Body, bool IsHtml, string From, string To, string Cc, string Bcc)
{
    const char SEPARATOR = ',';
    string[] Tos = new string[0];
    string[] Ccs = new string[0];
    string[] Bccs = new string[0];


    MailMessage mailMessage = new MailMessage();
    mailMessage.From = new MailAddress(From);
    mailMessage.Subject = Subject;
    mailMessage.Body = Body;
    mailMessage.IsBodyHtml = IsHtml;

    if (!String.IsNullOrEmpty(To))
         Tos = To.Split(new char[] { SEPARATOR }, StringSplitOptions.RemoveEmptyEntries);
    if (!String.IsNullOrEmpty(Cc))
         Ccs = Cc.Split(new char[] { SEPARATOR }, StringSplitOptions.RemoveEmptyEntries);
    if (!string.IsNullOrEmpty(Bcc))
        Bccs = Bcc.Split(new char[] { SEPARATOR }, StringSplitOptions.RemoveEmptyEntries);

    foreach (var item in Tos)
    {
        mailMessage.To.Add(new MailAddress(item));
    }

    foreach (string item in Ccs)
    {
        if (!isMailCointained(item, Tos))
        {
            mailMessage.CC.Add(new MailAddress(item));
        }  
    }

    foreach (string item in Bccs)
    {
        if (!isMailCointained(item, Tos) && !isMailCointained(item ,Ccs))
        {
            mailMessage.Bcc.Add(new MailAddress(item));
        }
    }
    try
    {
        SmtpClient smtpClient = new SmtpClient();
        // add your smtp server!
        smtpClient.Host = ConfigurationManager.AppSettings["YOUR EMAIL SERVER"];
        smtpClient.Send(mailMessage);
 }
     catch(Exception exc){// add your exception handlers.. }
}

private static bool isMailCointained(string mail, string[] mails)
{
    bool toRet = false;

    foreach (var item in mails)
    {
        if (mail == item)
            return true;
    }
    return toRet;
}

If you call the SendMail method using:

  SendMail("Hello", "<h3>Hello from myrocode!</h3>", true,
                "myEmail@a.com", "a@a.com,b@a.com,c@a.com", "z@a.com,b@a.com", "x@a.com,y@a.com,z@a.com");

You email will be deliverd with the following headers:

To: a@a.com
      b@a.com
      c@a.com

Cc:z@a.com

Bcc:x@a.com
      y@a.com

this post is nothing special.. but can be usefull for a fast refernce or a copy & paste code snippet for your project.

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   .NET | .NET
Actions:   Bookmark and Share | Permalink | Comments (2) | Comment RSSRSS comment feed

using Active directory with C#: LDAP query to get all users in a specific Group

Monday, 14 September 2009 11:14 by myro

This post illustrates how can you retreive all user's emails that belongs to an Active Directory group in c# using the .NET framework 3.5.
Code snippets provides 3 classes that shows you how to obtain basic informations about users and group, quering a LDAP server:

  • AdUser.cs is just a container class that rappresents a user with 3 basic properties: Distinguished Name, Display Name and Email
  • AdUserCollection.js is just an AdUser collection
  • ADSearcher.cs rappresents your Active Directory wrapper that retrieves your users based on LDAP queries

Use this classes and modify them to rappresents better your needs: right now the ADSearcher provides just methods to get Users that belongs to and Active Directory Group

ADUser class

public class AdUser
{
    string _email;
    string _name;
    string _displayName;

    public string Name
    {
        get { return _name; }
        private set { _name = value.ToLower().Trim(); }
    }

    public string DisplayName
    {
        get { return _displayName; }
        private set { _displayName = value.ToLower().Trim(); }
    }

    public string Email
    {
        get { return _email; }
        private set { _email = value.ToLower().Trim(); }
    }


    public AdUser(string name, string displayName, string email)
    {
        Name = name;
        DisplayName = displayName;
        Email = email;
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != typeof(AdUser)) return false;

        return Equals((AdUser)obj);
    }

    public bool Equals(AdUser other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return Equals(other.Name, Name);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            int result = (Name != null ? Name.GetHashCode() : 0);
            return result;
        }
    }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("Name: "+ Name);
        sb.AppendLine("DisplayName: " + DisplayName);
        sb.AppendLine("Email: " + Email);
        return sb.ToString();
    }
}

ADUserCollection class 

public class AdUserCollection : List<AdUser>
{
    public string GetAllMails()
    {
        StringCollection sc = new StringCollection();
        foreach (var item in this)
        {
            string email = item.Email.ToLower().Trim();
            if (!String.IsNullOrEmpty(email) && !sc.Contains(email) )
            {
                sc.Add(email);
            }
        }
        string toRet = string.Empty;

        foreach (var item in sc)
        {
           toRet =  string.Concat(toRet, ",", item );
        }
        toRet =  toRet.Remove(0, 1);
        return toRet;        
    }

    new public void AddRange(AdUserCollection users)
    {
        foreach (var user in users)
        {
            if (!this.Contains(user))
            {
                this.Add(user);
            }
        }
    }
}

ADSearcher class

public class ADSearcher
{

    string _ldap = string.Empty;

    public ADSearcher(string ServerLDAP)
    {
        _ldap = ServerLDAP;
    }

     public AdUserCollection GetUsersFromNeastedGroups
   (StringCollection activeDirectoryGroups)
    {
        AdUserCollection users = new AdUserCollection();
        foreach (var item in activeDirectoryGroups)
        {
            IEnumerable<AdUser> group = GetUsersFromAdGroup(item);
            if (group != null)
                users.AddRange(GetUsersFromAdGroup(item));
        }
        return users;
    }

     public IEnumerable<AdUser> GetUsersFromAdGroup
   (string activeDirectoryGroup)
    {

        DirectoryEntry entry = new DirectoryEntry(_ldap);
        DirectorySearcher search = new DirectorySearcher(entry);
        search.Filter = String.Format("(&(objectCategory=group)(cn={0}))", activeDirectoryGroup);
        search.PropertiesToLoad.Add("distinguishedName");
        SearchResult searchResult = search.FindOne();

        if (searchResult == null)
            return null;
        DirectoryEntry group = searchResult.GetDirectoryEntry();

        Hashtable searchedGroups = new Hashtable();
        return GetUsersInGroup(group.Properties["distinguishedName"].Value.ToString(),
        searchedGroups);
    }


    HashSet<AdUser> GetUsersInGroup(string activeDirectoryGroup,
       Hashtable searchedGroups)
    {
        HashSet<AdUser> groupMembers = new HashSet<AdUser>();
        searchedGroups.Add(activeDirectoryGroup, activeDirectoryGroup);

        // find all users in this group
        DirectorySearcher ds = new DirectorySearcher(_ldap);
        ds.Filter = String.Format("(&(memberOf={0})(objectClass=person))", activeDirectoryGroup);

        ds.PropertiesToLoad.Add("sAMAccountName");
        ds.PropertiesToLoad.Add("mail");
        ds.PropertiesToLoad.Add("displayName");

        //Add user to the list
        foreach (SearchResult sr in ds.FindAll())
        {
            string name = sr.Properties["sAMAccountName"][0].ToString();
            string email = string.Empty;
            if (sr.Properties.Contains("mail"))
            email = sr.Properties["mail"][0].ToString();

            string displayname = string.Empty;
            if (sr.Properties.Contains("displayName"))
                displayname = sr.Properties["displayName"][0].ToString();

            AdUser user = new AdUser(name, displayname, email);
            if (groupMembers.Contains(user) == false)
            {
                groupMembers.Add(user);
            }
        }

        List<string> nestedGroups = GetNestedGroups(activeDirectoryGroup);
        foreach (string directoryGroup in nestedGroups)
        {
            if (searchedGroups.ContainsKey(directoryGroup) == false)
            {
              HashSet<AdUser> users = GetUsersInGroup(directoryGroup,
        searchedGroups);
              foreach (AdUser user in users)
              {
                 if (groupMembers.Contains(user) == false)
                 {
                      groupMembers.Add(user);
                 }
              }
            }
        }

        return groupMembers;
    }

    private List<string> GetNestedGroups(string activeDirectoryGroup)
    {
        List<string> groupMembers = new List<string>();
        DirectorySearcher ds = new DirectorySearcher(_ldap);
        ds.Filter = String.Format("(&(memberOf={0})(objectClass=group))",
     activeDirectoryGroup);

        ds.PropertiesToLoad.Add("distinguishedName");
        foreach (SearchResult sr in ds.FindAll())
        {
            groupMembers.Add(sr.Properties["distinguishedName"][0].ToString());
        }
        return groupMembers;
    }
  }

Once you have copied this classes into your project you could use the ADSearcher to get users' emails that belongs to an Active Directory Group:

ADSearcher searcher = new ADSearcher(ConfigurationManager.AppSettings["LdapServer"]);
AdUserCollection UsersInGroup = searcher.GetUsersFromMulitpleGroups(sc);
string UsersInGroupMails = UsersInGroup.GetAllMails();

 

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   .NET
Actions:   Bookmark and Share | Permalink | Comments (0) | Comment RSSRSS comment feed