Fórum Duvida If / Else #453381
27/08/2013
0
Mas percebi, que algumas condições, estão sendo ignoradas, veja o trecho do código.
Em uma parte do código ele ignora meu if e vai direto para o else.
else if (id_projeto == 2)
{
var drOcorrencias = clsLojas.retornaQuery("SELECT * FROM OCORRENCIAS where id_loja=" + id_loja + "");
if (drOcorrencias == null) //ESSE IF E IGNORADO E VAI DIRETO PARA O ELSE, O QUE DEVO ESTAR FAZENDO DE ERRADO..
{
tablestring2 = tablestring2 + "<table align='center' border='1' bordercolor='#666666' bordercolordark='#000000' width='100%' style='font-size:12px'>";
var drItens = clsLojas.retornaQuery("SELECT * FROM ITENS where id_projeto = 2 and id_canal = " + id_canal + " order by id_item");
while (drItens.Read())
{
tablestring2 = tablestring2 + "<tr bgcolor=\"\" onMouseOver=\"javascript:this.style.backgroundColor='#FFCC66';\" onMouseOut=\"javascript:this.style.backgroundColor='';\">";
tablestring2 = tablestring2 + "<td style='padding:25px;'> " + drItens["ds_item"] + "</td>";
tablestring2 = tablestring2 + "<td>";
tablestring2 = tablestring2 + "<table width='100%' border='0'>";
var drResp = clsLojas.retornaQuery("SELECT * FROM RESP_2 where id_item = " + drItens["id_item"].ToString().Trim() + " and id_subitem = 0 and id_loja = " + id_loja.ToString().Trim() + " ");
while (drResp.Read())
{
tablestring2 = tablestring2 + "<tr><td colspan='2' style='text-align:center;'>";
tablestring2 = tablestring2 + "<hr />";
tablestring2 = tablestring2 + " " + drResp["subresp"] + " ";
tablestring2 = tablestring2 + "<hr />";
//obs = (string)drResp["obs"];
}
tablestring2 = tablestring2 + "</td></tr>";
var drSubItens = clsLojas.retornaQuery("SELECT * FROM subitens where id_item = " + drItens["id_item"].ToString().Trim() + " AND ID_SUBITEM IN (SELECT ID_SUBITEM FROM UNI_TB_RESP_2 where id_item = " + drItens["id_item"] + " and id_loja = " + id_loja + " AND SUBRESP <> '') order by id_subitem");
while (drSubItens.Read())
{
tablestring2 = tablestring2 + "<tr><td style='text-align:center;'><hr />";
if (drSubItens["tipo"].ToString().Trim() == "D" || drSubItens["tipo"].ToString().Trim() == "N" || drSubItens["tipo"].ToString().Trim() == "I" || drSubItens["tipo"].ToString().Trim() == "S" ||
drSubItens["tipo"].ToString().Trim() == "")
{
tablestring2 = tablestring2 + " " + drSubItens["ds_subitem"] + ":";
}
var drResp2 = clsLojas.retornaQuery("SELECT * FROM RESP_2 where id_item = " + drItens["id_item"].ToString().Trim() + " and id_subitem = " + drSubItens["id_subitem"].ToString().Trim() + " and id_loja = " + id_loja + " ");
while (drResp2.Read())
{
tablestring2 = tablestring2 + " " + drResp2["subresp"] + " ";
//drResp2["obs"].ToString();
drResp2.Close();
}
tablestring2 = tablestring2 + " </td></tr>";
tablestring2 = tablestring2 + "<tr><td colspan='2'><hr /></td></tr>";
}
drItens.Close();
drResp.Close();
drSubItens.Close();
tablestring2 = tablestring2 + "</td></tr>";
tablestring2 = tablestring2 + "</table>";
divRelatorio.InnerHtml = tablestring2;
}
}
else
{
while (drOcorrencias.Read())
{
tablestring2 = tablestring2 + "<table align='center' border='1' bordercolor='#666666' bordercolordark='#000000' width='100%' style='font-size:12px'>";
tablestring2 = tablestring2 + "<tr><td align='center'><font color='red'><strong>OCORRENCIA</strong></font></td></tr>";
tablestring2 = tablestring2 + "<tr><td align='center'> <br />" + drOcorrencias["obs"] + "<br /> </td></tr>";
tablestring2 = tablestring2 + "<tr><td align='center'>Responsável: " + drOcorrencias["responsavel"] + "</td></tr>";
tablestring2 = tablestring2 + "</table>";
divRelatorio.InnerHtml = tablestring2;
}
}
drOcorrencias.Close();
}
Obridado Galera
Rodrigolima
Curtir tópico
+ 0Posts
27/08/2013
Pjava
Gostei + 0
27/08/2013
Tiago
Gostei + 0
27/08/2013
Rodrigolima
if (drOcorrencias.HasRows == false)
{
}
obrigado amigos.
Gostei + 0
28/08/2013
Tiago
Gostei + 0
28/08/2013
Clayton Silva
if (drOcorrencias.HasRows == false)
{
}
if já procura o resultado boolean, não precisa fazer esta comparação.
if (drOcorrencias.HasRows)
já resolve, caso queria negativo use uma exclamação antes.
if (!drOcorrencias.HasRows)
Gostei + 0
28/08/2013
Rodrigolima
Fiz dessa maneira, uma pratica mais correta.
Obrigado.
Gostei + 0
28/08/2013
Clayton Silva
Fiz dessa maneira, uma pratica mais correta.
Obrigado.
Tentamos ajudar sempre, mais umas dicas que vi em seu código.
comece a usar parâmetros, evite concatenar strings nas querys pois deixa o código propenso a invasão.
Evite concatenar strings usando +, é um procedimento que gasta muita memória e usado várias vezes, como em seu código, não é recomendado. Para isto use um stringbuilder.
Gostei + 0
28/08/2013
Tiago
Tentamos ajudar sempre, mais umas dicas que vi em seu código.
comece a usar parâmetros, evite concatenar strings nas querys pois deixa o código propenso a invasão.
Evite concatenar strings usando +, é um procedimento que gasta muita memória e usado várias vezes, como em seu código, não é recomendado. Para isto use um stringbuilder.
Essa observação é muito importante, afinal usar parâmetros é uma boa prática e evita erros de SQLINJECTION... concatenar strings é uma má prática, se bem que ainda vemos em exemplos de faculdade ou coisa e tipo... mas de como não fazer hehe.
Gostei + 0
28/08/2013
Joel Rodrigues
Gostei + 0
10/09/2013
Rodrigolima
Obrigado Pessoal
Gostei + 0
10/09/2013
Clayton Silva
Obrigado Pessoal
Meus parabéns.
Gostei + 0
10/09/2013
Rodrigo Odasaki
Coloca um enum, seu código ficara mais entendível.
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)