2
I have a web form application where the user enters with some values to perform an analysis. It is two values item and quantity, this data is in one List<>
which is used as DataSource
of a GridView
. They can have up to 50 items, but usually does not exceed 5 items.
Use the following code to manipulate typed valuables:
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;
}
}
Stay like this:
I need to save the contents of this List
so that whenever the user enters the application they are available and are loaded in a GridView
with the last analysis performed. I didn’t want a table in the comic book for this.
How you could do this using Cache or there is another option?
Or I’ll have to create a comic book table for this
You want to know three different unrelated things so it should be three questions. But try to elaborate more the question, give some context.
– Maniero
I’ll try to elaborate on the question.
– Jothaz
I don’t know what you’re trying to do, but these three things are completely unrelated. You can’t choose between the three to solve what you want. Probably only cache is related to the problem. Of course, I may have misunderstood, but then you need to clarify more because you’re thinking of those three possibilities. What do you understand that they will help.
– Maniero
So I’m mixing things up. I’ll edit. But the idea is to save a list with some data and when the user logs in and loads one
gridview
with him.– Jothaz
I think the easiest is to use
BinarySerialization
to a file. On loading the program you read this file and de-serialize the data.– Vinícius Gobbo A. de Oliveira
Is it now clearer?
– Jothaz
@Vinícius Gobbo A. de Oliveira Ao usar
BinarySerialization
where the data is recorded?– Jothaz
The most common is to record it in any file. The biggest advantage of it is beyond write and read speed, is that it supports recording complex object trees. And it is very easy to use.
– Vinícius Gobbo A. de Oliveira
@Vinícius Gobbo A. de Oliveira I think I expressed myself badly. Is it in the client’s machine? I saw an example and it seemed simple. http://www.centerspace.net/examples/nmath/csharp/core/binary-serialization-example.php
– Jothaz
Yes, as long as your application is not ASP.Net. If it is C#, using WPF or Winforms, for example, the file can be client-side yes. If your application is ASP.Net, you will not be able to record files to the client because there is no support for this.
– Vinícius Gobbo A. de Oliveira
Every time I see one
catch (Exception ex) throw ex;
I feel like crying.– Maniero