End Google Ads 201810 - BS.net 01 --> Good morning.

I set several global variables to be used throughout the form code and when I need to reference the strContainer in the WorkerImport_ProgressChanged event, the value is Null for some reason. It is fine in the Submit event and passed successfully to the WorkerImport_DoWork event. Any idea why the strContainer is Null? My understanding is,with Win form apps the WorkerImport_ProgressChanged event operates on the main (GUI) thread. The code is as follows:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

using System.Collections;
using System.IO;
using System.Xml.Linq;
using System.Configuration;
using System.Threading;

namespace IPControl
{
public partial class ucWAH : UserControl
{

//Global variables
string strContainer = String.Empty;
string strDave = String.Empty;
object[] obj;
ArrayList ar = new ArrayList();


FormFunctions ff = new FormFunctions();
UIPCMainForm uipc;
VariableDefs vd = new VariableDefs();

//THese variables will be removed when we go live. We must
//make sure the defaul for drop-downs sets to the SelectedIndex = 0
const string constr******** = "Chattanooga";
const string constrSubnet = "/29 - Standard Work at Home";
const string constrRouter = "Cisco 999";
const string constrEUDepart = "Contact Center";

ArrayList arSubNetList = new ArrayList();
ArrayList arDHCPOptionList = new ArrayList();
ArrayList arDHCPPolicyList = new ArrayList();
ArrayList arAl********TemplateList = new ArrayList();

string strWAHSuffix = ConfigurationSettings.AppSettings["WAH_SUFFIX"];

public ucWAH()
{
InitializeComponent();
}

#region Work at Home Form Events

private void ucTabs_Load(object sender, EventArgs e)
{
PwdSecurity p = new PwdSecurity();
uipc = this.Parent as UIPCMainForm;

DAL d = new DAL(uipc.StrUserName, p.base64Decode(uipc.StrPassword));

DataSet ds = new DataSet();

#region Populate Drop Downs

ds = d.FillDropDowns(1); //********
if (checkForEmptyDataset(ds)) return;
if (ds == null)
{
return;
}
cb********.DataSource = ds.Tables[0];
cb********.DisplayMember = vd.STR_COLUMN_NEAR_********;
cb********.ValueMember = vd.STR_COLUMN_********;

foreach (DataRow dr in ds.Tables[0].Rows)
{
arDHCPOptionList.Add(dr[vd.STR_COLUMN_DHCP_OPTION]);
arDHCPPolicyList.Add(dr[vd.STR_COLUMN_DHCP_POLICY]);
}

ds = null;
ds = d.FillDropDowns(2);
if (checkForEmptyDataset(ds)) return;
cbRouter.DataSource = ds.Tables[0]; //Router
cbRouter.DisplayMember = vd.STR_COLUMN_ROUTER_TYPE;
cbRouter.ValueMember = vd.STR_COLUMN_DM_VPN_CLOUD;

ds = null;
ds = d.FillDropDowns(3); //Subnet Size
if (checkForEmptyDataset(ds)) return;
cbSubnet.DataSource = ds.Tables[0];
cbSubnet.DisplayMember = vd.STR_COLUMN_SUBNET_SIZE;
cbSubnet.ValueMember = vd.STR_COLUMN_DMVPN_SUBNET_TYPE;

foreach (DataRow dr in ds.Tables[0].Rows)
{
arSubNetList.Add(int.Parse(dr[vd.STR_COLUMN_SUBNET_BLOCK_SIZE].ToString()));
arAl********TemplateList.Add(dr[vd.STR_COLUMN_SUBNET_AL********_TEMPLATE].ToString());
}

ds = null;
ds = d.FillDropDowns(5); // End User Department
if (checkForEmptyDataset(ds)) return;
cbEndUserDepart.DataSource = ds.Tables[0];
cbEndUserDepart.DisplayMember = vd.STR_COLUMN_ROUTER_CATEGORY;
cbEndUserDepart.ValueMember = vd.STR_COLUMN_ONE_DIGIT_CODE;

//This section gets the different router configuration choices from the app.config file
string strAppConfigRouterConfigs = ConfigurationSettings.AppSettings["WAH_ROUTER_CONFIGURATION_TYPES"];
string[] strRouterConfigs = strAppConfigRouterConfigs.Split(',');
cbRouterConfig.DataSource = strRouterConfigs;
cbRouterConfig.SelectedIndex = 0;

#endregion

ff.ControlSetFocus(cbEndUserDepart); //Works
cbEndUserDepart.SelectAll();

uipc = this.Parent as UIPCMainForm;
uipc.AcceptButton = btnSubmit;
uipc.Width = 800;
uipc.Height = 600;

#region Set up progress bar thread
#endregion
}

private void ucTabs_Leave(object sender, EventArgs e)
{
uipc.AcceptButton = null;
}

#endregion

#region Work From Home Button Events

private void btnReset_Click(object sender, EventArgs e)
{
tbRACFID.Text = string.Empty;
tbRACFID.Enabled = true;
tbUserName.Text = string.Empty;

cbEndUserDepart.Text = constrEUDepart;
cb********.Text = constr********;
cbSubnet.Text = constrSubnet;
cbRouter.Text = constrRouter;
cbRouterConfig.SelectedIndex = 0;
ff.ControlSetFocus(tbRACFID);
}

private void btnSubmit_Click(object sender, EventArgs e)
{

//uipc = this.Parent as UIPCMainForm;
PwdSecurity p = new PwdSecurity();
DAL d = new DAL(uipc.StrUserName, p.base64Decode(uipc.StrPassword));

// Begin by defining the BackgroundWorker 1

BackgroundWorker WorkerImport = new BackgroundWorker();
WorkerImport.WorkerReportsProgress = true;
WorkerImport.WorkerSupportsCancellation = true;
WorkerImport.DoWork += new DoWorkEventHandler(WorkerImport_DoWork);
WorkerImport.ProgressChanged += new ProgressChangedEventHandler(WorkerImport_ProgressChanged);
WorkerImport.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerImport_RunWorkerCompleted);

if (tbRACFID.Text == string.Empty)
{
MessageBox.Show("You must have a RACFID in order to continue.", "RACFID Field Validation", MessageBoxButtons.OK, MessageBoxIcon.Stop);
ff.ControlSetFocus(tbRACFID);
return;
}
if (tbUserName.Text == string.Empty)
{
MessageBox.Show("You must have a User Name in order to continue.", "User Name Field Validation", MessageBoxButtons.OK, MessageBoxIcon.Stop);
ff.ControlSetFocus(tbUserName);
return;
}

string strContainer = cbEndUserDepart.SelectedValue.ToString() + tbRACFID.Text.ToLower();

// Place the values you need to pass to the DoWork event handler in a single object (i.e. an array)

ArrayList ar = new ArrayList();

ar.Add(strContainer);
ar.Add(tbUserName.Text);
ar.Add(cbRouter.SelectedValue.ToString());
ar.Add(cbRouter.Text);
ar.Add(cbRouterConfig.Text);
ar.Add((int)arSubNetList[cbSubnet.SelectedIndex]);
ar.Add(cbSubnet.SelectedValue.ToString());
ar.Add(arDHCPOptionList[cb********.SelectedIndex].ToString());
ar.Add(arDHCPPolicyList[cb********.SelectedIndex].ToString());
ar.Add(arAl********TemplateList[cbSubnet.SelectedIndex].ToString());
ar.Add(tbUserName.Text);

// Here we use an overloaded version of RunWorkerAsync to
// pass the values to the DoWork event handler so that they
// will be accessible on the separate thread.

WorkerImport.RunWorkerAsync(ar);

#region deactivated code
//Obtain Propper Subnet IP Address based on the Router Type from the Routr table, and
//the DMVPN Subnet Type from the SubntSize table
//string SubnetIPAddress = string.Empty;
//DataSet ds = new DataSet();
//ds = d.FillDropDowns(4); //Subnet Size IP
//foreach (DataRow dr in ds.Tables[0].Rows)
//{
// if (
// dr["ROUTR_TYPE"].ToString() == cbRouter.Text &&
// dr["LOC"].ToString() == cb********.SelectedValue.ToString())
// {
// SubnetIPAddress = dr["SUBNT_IPADR"].ToString();
// }
//}
#endregion


#endregion Work From Home Button Events

}

#region WorkerImport_DoWork

void WorkerImport_DoWork(object sender, DoWorkEventArgs e)
{
PwdSecurity p = new PwdSecurity();
DAL d = new DAL(uipc.StrUserName, p.base64Decode(uipc.StrPassword));

// First get the background worker instance.
BackgroundWorker worker = (BackgroundWorker)sender;

// Now that we are running on the separate thread, call the
// ReportProgress method to set the progress bar.

worker.ReportProgress(0); //Start the progressBar as marquee
//Thread.Sleep(500);

ar = (ArrayList)e.Argument;
string strContainer = (string)ar[0];
string tbUserName = (string)ar[1];
string cbRouter_SelectedValue = (string)ar[2];
string cbRouter_Text = (string)ar[3];
string cbRouterConfig_Text = (string)ar[4];
int arSubNetList = (int)ar[5];
string cbSubnet_SelectedValue = (string)ar[6];
string arDHCPOptionList = (string)ar[7];
string arDHCPPolicyList = (string)ar[8];
string arAl********TemplateList = (string)ar[9];
string tbUserName2 = (string)ar[10];

obj =
d.importContainer(
strContainer,
tbUserName,
cbRouter_SelectedValue,
cbRouter_Text,
cbRouterConfig_Text,
arSubNetList,
cbSubnet_SelectedValue,
arDHCPOptionList,
arDHCPPolicyList,
arAl********TemplateList,
tbUserName2
);


//Cursor.Current = Cursors.WaitCursor;

if (bool.Parse(obj[0].ToString()))
{
// If the Import Container returns true, then we should display success.
worker.ReportProgress(1); // A progressPercentage of 1 will signify success.
}
else
{
// If the Import COntianer returns false, then we should display failure.
worker.ReportProgress(2); // A progressPercentage of 2 will signify failure.
}

}

#endregion WorkerImport_DoWork

#region WorkerImport_ProgressChanged
// This event executes whenever the ReportProgress method is called FROM the DoWork
// event. This event handler executes on the main thread so it can modify controls
// and elements of the application's form without raising cross-threading errors.

void WorkerImport_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
StreamWriter SW;
XDocument xmlDoc;

//DialogResult result;
//string strMessage;

// Here we use a switch construct on the e.progressPercentage value
// in order to set the proper status.

switch (e.ProgressPercentage)
{
case 0: // A case of 0 means that the DoWork even handler has just started.

//MessageBox.Show("0");
// Begin by setting the progress bar.
SetProgressBarMarquee();

// Next set the cursor
Cursor.Current = Cursors.WaitCursor;

break;

case 1: // A case of 1 means that the import completed successfully.

//MessageBox.Show("1");
// Begin by setting the progress bar.
SetProgressBarContinuous();

// Next set the cursor.
Cursor.Current = Cursors.Default;

string strMessage =
"You have successfully imported your Router information into IPControl. \r\n \r\n" +
"Would you like to procede with creating the configuration file to \r\n" +
"store on a Removable drive?" + "\r\n \r\n" +
"IMPORTANT NOTE: Please note the configuration file will be lost by answering No" + "\r\n" +
"to this question, and cannot be regenerated without considerable effort.";

DialogResult result;
result = MessageBox.Show(strMessage,
"IPControl Import Complete",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);

if (result == DialogResult.Yes)
{
//Code for config here
xmlDoc = XDocument.Parse(obj[1].ToString());
XElement Results = XElement.Parse(xmlDoc.ToString());
string strFileName = strContainer + strWAHSuffix + ".txt"; //strContainer NULL?
#region Locate any removable drives
ArrayList alDriveLetter = new ArrayList();
foreach (System.IO.DriveInfo f in System.IO.DriveInfo.GetDrives())
{
if (f.DriveType.ToString() == "Removable")
{
alDriveLetter.Add(f.Name);
}
}
#endregion

if (alDriveLetter.Count == 1)
{
strMessage = "Removable drive " + alDriveLetter[0].ToString() + " was found. Would you " +
"like to save your configuration file to drive " + alDriveLetter[0].ToString() + "? \r\n \r\n" +
"IMPORTANT NOTE: Please note the configuration file will be lost by answering No" + "\r\n" +
"to this question, and cannot be regenerated without considerable effort.";

SetProgressBarMarquee();

result = MessageBox.Show(strMessage,
"Removable Drive Found",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);

if (result == DialogResult.Yes)
{
SW = File.CreateText(alDriveLetter[0].ToString() + strFileName);

foreach (XElement xe in Results.Descendants())
{
if (xe.HasElements)
{
foreach (XElement xeDesc in xe.Descendants())
{
if (xeDesc.Value != null)
{
SW.WriteLine(xeDesc.Value);
}
}
}
}
SW.Close();

SetProgressBarContinuous();

MessageBox.Show(
"Configuration file " + strFileName + " has saved to directory: " + alDriveLetter[0].ToString(),
"File Saved",
MessageBoxButtons.OK,
MessageBoxIcon.Information);

}
else if (result == DialogResult.No)
{
SetProgressBarContinuous();
btnReset.PerformClick();
return;
}
}
else
{
strMessage =
"More than one Removable drive was found, or no removable \r\n" +
"drives were found. After pressing OK, you will need \r\n" +
"to choose a ******** to save your configuration file. \r\n \r\n" +
"IMPORTANT NOTE: Please note the configuration file will be lost by Cancelling" + "\r\n" +
"when selecting your file output ********.";

result = MessageBox.Show(strMessage,
"Choose ******** to save configuration file",
MessageBoxButtons.OK,
MessageBoxIcon.Information);


FolderBrowserDialog fbd = new FolderBrowserDialog();
string strFolderName = string.Empty;
SetProgressBarMarquee();
DialogResult dr = fbd.ShowDialog();


if (dr == DialogResult.OK)
{
strFolderName = fbd.SelectedPath;

SW = File.CreateText(strFolderName + strFileName);

foreach (XElement xe in Results.Descendants())
{
if (xe.HasElements)
{
foreach (XElement xeDesc in xe.Descendants())
{
if (xeDesc.Value != null)
{
SW.WriteLine(xeDesc.Value);
}
}
}
}
SW.Close();

SetProgressBarContinuous();

MessageBox.Show(
"Configuration file " + strFileName + " has saved to directory: " + strFolderName,
"File Saved",
MessageBoxButtons.OK,
MessageBoxIcon.Information);

}
else
{
SetProgressBarContinuous();
return;
}
}

btnReset.PerformClick();
}
break;

case 2: // A case of 2 means that the authentication completely unsuccessfully.

// Begin by setting the progress bar.
SetProgressBarContinuous();

// Next set the cursor.
Cursor.Current = Cursors.Default;

btnReset.PerformClick();

break;
}
}
#endregion WorkerImport_ProgressChanged

