protected void buttonReadSourceFile_Click(object sender, EventArgs e) { try { if (FileUploadCSV.HasFile) { StockManager stockManager = new StockManager(); DataTable sourceData = null; ViewState["SOURCEDATA"] = null; int fileSize = FileUploadCSV.PostedFile.ContentLength; ViewState["SOURCE_FILE_NAME"] = FileUploadCSV.FileName; textboxPortfolioName.Text = FileUploadCSV.FileName.Split('.')[0]; if (fileSize <= 2097152) { // If file size is greater than 2 MB if ((ddlfiletype.SelectedIndex == 1) && (ddlFirstLine.SelectedIndex > 0) && (ddlFieldSeparator.SelectedIndex > 0)) { //text based file Stream receiveStream = FileUploadCSV.FileContent; StreamReader reader = null; reader = new StreamReader(receiveStream); char separatorChar = ddlFieldSeparator.SelectedIndex == 1 ? ',' : ddlFieldSeparator.SelectedIndex == 2 ? '|' : '\t'; sourceData = stockManager.readSourceCSV(reader, System.Convert.ToBoolean(ddlFirstLine.SelectedValue), separatorChar); if ((sourceData != null) && (sourceData.Rows.Count > 0)) { ViewState["SOURCEDATA"] = sourceData; } } else if ((ddlfiletype.SelectedIndex == 2) && (ddlFirstLine.SelectedIndex > 0) && (string.IsNullOrEmpty(textboxWorksheetName.Text) == false)) { string uploadedFileName = Server.MapPath("~/tempfiles/") + Session["EMAILID"].ToString() + "_" + FileUploadCSV.FileName; FileUploadCSV.SaveAs(uploadedFileName); sourceData = stockManager.ReadExcelFile(uploadedFileName, textboxWorksheetName.Text, System.Convert.ToBoolean(ddlFirstLine.SelectedValue)); if ((sourceData != null) && (sourceData.Rows.Count > 0)) { ViewState["SOURCEDATA"] = sourceData; } } } else { Page.ClientScript.RegisterStartupScript(GetType(), "myScript", "alert('File size must be less than or equal to 2MB');", true); } if (ViewState["SOURCEDATA"] != null) { panelSourceFileData.Enabled = true; panelSourceFileData.Visible = true; gvSource.DataSource = sourceData; gvSource.DataBind(); //LoadColumnMappers(); LoadSourceTargetColumns(); ShowColumnMapperGrid(); panelValidateSymbols.Enabled = true; panelValidateSymbols.Visible = true; } } else { Page.ClientScript.RegisterStartupScript(GetType(), "myScript", "alert('Please select file, file type and first line contents');", true); } } catch (Exception ex) { Page.ClientScript.RegisterStartupScript(GetType(), "myScript", "alert('" + ex.Message + "');", true); } }
protected void ButtonUpload_Click(object sender, EventArgs e) { try { DataTable csvTable, csvColTable; if (FileUploadCSV.HasFile) { // Get the file extension string fileExtension = System.IO.Path.GetExtension(FileUploadCSV.FileName); if (fileExtension.ToLower() != ".csv") { labelMessage.ForeColor = System.Drawing.Color.Red; labelMessage.Text = "Only text file files with .csv extension are allowed"; } else { // Get the file size int fileSize = FileUploadCSV.PostedFile.ContentLength; // If file size is greater than 2 MB if (fileSize > 2097152) { labelMessage.ForeColor = System.Drawing.Color.Red; labelMessage.Text = "File size cannot be greater than 2 MB"; } else { // Upload the file //string fileName = Session["PortfolioFolder"].ToString() + "\\" + textboxPortfolioName.Text + ".xml"; //FileUploadCSV.SaveAs(Server.MapPath("~/Uploads/" + FileUploadCSV.FileName)); ViewState["UploadedFileName"] = Session["PortfolioFolder"].ToString() + "\\" + FileUploadCSV.FileName; FileUploadCSV.SaveAs(Session["PortfolioFolder"].ToString() + "\\" + FileUploadCSV.FileName); labelMessage.ForeColor = System.Drawing.Color.Green; labelMessage.Text = "File uploaded successfully. Please map the fields below to create portfolio."; Stream receiveStream = FileUploadCSV.FileContent; StreamReader reader = null; reader = new StreamReader(receiveStream); //string fileData = reader.ReadToEnd(); //textboxMessage.Text = fileData; csvTable = StockApi.readCSV(reader); if (csvTable != null) { ViewState["CSVData"] = csvTable; ViewState["CSVGridData"] = csvTable; //if(GridViewData.Columns.Count > 0) //{ // GridViewData.Columns.Clear(); //} //foreach (DataColumn col in csvTable.Columns) //{ // BoundField newcol = new BoundField(); // newcol.DataField = col.ColumnName; // newcol.HeaderText = col.ColumnName; // GridViewData.Columns.Add(newcol); //} //GridViewData.DataSource = csvTable; //GridViewData.DataBind(); csvColTable = StockApi.readColumnsFromCSVTable(csvTable); if (csvColTable != null) { ViewState["CSVColData"] = csvColTable; ddlSourceCols.Items.Clear(); foreach (DataRow row in csvColTable.Rows) { ListItem li = new ListItem(row[0].ToString(), row[0].ToString()); ddlSourceCols.Items.Add(li); } GridViewMapping.DataSource = csvColTable; GridViewMapping.DataBind(); } else { labelMessage.ForeColor = System.Drawing.Color.Red; labelMessage.Text = "Problem while mapping source columns. Please select correct CSV file."; buttonMapSelected.Enabled = false; } buttonMapSelected.Enabled = true; } else { labelMessage.ForeColor = System.Drawing.Color.Red; labelMessage.Text = "Problem while reading data. Please select correct CSV file."; buttonMapSelected.Enabled = false; } } } } else { labelMessage.ForeColor = System.Drawing.Color.Red; labelMessage.Text = "Please select a file"; } } catch (Exception ex) { labelMessage.Text = ex.Message; } }