Export (0) Print
Expand All

Validating a Data Flow Component

The Validate method of the PipelineComponent base class is provided to prevent execution of a component that is not configured correctly. Use this method to verify that a component has the expected number of input and output objects, that the custom properties of the component have acceptable values, and that any connections, if required, are specified. Use this method also to verify that the columns in the input and output collections have the correct data types and that the DTSUsageType of each column is set appropriately for the component. The base class implementation assists in the validation process by checking the input column collection of the component and ensuring that each column in the collection refers to a column in the IDTSOutputCollection100 of the upstream component.

The Validate method is called repeatedly when a component is edited in SSIS Designer. You can provide feedback to the designer and to users of the component through the DTSValidationStatus enumeration return value, and by posting warnings and errors. The DTSValidationStatus enumeration contains three values that indicate various stages of failure, and a fourth, VS_ISVALID, that indicates whether the component is correctly configured and ready to execute.

The VS_NEEDSNEWMETADATA value indicates that an error exists in the ComponentMetaData, and that the component can repair the errors. If a component encounters a metadata error that it can repair, it should not fix the error in the Validate method, and ComponentMetaData should not be modified during validation. Instead, the Validate method should return only VS_NEEDSNEWMETADATA, and the component should repair the error in a call to the ReinitializeMetaData method, as described later in this section.

The VS_ISBROKEN value indicates that the component has an error that can be rectified by editing the component in the designer. The error is typically caused by a custom property or a required connection that is not specified or is set incorrectly.

The final error value is VS_ISCORRUPT, which indicates that the component has discovered errors that should only occur if the ComponentMetaData property has been modified directly, either by editing the package XML or by using the object model. For example, this kind of error occurs when a component has added only a single input, but validation discovers that more than one input exists in the ComponentMetaData. Errors that generate this return value can only be repaired by resetting the component by using the Reset button in the Advanced Editor dialog box.

Besides returning error values, components provide feedback by posting warnings or errors during validation. The FireWarning and FireError methods provide this mechanism. When these methods are called, these events are posted in the Error List window of SQL Server Data Tools (SSDT). Component developers can then provide direct feedback to users on the errors that have occurred, and if appropriate, how to correct them.

The following code example shows an overridden implementation of Validate.

public override DTSValidationStatus Validate()
{
    bool pbCancel = false;

    // Validate that there is one input.
    if (ComponentMetaData.InputCollection.Count != 1)
    {
    ComponentMetaData.FireError(0, ComponentMetaData.Name, "Incorrect number of inputs.", "", 0, out pbCancel);
    return DTSValidationStatus.VS_ISCORRUPT;
    }

    // Validate that the UserName custom property is set.
    if (ComponentMetaData.CustomPropertyCollection["UserName"].Value == null || ((string)ComponentMetaData.CustomPropertyCollection["UserName"].Value).Length == 0)
    {
        ComponentMetaData.FireError(0, ComponentMetaData.Name, "The UserName property must be set.", "", 0, out pbCancel);
        return DTSValidationStatus.VS_ISBROKEN;
    }

    // Validate that there is one output.
    if (ComponentMetaData.OutputCollection.Count != 1)
    {
        ComponentMetaData.FireError(0, ComponentMetaData.Name, "Incorrect number of outputs.", "", 0, out pbCancel);
        return DTSValidationStatus.VS_ISCORRUPT;
    }

    // Let the base class verify that the input column reflects the output 
    // of the upstream component.
    return base.Validate();
}

The ReinitializeMetaData method is called by SSIS Designer whenever you edit a component that returns VS_NEEDSNEWMETADATA from its Validate method. Components should contain code that detects and corrects the problems identified by the component during validation.

The following example shows a component that detects problems during validation and fixes these errors in the ReinitializeMetaData method.

private bool areInputColumnsValid = true;
public override DTSValidationStatus Validate()
{
    IDTSInput100 input = ComponentMetaData.InputCollection[0];
    IDTSVirtualInput100 vInput = input.GetVirtualInput();

    bool Cancel = false;
    foreach (IDTSInputColumn100 column in input.InputColumnCollection)
    {
        try
        {
            IDTSVirtualInputColumn100 vColumn = vInput.VirtualInputColumnCollection.GetVirtualInputColumnByLineageID(column.LineageID);
        }
        catch
        {
            ComponentMetaData.FireError(0, ComponentMetaData.Name, "The input column " + column.IdentificationString + " does not match a column in the upstream component.", "", 0, out Cancel);
            areInputColumnsValid = false;
            return DTSValidationStatus.VS_NEEDSNEWMETADATA;
        }
    }

    return DTSValidationStatus.VS_ISVALID;
}
public override void ReinitializeMetaData()
{
    if (!areInputColumnsValid)
    {
        IDTSInput100 input = ComponentMetaData.InputCollection[0];
        IDTSVirtualInput100 vInput = input.GetVirtualInput();

        foreach (IDTSInputColumn100 column in input.InputColumnCollection)
        {
            IDTSVirtualInputColumn100 vColumn = vInput.VirtualInputColumnCollection.GetVirtualInputColumnByLineageID(column.LineageID);

            if (vColumn == null)
                input.InputColumnCollection.RemoveObjectByID(column.ID);
        }
        areInputColumnsValid = true;
    }
}
Integration Services icon (small) Stay Up to Date with Integration Services

For the latest downloads, articles, samples, and videos from Microsoft, as well as selected solutions from the community, visit the Integration Services page on MSDN:


For automatic notification of these updates, subscribe to the RSS feeds available on the page.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft