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();
}
}
}