Productive Edge | Microsoft at Productive Edge

TAG | Example

I have a long list of users to add to an existing domain, and using the Active Directory Users and Computers GUI:

image

is an easy way to add a couple of users:

image

image

image

image

but with over 40 to add, it would take too long.  How can I speed this up?  dsadd to the rescue!  The command line you would use to add a new user is:

dsadd user "CN=First Last,CN=User,DC=corp,DC=productiveedge,DC=com" -samid first.last -pwd qwerty123! -mustchpwd yes

so you can create a script to add users to a domain relatively easily.  Note the:

-mustchpwd yes

at the end of the line.  That is added to require the user to change their password on the first login.

· · · · · · ·

This isn’t really big news, but I have had to look it up twice in the last quarter.  To find which stored procedures contain a specific text string, use:

SELECT Name, OBJECT_DEFINITION(OBJECT_ID)
FROM sys.procedures sp
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Language%'

· · · ·

Working with legacy code is always one of my favorite things to do.  I believe it helps me write more maintainable code, and the little mysteries are always quite engaging.  The most recent example that I feel compelled to share is related to the setting of a database value based on the selection made in a radio button group.  In this particular instance, a set of custom localization routines help cloud the issue by abstracting the text from the code, but the central fault is more universal.  There was an undocumented naming convention that was initially followed within the database, but the constraints on columns were not always put in place to enforce the convention and the code itself was not designed to assist in any way.

On to specifics (altered to insure confidentiality). 

  • Initially, in the UI there are three radio buttons that correspond to an integer column in the database (color).  The value is set on a public property of an object that corresponds to the table (widgets) in the database.  There is a corresponding dimension table (widgetColorCode) in the database, but there is a missing foreign key constraint and the naming convention doesn’t make it easy to track down the dimension domain.
  • Time passes.
  • A new UI is built for a new company that only manufactures red Widgets.  The combo boxes are removed, and the value is set upon submission to the constant 1. 
  • A derivative of the new UI is commissioned that is for a company only supplying blue Widgets.   There is nothing in the code to help the programmer find what value this property should have. 

So the code for the Widget class looks something like this:

    public class Widget
    {
        public int Color { get; set; }
    }

I am a firm believer in having enumerations corresponding to all dimensions in a project.  There is a performance hit for converting an enumeration to an integer, but it is far outweighed by the increased maintainability of the code.  By explicitly setting the values for the enumeration and adding an XML comment relating it to the dimension table, you can greatly increase the maintainability of the code. When starting from scratch on a project, I will typically have something along the lines of:

    ///

    /// corresponds to the Widget fact table
    /// 

    public class Widget
    {
        public WidgetColor Color { get; set; }
    }

    ///

    /// corresponds to the WidgetColorCode dimension table
    /// 

    public enum WidgetColor
    {
        Red = 1,
        White = 2,
        Blue = 3
    }

But, that is a personal preference, and refactoring the entire code base at this point can’t be justified.  What can be done quickly to lessen the impact of this when further maintenance is required?  Visual Studio comes to the rescue!  By simply adding an XML comment to the property, we can define the dimension so that hovering over the property will display the dimension information (Please excuse the missing pointer in the image, trust me, I am hovering over the Color property.):

    ///

    /// corresponds to the Widget fact table
    /// 

    public class Widget
    {
        ///

        /// corresponds to the WidgetColorCode dimension table
        /// 1: Red, 2: White, 3: Blue
        /// 

        public int Color { get; set; }
    }

image

· · · · · · ·

I have been working with Telerik controls for a little over two months (the ASP.NET AJAX suite) and have been continuously impressed with the quality and power they provide.  I am working on a project that requires the upload of several different file types which need to be displayed in a grid once uploaded.  The cleanest solution I could think of was to create a User Control to handle the uploads and embed it within the grid through the EditFormSettings FormTemplate.  So first, I created a simple upload User Control as follows:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UploadControl.ascx.cs"
    Inherits="WebApplication1.UserControls.UploadControl" %>
    
    
    
    
    
    Submit

with the code behind set up to store the uploaded files in the database using LINQ:

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            LinerModelDataContext dataContext = new LinerModelDataContext();
            foreach (UploadedFile file in RadUpload1.UploadedFiles)
            {
                byte[] bytes = new byte[file.ContentLength];
                file.InputStream.Read(bytes, 0, file.ContentLength);
                Attachment attachment = new Attachment();
                attachment.FileName = file.GetName();
                attachment.BinaryAttachment = bytes;
                attachment.CustomerID = this.CustomerID;
                dataContext.Attachments.InsertOnSubmit(attachment);
            }
            dataContext.SubmitChanges();
        }

I then just added the EditFormSettings section to RadGrid1 and I was able to upload files:

                    
                        
                        
                        
                            
                        
                    

But my grid wasn’t refreshing and I wasn’t getting any notification that the upload was complete. So, to provide the user experience I wanted, I exposed an event to the grid from the upload control by modifying the code behind of the UserControl. I added a delegate, and exposed the event as follows:

    public delegate void UploadHandler();

    public partial class UploadControl : System.Web.UI.UserControl
    {
...
        public event UploadHandler Upload;

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            LinerModelDataContext dataContext = new LinerModelDataContext();
            foreach (UploadedFile file in RadUpload1.UploadedFiles)
            {
                byte[] bytes = new byte[file.ContentLength];
                file.InputStream.Read(bytes, 0, file.ContentLength);
                Attachment attachment = new Attachment();
                attachment.FileName = file.GetName();
                attachment.Attachment1 = bytes;
                attachment.CustomerID = this.CustomerID;
                dataContext.Attachments.InsertOnSubmit(attachment);
            }
            dataContext.SubmitChanges(ConflictMode.ContinueOnConflict);
            if (Upload != null)
            {
                Upload.Invoke();
            }
        }

and changing the FormTemplate to be:

                        
                            
                        

(please note that the syntax highlighter I am using mangled the code by changing OnUpload to be onupload in the above source) and adding the handler to the main page:

        protected void UploadControl1_Upload()
        {
            RadGrid1.MasterTableView.IsItemInserted = false;
            RadGrid1.Rebind();
        }

· · ·

Sep/10

6

Quick SQL snippit

I occasionally need to get some representative values from a table for testing and thought my solution might be something worth sharing.  I tend to think in nested selects and go back and clean up code into joins where appropriate.  Below you will see that I am using a group by and a min function to identify single instances of different types, and then pulling the required data in an outer select:

SELECT
	UserName
FROM
	Users
WHERE
	UserID in
	(
	SELECT
		MIN(UserID)
	FROM
		USERS
	WHERE
		Active = 1
	GROUP BY
		TypeCode
	)

· ·

Theme Design by devolux.nh2.me