2
I’m trying to make a query in the table below, from my database.
This query is performed through the method below:
public void compraAcoes(float cepf, string codigo, int quantidade)
{
string cd = codigo;
try
{
float cpf;
BuscaNet busca = new BuscaNet();
Cotacao objeto = new Cotacao();
objeto = busca.buscaInformacao(codigo);
cpf = cepf;
string empresa = objeto.empresa;
string tipo = objeto.tipo;
DateTime data = Convert.ToDateTime(objeto.data);
string hora = objeto.hora;
double abertura = objeto.abertura;
double maxima = objeto.maxima;
double minima = objeto.minima;
double media = objeto.media;
double fechamento = objeto.fechamento;
double fechamento_anterior = objeto.fechamento_anterior;
Int32 volume = objeto.volume;
Int32 volume_financeiro = objeto.volume_financeiro;
Int32 negocio = objeto.negocio;
double oferta_de_compra = objeto.oferta_de_compra;
double oferta_de_venda = objeto.oferta_de_venda;
Int32 quantidade_ofertada_compra = objeto.quantidade_ofertada_compra;
Int32 quantidade_ofertada_venda = objeto.quantidade_fertada_venda;
double variação = objeto.variacao;
string status = objeto.status;
string fase = objeto.fase;
string cod = codigo;
bancotccEntities bc = new bancotccEntities();
var ac = from obj in bc.acao
where (obj.cpf == cepf && obj.codigo == cod)
select obj;
if (ac == null)
{
double vatotal_acao;
vatotal_acao = (quantidade * fechamento);
banco.inserirAcao(cepf, cod, empresa, tipo, data, hora, abertura, maxima, minima, media, fechamento, fechamento_anterior, volume, volume_financeiro, negocio,
oferta_de_compra, oferta_de_venda, quantidade_ofertada_compra, quantidade_ofertada_venda, variação, status, fase);
banco.inserirCarteira(cepf, cod, fechamento, quantidade, vatotal_acao);
}
else
{
carteira ca = bc.carteira.FirstOrDefault(obj => obj.cpf == cepf && obj.codigo == cod);
//int qt = ca;
ca.qtdacao = ca.qtdacao + quantidade;
//int nova_quant = ca.qtdacao + quantidade;
// int nova_quant = ca.qtdacao;
double valor = quantidade * fechamento;
ca.vtotalacao = ca.vtotalacao + valor;
//double novo_valor = ca.vtotalacao;
banco.atualizaAcao(cepf, codigo, empresa, tipo, data, hora, abertura, maxima, minima, media, fechamento, fechamento_anterior,
volume, volume_financeiro, negocio, oferta_de_compra, oferta_de_venda, quantidade_ofertada_compra,
quantidade_ofertada_venda, variação, status, fase);
// banco.atualizarCarteira(cepf, cod, fechamento, qt, total_acao);
// banco.atualizarCarteira(cpf, codigo, fechamento, quantidade, novo_valor);
}
}
catch (Exception e)
{
throw new Exception(e.Message.ToString());
}
/*catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
}*/
}
The method checks if the data exists, if there is no it inserts, this part has already tested and working, the problem is when it has data in the database. I try to find the values qdacao and vtotalacao, but the select I’m using is not returning any value as shown below:
returning me the error:
Undefined object reference for an object instance.
Already try to select according to the code below but just change the error:
var ca = (from obj in bc.carteira
where (obj.cpf == cepf && obj.codigo == cod)
select obj).First();
The sequence contains no elements
Can you help me?
The error is clear in saying that you are trying to get the first element of a list that has no results. What are the values being passed on
cepf
andcodigo
?– Leonel Sanches da Silva
The values of cepf and code are received per parameter of another class. public void buysAcoes(float cepf, string code, int quantity) in this case the values are: 98765500000 and PETR4. {
– user9090
I still think it’s a parameterization problem. Possibly the values passed to the
FirstOrDefault
are different from the columns you are trying to select.– Leonel Sanches da Silva
This select is not searching for the first object in the database that accepts the Where clause?
– user9090
The query you are doing is searching for the first element (First) that matches the condition passed or however, if there is no element, it is returned
null
(Ordefault). So as @Ciganomorrisonmendez mentioned, the problem is inWhere
. Put the variables you are using to make the comparison onwatch
, confirm that these are expected or actually exist in the BD.– Omni
ok guys got it. does anyone know how I do and return from the bank the items I need? qtdacao and vtotalacao? or do I have to do it in another question?
– user9090
@user9090, when you debug which value before above in the variable cepf and Cod, so this is what is happening of not bringing the data correctly, regarding the second question would be:
var c = bc.cateira.Where(x => x.cpf == cepf && x.codigo == cod).Select(x => new { x.qtdacao, x.vtotalacao).FirstOrDefault()
– user6026
the value of Cod is PTR4, but the value of cepf 9.87655455E+10
– user9090
So you just found the problem, the cepf is in a different format. Usually in Cpf I use string (nvarchar, varchar), so in this format you will not find the record, understand? At its base is 987654321 and at what it shows has a point after 9.8... so ratifying the value ta different!
– user6026