/// <summary>
        /// This is the method that is executed when the BackgroundWorker is activated.
        /// BackgroundWorkers make multi-threading simple and allow the user to do other
        /// things while exporting is occurring. It also provides an intuitive way to display
        /// the progress of the export to the user.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exportBW_DoWork(object sender, DoWorkEventArgs e)
        {
            String[] selection = (String[])e.Argument;

            //Export everything to Word
            if (selection[0].Equals("All"))
            {
                String[] projects;

                string nextProjectSqlText = "SELECT projectName, projectID FROM Project;";
                string countProjectSql = "SELECT COUNT(projectID) FROM Project";

                DBCommand nextProjectSqlCmd = DBConnection.makeCommand(nextProjectSqlText);
                DBCommand countProjectSqlCmd = DBConnection.makeCommand(countProjectSql);

                SqlCeDataReader countProjectSqlReader = countProjectSqlCmd.Start();
                countProjectSqlReader.Read();
                projects = new String[countProjectSqlReader.GetInt32(0)]; //Initializes array
                countProjectSqlCmd.Stop(); //Stops command.

                SqlCeDataReader nextProjectSqlReader = nextProjectSqlCmd.Start();

                int i = 0;
                while (nextProjectSqlReader.Read())
                {
                    projects[i] = nextProjectSqlReader.GetString(0);
                    i++;
                }
                nextProjectSqlCmd.Stop(); //Stops command.

                for (int j = 0; j < projects.Length; j++)
                {
                    if (!exportProject("TMS_Export\\" + projects[j]))
                    {
                        e.Cancel = true;
                        break;
                    }

                    exportBW.ReportProgress((100 * (j + 1)) / projects.Length);

                    string getProjectIDText = "SELECT projectID FROM Project WHERE projectName='" + projects[j] + "';";
                    DBCommand getProjectIDCmd = DBConnection.makeCommand(getProjectIDText);
                    SqlCeDataReader getProjectIDReader = getProjectIDCmd.Start();
                    getProjectIDReader.Read();
                    int projectID = getProjectIDReader.GetInt32(0);

                    string getVersionSqlText = "SELECT versionNumber FROM Version WHERE project=" + projectID + ";";
                    DBCommand getVersionSqlCmd = DBConnection.makeCommand(getVersionSqlText);
                    SqlCeDataReader getVersionSqlReader = getVersionSqlCmd.Start();

                    while (getVersionSqlReader.Read())
                    {
                        double versionNum = (Double)getVersionSqlReader.GetSqlDouble(0);
                        exportVersion("TMS_Export\\" + projects[j], "Version " + versionNum);

                        string getVersionText = "SELECT versionID FROM Version WHERE versionNumber=" + versionNum + " AND project=" + projectID + ";";
                        DBCommand getVersionCmd = DBConnection.makeCommand(getVersionText);
                        SqlCeDataReader getVersionReader = getVersionCmd.Start();
                        getVersionReader.Read();
                        int versionID = getVersionReader.GetInt32(0);

                        string getFilesSqlText = "SELECT documentName FROM Document WHERE versionID=" + versionID + ";";
                        DBCommand getFilesSqlCmd = DBConnection.makeCommand(getFilesSqlText);
                        SqlCeDataReader getFilesSqlReader = getFilesSqlCmd.Start();
                        while (getFilesSqlReader.Read())
                        {
                            string documentName = getFilesSqlReader.GetString(0);

                            Serialization saveDocument = new Serialization();

                            string getTemplateTypeSqlText = "SELECT documentType FROM Document WHERE documentName='" + documentName + "' AND versionID=" + versionID + ";";
                            DBCommand getTemplateTypeSqlCmd = DBConnection.makeCommand(getTemplateTypeSqlText);
                            SqlCeDataReader getTemplateTypeSqlReader = getTemplateTypeSqlCmd.Start();
                            String templateType = "";
                            while (getTemplateTypeSqlReader.Read())
                            {
                                templateType += getTemplateTypeSqlReader.GetString(0);
                            }

                            string getTemplateSqlText = "SELECT templateData FROM Template WHERE templateName='" + templateType + "';";
                            DBCommand getTemplateSqlCmd = DBConnection.makeCommand(getTemplateSqlText);
                            SqlCeDataReader getTemplateSqlReader = getTemplateSqlCmd.Start();
                            String templateInput = "";
                            while (getTemplateSqlReader.Read())
                            {
                                templateInput += getTemplateSqlReader.GetString(0);
                            }

                            //Testing the deSerialize method.
                            TemplateStorage templateStorage = new TemplateStorage();
                            templateStorage = (TemplateStorage)saveDocument.deSerialize(templateStorage, templateInput);
                            List<string> genericList = new List<string>(templateStorage.Componentlist);
                            string getDocSqlText = "SELECT data FROM Document, Version WHERE documentName='" + documentName + "' AND Document.versionID=" + versionID + ";";
                            DBCommand getDocSqlCmd = DBConnection.makeCommand(getDocSqlText);
                            SqlCeDataReader getDocSqlReader = getDocSqlCmd.Start();
                            String documentData = "";

                            getDocSqlReader.Read();
                            documentData += getDocSqlReader.GetString(0);

                            while (getDocSqlReader.Read())
                            {

                            }
                            DocumentStorage documentStorage = new DocumentStorage();
                            documentStorage = (DocumentStorage)saveDocument.deSerialize(documentStorage, documentData);

                            string[] controlData = documentStorage.DocumentData;

                            exportFileWord(documentName, templateStorage.Componentlist, documentStorage.DocumentData, this.path + "\\TMS_Export\\" + projects[j] + "\\Version " + versionNum + "\\" + documentName);

                        }
                    }
                }
            }
            else
            {
                //Export specific project
                string nextProjectSqlText = "SELECT projectName, projectID FROM Project WHERE projectName='" + selection[0] + "';";
                DBCommand nextProjectSqlCmd = DBConnection.makeCommand(nextProjectSqlText);
                SqlCeDataReader nextProjectSqlReader = nextProjectSqlCmd.Start();
                nextProjectSqlReader.Read();
                String project = nextProjectSqlReader.GetString(0);
                int projectID = nextProjectSqlReader.GetInt32(1);

                bool directoryExists;

                if (selection[1].Equals("All"))
                {

                    directoryExists = exportProject(project);

                    if (!directoryExists)
                    {
                        e.Cancel = true;
                    }

                    string getVersionSqlText = "SELECT versionNumber FROM Version WHERE project=" + projectID + ";";
                    DBCommand getVersionSqlCmd = DBConnection.makeCommand(getVersionSqlText);
                    SqlCeDataReader getVersionSqlReader = getVersionSqlCmd.Start();

                    string getVersionSqlCountText = "SELECT COUNT(versionID) FROM Version WHERE project=" + projectID + ";";
                    DBCommand getVersionSqlCountCmd = DBConnection.makeCommand(getVersionSqlCountText);
                    SqlCeDataReader getVersionSqlCountReader = getVersionSqlCountCmd.Start();
                    getVersionSqlCountReader.Read();
                    int numberOfVersions = getVersionSqlCountReader.GetInt32(0);
                    int count = 0;

                    while (getVersionSqlReader.Read())
                    {
                        double versionNum = (Double)getVersionSqlReader.GetSqlDouble(0);

                        if (!exportVersion(project, "Version " + versionNum))
                        {
                            e.Cancel = true;
                            break;
                        }

                        exportBW.ReportProgress((100 * (count + 1)) / numberOfVersions);
                        count++;

                        string getVersionText = "SELECT versionID FROM Version WHERE versionNumber=" + versionNum + " AND project=" + projectID + ";";
                        DBCommand getVersionCmd = DBConnection.makeCommand(getVersionText);
                        SqlCeDataReader getVersionReader = getVersionCmd.Start();
                        getVersionReader.Read();
                        int versionID = getVersionReader.GetInt32(0);

                        string getFilesSqlText = "SELECT documentName FROM Document WHERE versionID=" + versionID + ";";
                        DBCommand getFilesSqlCmd = DBConnection.makeCommand(getFilesSqlText);
                        SqlCeDataReader getFilesSqlReader = getFilesSqlCmd.Start();
                        while (getFilesSqlReader.Read())
                        {
                            string documentName = getFilesSqlReader.GetString(0);

                            Serialization saveDocument = new Serialization();

                            string getTemplateTypeSqlText = "SELECT documentType FROM Document WHERE documentName='" + documentName + "' AND versionID=" + versionID + ";";
                            DBCommand getTemplateTypeSqlCmd = DBConnection.makeCommand(getTemplateTypeSqlText);
                            SqlCeDataReader getTemplateTypeSqlReader = getTemplateTypeSqlCmd.Start();
                            String templateType = "";
                            while (getTemplateTypeSqlReader.Read())
                            {
                                templateType += getTemplateTypeSqlReader.GetString(0);
                            }

                            string getTemplateSqlText = "SELECT templateData FROM Template WHERE templateName='" + templateType + "';";
                            DBCommand getTemplateSqlCmd = DBConnection.makeCommand(getTemplateSqlText);
                            SqlCeDataReader getTemplateSqlReader = getTemplateSqlCmd.Start();
                            String templateInput = "";
                            while (getTemplateSqlReader.Read())
                            {
                                templateInput += getTemplateSqlReader.GetString(0);
                            }

                            //Testing the deSerialize method.
                            TemplateStorage templateStorage = new TemplateStorage();
                            templateStorage = (TemplateStorage)saveDocument.deSerialize(templateStorage, templateInput);
                            List<string> genericList = new List<string>(templateStorage.Componentlist);
                            string getDocSqlText = "SELECT data FROM Document, Version WHERE documentName='" + documentName + "' AND Document.versionID=" + versionID + ";";
                            DBCommand getDocSqlCmd = DBConnection.makeCommand(getDocSqlText);
                            SqlCeDataReader getDocSqlReader = getDocSqlCmd.Start();
                            String documentData = "";

                            getDocSqlReader.Read();
                            documentData += getDocSqlReader.GetString(0);

                            while (getDocSqlReader.Read())
                            {

                            }
                            DocumentStorage documentStorage = new DocumentStorage();
                            documentStorage = (DocumentStorage)saveDocument.deSerialize(documentStorage, documentData);

                            string[] controlData = documentStorage.DocumentData;

                            exportFileWord(documentName, templateStorage.Componentlist, documentStorage.DocumentData, this.path + "\\" + project + "\\Version " + versionNum + "\\" + documentName);

                        }
                    }
                }
                else
                {
                    //Export specific version here
                    string getVersionSqlText = "SELECT versionNumber, versionID FROM Version WHERE project=" + projectID + " and versionNumber=" + selection[1].Substring(7) + ";";
                    DBCommand getVersionSqlCmd = DBConnection.makeCommand(getVersionSqlText);
                    SqlCeDataReader getVersionSqlReader = getVersionSqlCmd.Start();
                    getVersionSqlReader.Read();
                    Double versionNum = (Double)getVersionSqlReader.GetSqlDouble(0);
                    int versionID = getVersionSqlReader.GetInt32(1);

                    if (selection[2].Equals("All"))
                    {
                        directoryExists = exportVersion(project, "Version " + versionNum);

                        if (!directoryExists)
                        {
                            e.Cancel = true;
                        }

                        //Export all files for specific version here
                        string getFilesSqlText = "SELECT documentName FROM Document WHERE versionID=" + versionID + ";";
                        DBCommand getFilesSqlCmd = DBConnection.makeCommand(getFilesSqlText);
                        SqlCeDataReader getFilesSqlReader = getFilesSqlCmd.Start();

                        string getFileCountSqlText = "SELECT COUNT(documentID) FROM Document WHERE versionID=" + versionID + ";";
                        DBCommand getFileCountSqlCmd = DBConnection.makeCommand(getFileCountSqlText);
                        SqlCeDataReader getFileCountSqlReader = getFileCountSqlCmd.Start();
                        getFileCountSqlReader.Read();
                        int docCount = getFileCountSqlReader.GetInt32(0);
                        int count = 0;

                        while (getFilesSqlReader.Read())
                        {
                            exportBW.ReportProgress((100 * (count + 1)) / docCount);
                            count++;

                            string documentName = getFilesSqlReader.GetString(0);

                            Serialization saveDocument = new Serialization();

                            string getTemplateTypeSqlText = "SELECT documentType FROM Document WHERE documentName='" + documentName + "' AND versionID=" + versionID + ";";
                            DBCommand getTemplateTypeSqlCmd = DBConnection.makeCommand(getTemplateTypeSqlText);
                            SqlCeDataReader getTemplateTypeSqlReader = getTemplateTypeSqlCmd.Start();
                            String templateType = "";
                            while (getTemplateTypeSqlReader.Read())
                            {
                                templateType += getTemplateTypeSqlReader.GetString(0);
                            }

                            string getTemplateSqlText = "SELECT templateData FROM Template WHERE templateName='" + templateType + "';";
                            DBCommand getTemplateSqlCmd = DBConnection.makeCommand(getTemplateSqlText);
                            SqlCeDataReader getTemplateSqlReader = getTemplateSqlCmd.Start();
                            String templateInput = "";
                            while (getTemplateSqlReader.Read())
                            {
                                templateInput += getTemplateSqlReader.GetString(0);
                            }

                            //Testing the deSerialize method.
                            TemplateStorage templateStorage = new TemplateStorage();
                            templateStorage = (TemplateStorage)saveDocument.deSerialize(templateStorage, templateInput);
                            List<string> genericList = new List<string>(templateStorage.Componentlist);
                            string getDocSqlText = "SELECT data FROM Document, Version WHERE documentName='" + documentName + "' AND Document.versionID=" + versionID + ";";
                            DBCommand getDocSqlCmd = DBConnection.makeCommand(getDocSqlText);
                            SqlCeDataReader getDocSqlReader = getDocSqlCmd.Start();
                            String documentData = "";

                            getDocSqlReader.Read();
                            documentData += getDocSqlReader.GetString(0);

                            while (getDocSqlReader.Read())
                            {

                            }
                            DocumentStorage documentStorage = new DocumentStorage();
                            documentStorage = (DocumentStorage)saveDocument.deSerialize(documentStorage, documentData);

                            string[] controlData = documentStorage.DocumentData;

                            exportFileWord(documentName, templateStorage.Componentlist, documentStorage.DocumentData, this.path + "\\" + project + "\\Version " + versionNum + "\\" + documentName);

                        }
                    }
                    else
                    {
                        //Export specific file here
                        //Export all files for specific version here
                        string getFileSqlText = "SELECT documentName FROM Document WHERE versionID=" + versionID + " AND documentName='" + selection[2] + "';";
                        DBCommand getFileSqlCmd = DBConnection.makeCommand(getFileSqlText);
                        SqlCeDataReader getFileSqlReader = getFileSqlCmd.Start();

                        while (getFileSqlReader.Read())
                        {

                            string documentName = getFileSqlReader.GetString(0);

                            Serialization saveDocument = new Serialization();

                            string getTemplateTypeSqlText = "SELECT documentType FROM Document WHERE documentName='" + documentName + "' AND versionID=" + versionID + ";";
                            DBCommand getTemplateTypeSqlCmd = DBConnection.makeCommand(getTemplateTypeSqlText);
                            SqlCeDataReader getTemplateTypeSqlReader = getTemplateTypeSqlCmd.Start();
                            String templateType = "";
                            while (getTemplateTypeSqlReader.Read())
                            {
                                templateType += getTemplateTypeSqlReader.GetString(0);
                            }

                            string getTemplateSqlText = "SELECT templateData FROM Template WHERE templateName='" + templateType + "';";
                            DBCommand getTemplateSqlCmd = DBConnection.makeCommand(getTemplateSqlText);
                            SqlCeDataReader getTemplateSqlReader = getTemplateSqlCmd.Start();
                            String templateInput = "";
                            while (getTemplateSqlReader.Read())
                            {
                                templateInput += getTemplateSqlReader.GetString(0);
                            }

                            //Testing the deSerialize method.
                            TemplateStorage templateStorage = new TemplateStorage();
                            templateStorage = (TemplateStorage)saveDocument.deSerialize(templateStorage, templateInput);
                            List<string> genericList = new List<string>(templateStorage.Componentlist);
                            string getDocSqlText = "SELECT data FROM Document, Version WHERE documentName='" + documentName + "' AND Document.versionID=" + versionID + ";";
                            DBCommand getDocSqlCmd = DBConnection.makeCommand(getDocSqlText);
                            SqlCeDataReader getDocSqlReader = getDocSqlCmd.Start();
                            String documentData = "";

                            getDocSqlReader.Read();
                            documentData += getDocSqlReader.GetString(0);

                            while (getDocSqlReader.Read())
                            {

                            }
                            DocumentStorage documentStorage = new DocumentStorage();
                            documentStorage = (DocumentStorage)saveDocument.deSerialize(documentStorage, documentData);

                            string[] controlData = documentStorage.DocumentData;

                            exportFileWord(documentName, templateStorage.Componentlist, documentStorage.DocumentData, this.path + "\\" + documentName);

                            exportBW.ReportProgress(100);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Gets the document's data and the associated template and then generates the document for the user to view.
        /// </summary>
        private void getTemplate()
        {
            if (projectTreeView.SelectedNode.Nodes.Count == 0 && projectTreeView.SelectedNode.Text != "Documents" && projectTreeView.SelectedNode.Level != 0)
            {
                newDocument = false;
                Serialization saveDocument = new Serialization();

                string getVersionIDText = "SELECT Version.versionID, Project.projectID FROM Version, Project WHERE Version.versionNumber=" + projectTreeView.SelectedNode.Parent.Parent.Text.Substring(7) + " AND Project.projectName='" + projectTreeView.SelectedNode.Parent.Parent.Parent.Text + "' AND Version.project = Project.projectID;";
                DBCommand getVersionIDCmd = DBConnection.makeCommand(getVersionIDText);
                SqlCeDataReader getVersionIDReader = getVersionIDCmd.Start();
                int versionID;
                int projectID;
                getVersionIDReader.Read();
                versionID = getVersionIDReader.GetInt32(0);
                projectID = getVersionIDReader.GetInt32(1);

                string getTemplateTypeSqlText = "SELECT documentType FROM Document WHERE documentName='" + projectTreeView.SelectedNode.Text + "' AND versionID=" + versionID + ";";
                DBCommand getTemplateTypeSqlCmd = DBConnection.makeCommand(getTemplateTypeSqlText);
                SqlCeDataReader getTemplateTypeSqlReader = getTemplateTypeSqlCmd.Start();
                String templateType = "";
                while (getTemplateTypeSqlReader.Read())
                {
                    templateType += getTemplateTypeSqlReader.GetString(0);
                }

                string getTemplateSqlText = "SELECT templateData FROM Template WHERE templateName='" + templateType + "';";
                DBCommand getTemplateSqlCmd = DBConnection.makeCommand(getTemplateSqlText);
                SqlCeDataReader getTemplateSqlReader = getTemplateSqlCmd.Start();
                String templateInput = "";
                while (getTemplateSqlReader.Read())
                {
                    templateInput += getTemplateSqlReader.GetString(0);
                }
                //Testing the deSerialize method.
                TemplateStorage templateStorage = new TemplateStorage();
                templateStorage = (TemplateStorage)saveDocument.deSerialize(templateStorage, templateInput);
                this.templateStorage = templateStorage;
                List<string> genericList = new List<string>(templateStorage.Componentlist);

                DocumentFactory df = new DocumentFactory();
                outputPanel.Controls.Clear();
                outputPanel.Controls.Add(df.createDocument(templateStorage.Name, templateStorage.Version, genericList));
                templateControlToolStrip.Enabled = true;

                string getDocSqlText = "SELECT documentID, data FROM Document, Version WHERE documentName='" + projectTreeView.SelectedNode.Text + "' AND Document.versionID=" + versionID + ";";
                DBCommand getDocSqlCmd = DBConnection.makeCommand(getDocSqlText);
                SqlCeDataReader getDocSqlReader = getDocSqlCmd.Start();
                String documentData = "";

                getDocSqlReader.Read();
                this.documentID = (int)getDocSqlReader.GetSqlInt32(0);
                documentData += getDocSqlReader.GetString(1);

                while (getDocSqlReader.Read())
                {

                }
                DocumentStorage documentStorage = new DocumentStorage();
                documentStorage = (DocumentStorage)saveDocument.deSerialize(documentStorage, documentData);

                string[] controlData = documentStorage.DocumentData;
                if (controlData != null)
                {
                    int counter = 0;
                    for (int i = 0; i < outputPanel.Controls[0].Controls[2].Controls.Count; i++)
                    {
                        if (i % 2 != 0)
                        {
                            outputPanel.Controls[0].Controls[2].Controls[i].Text = controlData[counter];
                            counter++;
                        }
                    }
                }
            }
        }
        private void useTemplateButton_Click(object sender, EventArgs e)
        {
            exampleOutputPanel.Controls.Clear();
            if (templateListView.SelectedItems.Count > 0)
            {
                newDocument = true;
                string nextSqlText = "SELECT templateData FROM Template WHERE templateName='" + templateListView.SelectedItems[0].Text + "';";
                DBCommand nextSqlCmd = DBConnection.makeCommand(nextSqlText);
                SqlCeDataReader nextSqlReader = nextSqlCmd.Start();
                String input = "";
                while (nextSqlReader.Read())
                {
                    input += nextSqlReader.GetString(0);
                }

                nextSqlCmd.Stop(); //Stops command.
                //Testing the deSerialize method.
                TemplateStorage templateStorage = new TemplateStorage();
                Serialization saveDocument = new Serialization();
                templateStorage = (TemplateStorage)saveDocument.deSerialize(templateStorage, input);
                this.templateStorage = templateStorage;
                List<string> genericList = new List<string>(templateStorage.Componentlist);

                DocumentFactory df = new DocumentFactory();
                outputPanel.Controls.Clear();
                outputPanel.Controls.Add(df.createDocument(templateStorage.Name, templateStorage.Description, genericList));
                templateControlToolStrip.Enabled = true;

                string next1SqlText = "SELECT documentID FROM Document WHERE documentType='" + templateListView.SelectedItems[0].Text + "';";
                DBCommand next1SqlCmd = DBConnection.makeCommand(next1SqlText);
                SqlCeDataReader next1SqlReader = next1SqlCmd.Start();
                int input1=0;
                if (next1SqlReader.Read())
                {
                    input1 = next1SqlReader.GetInt32(0);
                }
                                nextSqlCmd.Stop(); //Stops command.
                if (input1 == 0)
                {
                    Label noTemp = new Label();
                    noTemp.AutoSize = true;
                    noTemp.Text = "The Template has not been used.";
                    exampleOutputPanel.Controls.Add(noTemp);
                }
                else
                {
                    getExample(input1, templateListView.SelectedItems[0].Text);
                }
            }
        }
        private void getExample(int documentID, String docuType)
        {
            Serialization saveDocument = new Serialization();

                string getTemplateSqlText = "SELECT templateData FROM Template WHERE templateName='" + docuType + "';";
                DBCommand getTemplateSqlCmd = DBConnection.makeCommand(getTemplateSqlText);
                SqlCeDataReader getTemplateSqlReader = getTemplateSqlCmd.Start();
                String templateInput = "";
                while (getTemplateSqlReader.Read())
                {
                    templateInput += getTemplateSqlReader.GetString(0);
                }
                //Testing the deSerialize method.
                TemplateStorage templateStorage = new TemplateStorage();
                templateStorage = (TemplateStorage)saveDocument.deSerialize(templateStorage, templateInput);
                this.templateStorage = templateStorage;
                List<string> genericList = new List<string>(templateStorage.Componentlist);

                DocumentFactory df = new DocumentFactory();
                exampleOutputPanel.Controls.Clear();
                exampleOutputPanel.Controls.Add(df.createDocument(templateStorage.Name, templateStorage.Version, genericList));

                string getDocSqlText = "SELECT data FROM Document WHERE documentID='" + documentID + "';";
                DBCommand getDocSqlCmd = DBConnection.makeCommand(getDocSqlText);
                SqlCeDataReader getDocSqlReader = getDocSqlCmd.Start();
                String documentData = "";

                getDocSqlReader.Read();
                documentData += getDocSqlReader.GetString(0);

                DocumentStorage documentStorage = new DocumentStorage();
                documentStorage = (DocumentStorage)saveDocument.deSerialize(documentStorage, documentData);

                string[] controlData = documentStorage.DocumentData;
                if (controlData != null)
                {
                    int counter = 0;
                    for (int i = 0; i < outputPanel.Controls[0].Controls[2].Controls.Count; i++)
                    {
                        if (i % 2 != 0)
                        {
                            exampleOutputPanel.Controls[0].Controls[2].Controls[i].Text = controlData[counter];
                            counter++;
                        }
                    }
                }
        }