SSIS capturing multiple variables

  • Hello, I would be grateful if anyone could help me. I have to modify an SSIS package that someone else has written that prompts the user for a variable using the code below. All I want to do is for the form that pops up to have 20 fields, so when the button is clicked it assigns the value put in the second field to a second variable (Campcode2), the value in the third field to a third variable (Campcode3) etc etc...then I guess the void inputset_Click would contain:

    Dts.Variables["CampCode"].Value = txt.Text.ToString();

    Dts.Variables["CampCode2"].Value = txt.Text.ToString();

    Dts.Variables["CampCode3"].Value = txt.Text.ToString();

    If anyone can think of a simple way of doing this I would really appreciate it. Many thanks. I do not understand C#, I don't know what I have to put in the form code to create 19 new fields and then assign them to 19 new variables

    using System;

    using System.Data;

    using Microsoft.SqlServer.Dts.Runtime;

    using System.Windows.Forms;

    namespace ST_21bd9709f1944de2a5f7eaf9faac94cd.csproj

    {

    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]

    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

    {

    #region VSTA generated code

    enum ScriptResults

    {

    Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,

    Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure

    };

    #endregion

    System.Windows.Forms.Form frm = new Form();

    TextBox txt = new TextBox();

    Button inputset = new Button();

    public void Main()

    {

    inputset.Text = "Enter Campaign Code";

    inputset.Width = 100;

    inputset.Height = 200;

    inputset.Click += new EventHandler(inputset_Click);

    txt.Name = "Input";

    frm.Controls.Add(txt);

    frm.Controls.Add(inputset);

    frm.ShowDialog();

    Dts.TaskResult = (int)ScriptResults.Success;

    }

    void inputset_Click(object sender, EventArgs e)

    {

    Dts.Variables["CampCode"].Value = txt.Text.ToString();

    frm.Close();

    }

    }

    }

  • john.brees (8/14/2014)


    Hello, I would be grateful if anyone could help me. I have to modify an SSIS package that someone else has written that prompts the user for a variable using the code below.

    Wait, whut? This is quite possibly the worst use of SSIS you can do. SSIS is meant for automation behind the scenes and was never intended for user interfacing. This is why you're running into such significant problems trying to do it. It'll fight you every step of the way.

    Yes, like everything else it can be done. You're opening up a can of worms you don't realize yet though if you try.

    Are you willing to back this discussion up a bit? What is the purpose of this SSIS package and user interface? Maybe we can give you something a lot more friendly to attempt to implement.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Any specific reason for having the UI. why don't you just put all the 20 variables in a confiuration file and pass those values at the runtime?

  • Thanks for the replies. It's running a sql stored procedure that has one argument. It's working well for the one argument, I just need to know how to modify the windows form. A configuration file is not going to be suitable, it's been in use for some time by an end user. I want to expand it to 20 variables, I just want it to store whatever is entered, if anything is entered.

    I would really appreciate it if a visual C# expert could tell me how to modify the windows form to take 20 strings, then how to convert the strings to 20 variables. I know it is not ideal.

    Many thanks

  • It is pulling customers out of a database according to the codes that are entered, the codes come over in a brief, there may be 1 or more than 1 but 20 should be sufficient

  • You also need to declare 20 variables named dtsCampaign_Code1,dtsCampaign_Code2 .....dtsCampaign_Code20 in the Variables window.

    System.Windows.Forms.Form frm = new Form();

    Label[] lblCampaignCode = new Label[20];

    TextBox[] txtCampaignCode = new TextBox[20];

    Button btnCampaignCode = new Button();

    // Create a variables 'container' to store variables

    Variables vars = null;

    public void Main()

    {

    for (int i = 0; i < lblCampaignCode.Length; i++)

    {

    var lblCampaign_Code = new Label();

    lblCampaignCode = lblCampaign_Code;

    lblCampaign_Code.Name = "lblCampaign_Code" + (i+1);

    lblCampaign_Code.Text = "CampaignCode" + i+1;

    lblCampaign_Code.Location = new Point(15, 32 + (i * 28));

    lblCampaign_Code.Visible = true;

    lblCampaign_Code.Font = new Font(lblCampaign_Code.Font, FontStyle.Bold);

    lblCampaign_Code.TextAlign = ContentAlignment.MiddleCenter;

    lblCampaign_Code.AutoSize = true;

    frm.Controls.Add(lblCampaign_Code);

    }

    for (int i = 0; i < txtCampaignCode.Length; i++)

    {

    var txtCampaign_Code = new TextBox();

    txtCampaignCode = txtCampaign_Code;

    txtCampaign_Code.Name = "txtCampaign_Code" + (i+1);

    txtCampaign_Code.Location = new Point(172, 32 + (i * 28));

    txtCampaign_Code.Visible = true;

    txtCampaign_Code.Font = new Font(txtCampaign_Code.Font, FontStyle.Bold);

    //txtCampaign_Code.TextAlign = ContentAlignment.MiddleCenter;

    txtCampaign_Code.AutoSize = true;

    frm.Controls.Add(txtCampaign_Code);

    }

    btnCampaignCode.Name = "btnCampaignCode";

    btnCampaignCode.Text = "Submit";

    btnCampaignCode.Visible = true;

    btnCampaignCode.Font = new Font(btnCampaignCode.Font, FontStyle.Bold);

    btnCampaignCode.AutoSize = true;

    btnCampaignCode.Location = new Point(125, 32 + (23 * 28));

    frm.Controls.Add(btnCampaignCode);

    btnCampaignCode.Click += new EventHandler(btnCampaignCode_Click);

    frm.WindowState = FormWindowState.Maximized;

    frm.ShowDialog();

    Dts.TaskResult = (int)ScriptResults.Success;

    }

    void btnCampaignCode_Click(object sender, EventArgs e)

    {

    foreach (TextBox inputBox in txtCampaignCode)

    {

    string variable = "dts" + inputBox.Name.Replace("txt", "");

    // Lock variables

    Dts.VariableDispenser.LockForWrite(variable);

    // Add variables from the VariableDispenser to the variables 'container'

    Dts.VariableDispenser.GetVariables(ref vars);

    // Now you can use the variables

    if (inputBox.Name.ToString().Substring(3, (inputBox.Name.ToString().Length) - 3) == variable.ToString().Substring(3, (variable.ToString().Length) - 3))

    {

    int number;

    if( (int.TryParse(inputBox.Text, out number)) == true)

    {

    vars[variable].Value = Convert.ToInt32(inputBox.Text);

    }

    // if DTS varibale is of type String

    // vars[variable].Value = inputBox.Text.ToString();

    }

    // Release the locks

    vars.Unlock();

    frm.Close();

    }

    }

    }

  • rxm119528 thank you so much! I do appreciate your help, you are a great guy thanks again 🙂

  • Thanks very much for this, I have tried using and it is only giving a couple of errors, the line:

    Variables vars = null;

    gives error:

    Error1The type or namespace name 'Variables' could not be found (are you missing a using directive or an assembly reference?)

    and:

    Dts.VariableDispenser.GetVariables(ref vars);

    gives error:

    Error2The best overloaded method match for 'Microsoft.SqlServer.Dts.Runtime.VariableDispenser.GetVariables(ref Microsoft.SqlServer.Dts.Runtime.Variables)' has some invalid argumentsst_c8fd8765cc1c4f55ba93d8a2aadcd90f

    Error3Argument '1': cannot convert from 'ref Variables' to 'ref Microsoft.SqlServer.Dts.Runtime.Variables'

    I have created the 20 variables in SSIS, dtsCampaign_Code1, dtsCampaign_Code2, dtsCampaign_Code3.....dtsCampaign_Code20 as string variables.

    If anyone could help I would really appreciate it, I can see this is so close to working now!!

    Many thanks

  • using Microsoft.SqlServer.Dts.Runtime should be there in the namespace list.

  • Oh great thank you, that has removed those errors. The only errors I am getting now are whenever these 3 lines appear:

    lblCampaign_Code.Location = new Point(15, 32 + (i * 28));

    lblCampaign_Code.Font = new Font(lblCampaign_Code.Font, FontStyle.Bold);

    lblCampaign_Code.TextAlign = ContentAlignment.MiddleCenter;

    I get

    Error1The type or namespace name 'Point' could not be found (are you missing a using directive or an assembly reference?)

    Error2The type or namespace name 'Font' could not be found (are you missing a using directive or an assembly reference?)

    Error3The name 'FontStyle' does not exist in the current context

    Error4The name 'ContentAlignment' does not exist in the current context

    Am I missing another reference?

    thanks so much for help so far

  • you have to add the system.drawing reference. Attached is the screen shot.

  • I have these references at the top:

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Text;

    using System.Windows.Forms;

    using Microsoft.SqlServer.Dts.Runtime;

    do I need to use another reference for the point, font etc lines to work?

  • Go to the project explorer window(CTRL+ALT+L). then right click on the references and add the system.drawing reference as show in the previous screen shot.

  • Thanks, have done that, I am getting somewhere now, there is only one line the compiler is complaining about now:

    txtCampaign_Code.TextAlign = ContentAlignment.MiddleCenter;

    error is:

    Error1Cannot implicitly convert type 'System.Drawing.ContentAlignment' to 'System.Windows.Forms.HorizontalAlignment'. An explicit conversion exists (are you missing a cast?)

    I tried just commenting out this line and it now runs with the attached screenshot:

    the labels are CampaignCode01

    CampaignCode11

    CampaignCode21

    CampaignCode31 etc

    rather than CampaignCode1

    CampaignCode2

    CampaignCode3

    CampaignCode4 etc?

    I'm sure this is easily fixable where it creates the labels? I have called my SSIS variables dtsCampaign_Code1, dtsCampaign_Code2, dtsCampaign_Code3 like you suggested? so I guess it would be best for the labels to match?

    This is really close now, thanks very much again

  • chage the line

    lblCampaign_Code.Text = "CampaignCode" + i + 1;

    to

    lblCampaign_Code.Text = "CampaignCode" + (i + 1);

    Campaign_Code.TextAlign = ContentAlignment.MiddleCenter is already commented in my orgianl code. so you can keep that commented out.

Viewing 15 posts - 1 through 15 (of 20 total)

You must be logged in to reply to this topic. Login to reply