Q:

Generating unique random string in SharePoint document library

I'm working on customizing a SharePoint document library called "Quality Documents" so that when new docs are added to the library, a random and unique number is generated and applied to a field named "Document Number".  I coded the feature below, but it's not working.  Can anyone see what might be the problem?  Nothing happens, no errors nothing, the page just works fine, but no Document Number gets generated.  Any suggestions?
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

namespace QualityDocHandler
{
    class DocumentHandler : SPItemEventReceiver
    {
    /// <summary>
    /// Generates a random string with the given length
    /// </summary>
    /// <param name="size">Size of the string</param>
    /// <returns>Random string</returns>

    private string RandomString(int size)
    {
        StringBuilder builder = new StringBuilder();
        Random random = new Random();
        char ch;
        for (int i = 0; i < size; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            builder.Append(ch);
        }
        return builder.ToString();
    }

    private string createDocNum(SPItemEventProperties properties)
    {
        int newRnd = 0;

        do
        {
            // set static department
            string dept = "QUA";

            // set date without separators
            string dateString = DateTime.Today.ToString("ddMMyyyy");

            // get 1st random string 
            string Rand1 = RandomString(4);

            // get 1st random string 
            string Rand2 = RandomString(4);

            // creat full document number
            string docNum = dept + "-" + dateString + "-" + Rand1 + "-" + Rand2;

            using (SPWeb oWeb = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl))
            {
                SPList oList = oWeb.Lists["Quality Documents"];

                //create query
                SPQuery oQuery = new SPQuery();

                //configure the query  //
                oQuery.Query = "<Where><Eq><FieldRef Name='Document_x0020_Number' /><Value Type='Text'>" + docNum + "</Value></Eq></Where>";

                //get the collection of items in the list
                SPListItemCollection oItems = oList.GetItems(oQuery);

                if (oItems.Count > 0)
                {
                    newRnd = 0;
                }
                else
                {
                    newRnd = 1;
                }
            }
            return docNum;
        }
        while (newRnd < 1);

    }

    public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);
    }

    public override void ItemAdding(SPItemEventProperties properties)
    {

        string documentNum = createDocNum(properties);
        using (SPWeb oWeb = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl))
        {
            SPListItem listItem = properties.ListItem;
            properties.AfterProperties["Document_x0020_Number"] = documentNum;
            listItem.Update();
            oWeb.Update();
        }
        base.ItemAdding(properties);

    }

    public override void ItemUpdated(SPItemEventProperties properties)
    {
        base.ItemUpdated(properties);
    }

    public override void ItemUpdating(SPItemEventProperties properties)
    {
        base.ItemUpdating(properties);
    }

}

}

A:

I was able to get this working.  Here's the finished code:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

namespace QualityDocHandler
{
    class DocumentHandler : SPItemEventReceiver
    {
        private readonly Random _rng = new Random();
        private const string _chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private string RandomString(int size)
        { 
            char[] buffer = new char[size];
            for (int i = 0; i < size; i++)
            {
                buffer[i] = _chars[_rng.Next(_chars.Length)];
            }
            return new string(buffer);
        }

        private string createDocNum(SPItemEventProperties properties)
        {
            int newRnd = 0;

            do
            {
                // set static department
                string dept = "QUA";

                // set date without separators
                string dateString = DateTime.Today.ToString("ddMMyyyy");

                // get 1st random string 
                string Rand1 = RandomString(4);

                // get 2nd random string 
                string Rand2 = RandomString(4);

                // creat full document number
                string docNum = dept + "-" + dateString + "-" + Rand1 + "-" + Rand2;

                using (SPWeb oWeb = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl))
                {
                    SPSiteDataQuery q = new SPSiteDataQuery();
                    q.Lists = "<Lists BaseType='1'/>";
                    q.Query = "<Where><Eq><FieldRef Name='Document_x0020_Number' /><Value Type='Text'>" + docNum + "</Value></Eq></Where>";
                    q.Webs = "<Webs Scope='SiteCollection' />";
                    q.RowLimit = 1;

                    System.Data.DataTable spSiteDataQueryResults = oWeb.GetSiteData(q);

                    if (spSiteDataQueryResults.Rows.Count > 0)
                    {
                        newRnd = 0;
                    }
                    else
                    {
                        newRnd = 1;
                    }
                }

                return docNum;
            }
            while (newRnd < 1);
        }

        public override void ItemAdded(SPItemEventProperties properties)
        {
            this.DisableEventFiring();
            properties.ListItem["Document Number"] = properties.AfterProperties["Document Number"];
            properties.ListItem.SystemUpdate();
            this.EnableEventFiring();
        }

        public override void ItemAdding(SPItemEventProperties properties)
        {
            string documentNum = createDocNum(properties);

            this.DisableEventFiring();
            properties.AfterProperties["Document Number"] = documentNum;
            this.EnableEventFiring();
        }

        public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);
        }

        public override void ItemUpdating(SPItemEventProperties properties)
        {
            base.ItemUpdating(properties);
        }
    }
}

