Beispiel #1
0
        public static void fillCombo(xList fldData, ComboBox frmField, int chosenIdx = 0, bool useTitle = true, bool inclParmData = false)
        {
            int selIdx = 0;

            if (useTitle)
            {
                frmField.Items.Add(fldData.Title);
            }
            else
            {
                chosenIdx--;
            }
            //if (fldData.hasSubList) {
            //    frmField.SelectedIndexChanged += Combo_SelectedIndexChanged;
            //    fldSubFld.Add(frmField.Name, fldData.SubList);
            //    fldSubData.Add(frmField.Name, fldData);
            //}

            for (int Lp1 = 1; Lp1 <= fldData.MaxIdx; Lp1++)
            {
                string thing = fldData.Descr(Lp1);
                if (fldData.Idx(Lp1).Equals(chosenIdx))
                {
                    selIdx = Lp1;
                }
                if (inclParmData)
                {
                    thing += " - " + fldData.Parm(Lp1) + "[" + fldData.Idx(Lp1) + "]";
                }
                frmField.Items.Add(thing);
            }

            //foreach (string province in fldData) {
            //    frmField.Items.Add(province);
            //}

            frmField.SelectedIndex = selIdx;
        }
        private void btnExport_Click(object sender, EventArgs e)
        {
            ListBox lb = (ListBox)tabReportLists.SelectedTab.Controls["listBox"];

            if (lb.SelectedIndex > 0)
            {
                string chosenReport = lb.SelectedItem.ToString();
                string actualReport = chosenReport;
                string idxTab       = (tabReportLists.SelectedIndex - 1).ToString("000") + "-";
                if (_lstReportTitles.ContainsKey(idxTab + chosenReport))
                {
                    actualReport = _lstReportTitles[idxTab + chosenReport];
                }

                string fileFull = xMyProfile.Parm("ReportSuiteFile");

                SaveFileDialog fileSelect = new SaveFileDialog();
                fileSelect.Title = "Navigate to destination folder for export";
                if (Path.IsPathRooted(fileFull))
                {
                    fileSelect.InitialDirectory = Path.GetDirectoryName(fileFull);
                    fileSelect.FileName         = chosenReport;
                }
                fileSelect.DefaultExt = "csv";
                if (fileSelect.ShowDialog() == DialogResult.OK)
                {
                    // commented out as the filename is dependent on the report chosen and this will change a lot.
                    //xMyProfile.Parm("ReportSuiteFile", fileSelect.FileName);
                    //xMyProfile.Save();
                    fileFull = fileSelect.FileName;

                    Cursor.Current = Cursors.WaitCursor;

                    if (actualReport.Length < 6 || actualReport.Substring(0, 6).ToLower() != "select")
                    {
                        actualReport = "Select * from " + actualReport + ";";
                    }

                    adFns         Defns  = new adFns();
                    SqlConnection sqlCxn = new SqlConnection(Defns.cxn(_DBName));
                    sqlCxn.Open();
                    try
                    {
                        SqlCommand    sqlCmd  = new SqlCommand(actualReport, sqlCxn);
                        SqlDataReader sqlRdr  = sqlCmd.ExecuteReader();
                        StreamWriter  csvFile = new StreamWriter(Path.Combine(fileFull));

                        var    csvColNames = Enumerable.Range(0, sqlRdr.FieldCount).Select(sqlRdr.GetName).ToList();
                        string csvRow      = "";
                        foreach (string colTitle in csvColNames)
                        {
                            csvRow += (colTitle.Contains(" ") ? "\"" + colTitle + "\"" : colTitle) + ",";
                        }
                        csvRow = csvRow.Substring(0, csvRow.Length - 1);
                        csvFile.WriteLine(csvRow);

                        while (sqlRdr.Read())
                        {
                            csvRow = "";
                            for (int Lp1 = 0; Lp1 < sqlRdr.FieldCount; ++Lp1)
                            {
                                string value = sqlRdr[Lp1].ToString();
                                if (value.Contains(","))
                                {
                                    value = "\"" + value + "\"";
                                }
                                csvRow += value.Replace(Environment.NewLine, " ") + ",";
                            }
                            csvRow = csvRow.Substring(0, csvRow.Length - 1);
                            csvFile.WriteLine(csvRow);
                        }

                        sqlRdr.Close();

                        csvFile.Close();
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Data Set either broken or does not exist", "File not created", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                    sqlCxn.Close();

                    Cursor.Current = Cursors.Default;
                }
            }
        }