// Write out event methods for the page events // event handler for Button with Layout public void ImportButton_Click_Base(object sender, EventArgs args) { BaseTable bt = this.GetTable(); BaseClasses.Utils.ImportedResults recordsImported = null; try { // Get the column names list and importChkBox list. Table disp = (Table)this.Page.FindControl("DisplayTable"); int cellcount = disp.Rows[0].Cells.Count; ArrayList ddListSelected = new ArrayList(); ArrayList chkBoxList = new ArrayList(); for (int rc = 0; rc < cellcount - 1; rc++) { DropDownList dd = (DropDownList)(disp.FindControl("dropDownList" + rc)); if (ddListSelected.Contains(dd.SelectedValue) && dd.SelectedValue != "") //&& this.ColumnsList.SelectedValue != DDLIST_TEXT) { MiscUtils.RegisterJScriptAlert(this, "Duplicate Column", this.GetResourceValue("Import:DuplicateColumn", this.AppName).Replace("{ColumnName}", dd.SelectedValue)); return; } ddListSelected.Add(dd.SelectedValue); CheckBox chkBox = (CheckBox)(disp.FindControl("importChkBox" + rc)); chkBoxList.Add(chkBox); } // Validate selected columns. if (!validateColumns(ddListSelected, chkBoxList, bt)) { return; } ImportDataItems imp = new ImportDataItems(this.FilePath, GetFileType(), bt, ddListSelected, chkBoxList); recordsImported = imp.ImportRecords(this.ImportFirstRowCheckBox.Checked, this.ImportResolveForeignKeys.Checked); } catch (Exception e) { AjaxControlToolkit.ToolkitScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "ErrorMsg", "alert(" + BaseClasses.Web.AspxTextWriter.CreateJScriptStringLiteral(e.Message) + ");", true); } if (recordsImported == null) { recordsImported = new BaseClasses.Utils.ImportedResults(); } string script = this.ConstructScriptForSkippedRecords(recordsImported); if (script.Contains("<script>")) { ClientScript.RegisterStartupScript(Page.GetType(), "CloseWindow", script); } else { WriteTextFileForSkippedRecords(script); } }
/// <summary> /// Reads rows of values for CSV file and import it to Database. /// </summary> public BaseClasses.Utils.ImportedResults ImportRecords(bool isImportFirstRowChecked, bool isResolvedForeignKeysChecked) { Parser parsr = null; BaseClasses.Utils.ImportedResults results = new BaseClasses.Utils.ImportedResults(); try { if (this.FilePath == null || this.DBTable == null || this.ColumnNameList == null || this.ImportList == null) { return(results); } DbUtils.StartTransaction(); parsr = Parser.GetParser(this.FilePath, this.FileType); string[] rowValues = parsr.GetNextRow(); // get the first row if ((!isImportFirstRowChecked) && rowValues != null) { try { DoImport(rowValues, isResolvedForeignKeysChecked); RowCount++; } catch (Exception ex) { results.NumberOfSkipped += 1; this.AddSkippedRecordToList(results.ListOfSkipped, RowCount + results.NumberOfSkipped, rowValues, ex.Message); } } while (rowValues != null) { rowValues = parsr.GetNextRow(); if (rowValues != null) { try { DoImport(rowValues, isResolvedForeignKeysChecked); RowCount++; } catch (Exception ex) { results.NumberOfSkipped += 1; this.AddSkippedRecordToList(results.ListOfSkipped, RowCount + results.NumberOfSkipped, rowValues, ex.Message); } } } DbUtils.CommitTransaction(); parsr.Close(); } catch (Exception e) { DbUtils.RollBackTransaction(); results.NumberOfImported = 0; parsr.Close(); string errorMsg = e.Message; if (File.Exists(this.FilePath)) { File.Delete(this.FilePath); } errorMsg = string.Format("- " + errorMsg + Environment.NewLine + "- Import error occurred at Row = {0},Column = {1}", RowCount, ColumnCount); throw new Exception(errorMsg); } finally { DbUtils.EndTransaction(); if (File.Exists(this.FilePath)) { File.Delete(this.FilePath); } } results.NumberOfImported = NumberOfRecordImported; return(results); }
/// <summary> /// Reads rows of values for CSV file and import it to Database. /// </summary> public BaseClasses.Utils.ImportedResults ImportRecords(bool isImportFirstRowChecked, bool isResolvedForeignKeysChecked) { Parser parsr = null; BaseClasses.Utils.ImportedResults results = new BaseClasses.Utils.ImportedResults(); try { if (this.FilePath == null || this.DBTable == null || this.ColumnNameList == null || this.ImportList == null) return results; DbUtils.StartTransaction(); parsr = Parser.GetParser(this.FilePath, this.FileType); string[] rowValues = parsr.GetNextRow(); // get the first row if ((!isImportFirstRowChecked) && rowValues != null) { try { DoImport(rowValues, isResolvedForeignKeysChecked); RowCount++; } catch (Exception ex) { results.NumberOfSkipped += 1; this.AddSkippedRecordToList(results.ListOfSkipped, RowCount + results.NumberOfSkipped, rowValues, ex.Message); } } while (rowValues != null) { rowValues = parsr.GetNextRow(); if (rowValues != null) { try { DoImport(rowValues, isResolvedForeignKeysChecked); RowCount++; } catch (Exception ex) { results.NumberOfSkipped += 1; this.AddSkippedRecordToList(results.ListOfSkipped, RowCount + results.NumberOfSkipped, rowValues, ex.Message); } } } DbUtils.CommitTransaction(); parsr.Close(); } catch (Exception e) { DbUtils.RollBackTransaction(); results.NumberOfImported = 0; parsr.Close(); string errorMsg = e.Message; if (File.Exists(this.FilePath)) { File.Delete(this.FilePath); } errorMsg = string.Format("- " + errorMsg + Environment.NewLine + "- Import error occurred at Row = {0},Column = {1}", RowCount, ColumnCount); throw new Exception(errorMsg); } finally { DbUtils.EndTransaction(); if (File.Exists(this.FilePath)) { File.Delete(this.FilePath); } } results.NumberOfImported = NumberOfRecordImported; return results; }
private string ConstructScriptForSkippedRecords(BaseClasses.Utils.ImportedResults recordsImported) { string msg = this.GetResourceValue("Import:Success", this.AppName).Replace("{Records}", recordsImported.NumberOfImported.ToString()); string msg2 = this.GetResourceValue("Import:Skipped", this.AppName).Replace("{Records}", recordsImported.NumberOfSkipped.ToString()); string script = ""; int symbolsCount = 0; script += "<script>"; if (recordsImported.NumberOfSkipped > 0) { script += "CloseWindow('" + msg + "','" + msg2; string msg3 = this.GetResourceValue("Import:FirstSkippedRecords", this.AppName); symbolsCount += msg.Length + msg2.Length + msg3.Length; string linesMsg = ""; int index = 0; foreach (BaseClasses.Utils.SkippedLine rec in recordsImported.ListOfSkipped) { string newLineEntry = ""; if (recordsImported.NumberOfSkipped > SKIPTHRESHOLD || (recordsImported.NumberOfSkipped > 0 && recordsImported.NumberOfImported > 0)) { newLineEntry = this.ConstructLineEntryForTextFile(rec, MAXIMUMCHAR); } else { newLineEntry = this.ConstructLineEntry(rec, MAXIMUMCHAR); } if (newLineEntry.Length + symbolsCount < CHARLIMIT) { linesMsg += newLineEntry; symbolsCount += newLineEntry.Length; index += 1; } else { break; } } if (index < recordsImported.ListOfSkipped.Count) { msg3 = this.GetResourceValue("Import:FirstSkippedRecords", this.AppName).Replace("{Number}", index.ToString()); } else { msg3 = this.GetResourceValue("Import:SkippedRecords", this.AppName); } if (recordsImported.NumberOfSkipped > SKIPTHRESHOLD || (recordsImported.NumberOfSkipped > 0 && recordsImported.NumberOfImported > 0)) { script = ""; if (recordsImported.NumberOfImported > 0) { script += msg + "\r\n"; } script += msg3 + "\r\n"; script += linesMsg; return(script); } else { script += "\\n" + "\\n" + msg3; script += linesMsg; script += "');"; } } else { script += "CloseWindow('" + msg + "','" + msg2 + "');"; } script += "</" + "script>"; return(script); }