/// <summary>
    /// Displays the uploaded prices in the grid. Funds are retrieved on each postback using the date of the fundprices found in the upload file
    /// </summary>
    private void displayUploadedPrices()
    {
        OMAMFundPricesCollection fundPricesCollection = getFundPricesCollection();

        LabelHeader.Visible = fundPricesCollection.Count > 0;
        GridViewUploadedPrices.DataSource = fundPricesCollection;
        GridViewUploadedPrices.DataBind();
    }
    /// <summary>
    /// Gets the fund prices from the db based on the datetime found in the upload.
    /// </summary>
    /// <returns></returns>
    private OMAMFundPricesCollection getFundPricesCollection()
    {
        OMAMFundPricesCollection fundPricesCollection = new OMAMFundPricesCollection();
        DateTime?priceDate = (DateTime?)ViewState["PriceDate"];

        if (priceDate != null)
        {
            fundPricesCollection.Query.Where(fundPricesCollection.Query.UploadDate.Equal(priceDate));
            fundPricesCollection.Query.Load();
        }
        return(fundPricesCollection);
    }
    private static void performContentValidation(HttpPostedFile postedFile, out OMAMFundPricesCollection fundPricesCollection, ref List <string> validationErrors)
    {
        //needed as you cannot assign values to an out param
        OMAMFundPricesCollection _fundPricesCollection = new OMAMFundPricesCollection();
        List <string>            _tempValidationErrors = validationErrors;

        //set the parsing class to parse the contents
        TextFieldParser textFieldParser = new TextFieldParser();

        setFileSchema(textFieldParser.TextFields);

        textFieldParser.QuoteCharacter    = '"';
        textFieldParser.FirstLineIsHeader = false;
        textFieldParser.FileType          = TextFieldParser.FileFormat.Delimited;
        textFieldParser.Delimiter         = ',';
        textFieldParser.TrimWhiteSpace    = true;

        // Set up event handlers for when a row is read and when a row is read but failes to match the expected schema
        textFieldParser.RecordFound += new TextFieldParser.RecordFoundHandler(
            delegate(ref Int32 currentLineNumber, TextFieldCollection textFields, string lineText)
        {
            string priceType = (string)textFields["Price Type"].Value;
            if (priceType.ToUpper() == "ACC" || priceType.ToUpper() == "INC")
            {
                OMAMFundPrices fundPrices = _fundPricesCollection.AddNew();
                //populate with values in text field array
                fundPrices.BidPrice    = (double?)textFields["Bid Price"].Value;
                fundPrices.OfferPrice  = (double?)textFields["Offer Price"].Value;
                fundPrices.Yield       = (double?)textFields["Yield"].Value;
                fundPrices.Description = (string)textFields["Description"].Value;
                fundPrices.FundCode    = (string)textFields["Fund Code"].Value;
                fundPrices.PriceType   = priceType;
                fundPrices.UploadDate  = (DateTime?)textFields["Price Date"].Value;
                fundPrices.UploadedBy  = 1;
            }
            else
            {
                _tempValidationErrors.Add(
                    string.Format("Line {0}: Unknown Fund Type - '{1}'.", priceType, currentLineNumber));
            }
        });

        textFieldParser.RecordFailed += new TextFieldParser.RecordFailedHandler(
            delegate(ref Int32 CurrentLineNumber, string LineText, string ErrorMessage, ref bool Continue) { _tempValidationErrors.Add(string.Format("Line {0}: {1}", CurrentLineNumber, ErrorMessage)); });

        //// parse the file contents
        textFieldParser.ParseFileContents(postedFile.InputStream);

        //assign temp varables back to the outand ref params
        fundPricesCollection = _fundPricesCollection;
        validationErrors     = _tempValidationErrors;
    }
    /// <summary>
    /// Saves the fund prices yo DB, remove any previous ones for the same date/time.
    /// </summary>
    /// <param name="fundPricesToSave">The fund prices to save.</param>
    private void saveFundPrices(OMAMFundPricesCollection fundPricesToSave)
    {
        //if we have any prices to save
        if (fundPricesToSave.Count > 0)
        {
            //select any prices already uploaded with the same date as the set we are about to save
            ViewState.Add("PriceDate", fundPricesToSave[0].UploadDate);
            OMAMFundPricesCollection fundPricesCollection = getFundPricesCollection();

            //NOTE: Perhaps a transaction should be placed around the delete and save?
            //remove any prices retrieved
            if (fundPricesCollection.Count > 0)
            {
                fundPricesCollection.MarkAllAsDeleted();
                fundPricesCollection.Save();
            }

            //insert the uploaded prices
            fundPricesToSave.Save();
        }
    }
    /// <summary>
    /// validates the upload and parses the file
    /// </summary>
    /// <param name="fileUpload">The file upload.</param>
    /// <param name="validationErrors">The validation errors.</param>
    /// <param name="fundPricesCollection">The fund prices collection from the parsed file.</param>
    /// <returns>
    ///     <c>true</c> if the specified file upload is valid; otherwise, <c>false</c>.
    /// </returns>
    private bool isFileValid(FileUpload fileUpload, out List <string> validationErrors,
                             out OMAMFundPricesCollection fundPricesCollection)
    {
        //set up variables
        List <string> tempValidationErrors = new List <string>();

        //perform upload validation
        HttpPostedFile postedFile;

        if (!performUploadValidation(fileUpload, out postedFile, ref tempValidationErrors))
        {
            fundPricesCollection = new OMAMFundPricesCollection();
            validationErrors     = new List <string>();
            return(false);
        }

        performContentValidation(postedFile, out fundPricesCollection, ref tempValidationErrors);

        //assign out param
        validationErrors = tempValidationErrors;
        return(validationErrors.Count == 0);
    }