Save data to the client for later use

2

I have a web form application where the user enters some values to perform an analysis. There are two item and quantity values, this data is in List<> which is used as DataSource of a GridView . They can have up to 50 items, but usually do not exceed 5 items.

I use the following code to manipulate the typed values:

public class Analise
{
    public int Item { get; set; }
    public int Quantidade { get; set; }
    public double CustoUnitario { get; set; }
    public double CustoAnual { get; set; }
}

static List<Analise> analiseLista = new List<Analise>();


protected void btnAdicionar_Click(object sender, EventArgs e)
{
    try
    {
        lblErro.Text = "";
        //Regex para validar somente números
        Regex soNumeros = new Regex(@"^[0-9]+$");
        if (!soNumeros.IsMatch(txtItem.Text) || !soNumeros.IsMatch(txtQuantidade.Text))
        {
            lblErro.Text = "Valore devem ser numéricos";
            return;
        }

        Int32 item = Convert.ToInt32(txtitem.Text);
        Int32 quantidade = Convert.ToInt32(txtQuantidade.Text);

        using (BDEntities db = new BDEntities())
        {
            //Verifica ser item existe em TB_item 
            var itemBD = (from c in db.TB_item
                                where c.item == item
                                select new
                                {
                                    c.CUSTO_item
                                }).ToList();
            if (itemBD.Count == 0)
            {
                //Se não existir mensagem de erro
                lblErro.Text = "item: " + item + " não cadastrada!";
            }
            else
            {
                //Se existir continua o processo
                custoUnitarioitem = (Double)itemBD.FirstOrDefault().CUSTO_item;
                custoAnual = (quantidade * custoUnitarioitem);

                //Verifica se item existe na lista analiseLista
                var existeAnalise = analiseLista.Where(c => c.item.Equals(item)).FirstOrDefault();
                if (existeAnalise == null)
                {
                    //Se não existir INCLUI
                    var novo = new Analise() { item = item, Quantidade = quantidade, CustoUnitarioitem = custoUnitarioitem, CustoAnual = custoAnual };
                    analiseLista.Add(novo);
                }
                else
                {
                    //Se existir ALTERA ou EXLCUI
                    if (quantidade == 0)
                    {
                        //Se quantidade zero exclui item da analiseLista
                        analiseLista.Remove(existeAnalise);
                    }
                    else
                    {
                        //Se quantidade maior que zero e capcacidade existe em analiseLista ALTERA 
                        existeAnalise.item = item;
                        existeAnalise.Quantidade = quantidade;
                        existeAnalise.CustoUnitarioitem = custoUnitarioitem;
                        existeAnalise.CustoAnual = custoAnual;
                    }
                }
                //Atualiza o grid com os valores
                gdvAnalise.DataSource = analiseLista.OrderBy(a => a.item).ToList();
                gdvAnalise.DataBind();
                double somatorio = analiseLista.Sum(x => Convert.ToDouble(x.CustoAnual));
                txtTotal.Text = somatorio.ToString();
            }
        }
        txtitem.Text = null;
        txtQuantidade.Text = null;
        txtitem.Focus();
    }
    catch (Exception ex)
    {
        throw ex;

    }
}

It looks like this:

I need to save the contents of this List so that whenever the user enters the application they are available and loaded in GridView with the useful analysis done. I did not want to make a table in the bd for this.

How could I do this using Cache or is there another option?

Or will I have to create a table in the DB for this

    
asked by anonymous 23.12.2014 / 15:16

1 answer

2

Of course there are other options but it might be that using a table in the bank is really the best.

Think about this scenario: the user opens the page on the work computer, creates such an analysis, after a while returns to that page and the last analysis appears automatically because it was saved in the client (browser). another browser or even another computer and nothing is remembered. Would this be intuitive for the user? Is this what you really want to implement?

If you find that the above scenario is not a problem, then you can look for local storage alternatives (on the client) as window.localStorage , window.indexedDB or File API . All of these alternatives may not work on all browser types.

Other alternatives that involve storing on the server are equivalent to using a database, and worse, they may suffer from limitations such as not working properly when you have more than one web server (web farms). I would suggest reconsidering and using the database instead of creating something more fragile.

    
23.12.2014 / 20:58