#region WorkerImport_RunWorkerCompleted
// This will executed once the DoWork event handler terminates. It will be executed on
// the main thread so it, like the ProgressChanged event can modify controls on the
// main thread.

void WorkerImport_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// If you want to have the progress bar remain filled to 100% for a certain amount
// of time and then clear itself to zero, uncomment the code below, as well as, the
// code found at the bottom of the DoWork event handler.

// Clear the progress bar.
//progressBar1.Style = ProgressBarStyle.Continuous;
//progressBar1.Value = 0;
}
#endregion WorkerImport_RunWorkerCompleted

#region Work From Home field events

private void tbUserName_Enter(object sender, EventArgs e)
{

tbRACFID.Enabled = false;

if (tbRACFID.Text == string.Empty)
{
MessageBox.Show("You must have a RACFID in order to continue.", "RACFID Field Validation", MessageBoxButtons.OK, MessageBoxIcon.Stop);
tbRACFID.Enabled = true;
ff.ControlSetFocus(tbRACFID);
return;
}

EEInfo ee = new EEInfo();
string strName = String.Empty;

Cursor.Current = Cursors.WaitCursor;

try
{
ee.EE(tbRACFID.Text.ToUpper()); // RACFID must be uppercase.

if (ee.StrPreferredFirstName == string.Empty && ee.StrLastName == string.Empty)
{
Cursor.Current = Cursors.Default;

string strMessage =
"The RACFID or LAN ID you entered is not a valid ID. \r\n \r\n" +
"Would you like to continue with the ID you entered?";

DialogResult result;
result = MessageBox.Show(strMessage,
"RACFID/Lan ID not Found",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);

if (result == DialogResult.Yes)
{
tbRACFID.Text = Regex.Replace(tbRACFID.Text, @"[^\w\.]|_", "");
ff.ControlSetFocus(tbUserName);
}
else
{
tbRACFID.SelectAll();
tbRACFID.Enabled = true;
ff.ControlSetFocus(tbRACFID);
}
}
else
{
tbRACFID.Text = Regex.Replace(tbRACFID.Text, @"[^\w\.]|_", "");
tbUserName.Text = ee.StrPreferredFirstName + " " + ee.StrLastName;
ff.ControlSetFocus(cb********);
}
}
catch (Exception ex)
{
Cursor.Current = Cursors.Default;

MessageBox.Show(
"tbUserName_Enter method error. \n\r \n\r" + ex.Message,
"Error in User Name Routine",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}

#endregion

private bool checkForEmptyDataset(DataSet ds)
{
bool booEmpty = false;
if (ds.Tables.Count < 1)
{
uipc = this.Parent as UIPCMainForm;
uipc.Width = 800;
uipc.Height = 600;
booEmpty = true;
}
return booEmpty;
}
private void SetProgressBarMarquee()
{
uipc.progressBar1.Style = ProgressBarStyle.Marquee;
uipc.progressBar1.MarqueeAnimationSpeed = 50;
}
private void SetProgressBarContinuous()
{
uipc.progressBar1.Style = ProgressBarStyle.Continuous;
uipc.progressBar1.Value = 0;
}
}
}


Thank you for taking the time to peruse this post. WHEELS