Saturday, March 24, 2012

Strange Error when executing a function

in my code-behind i call a class method that returns a string value...
i run step by step and works great!!

but when i run without breakpoint... sometimes i get error because my string is empty...
and then i run again step by step... and the surprise! works fine again...

why is this happening??

this is my code-behind

string param = " ";

param +=ConfigurarLoja.RetParams( );

Session.Add("ssnHabItemVincPromo",param);

if(param == " ")

{

response.redirect("~/Erro.aspx");

}

And this is my class method:

publicstaticstring RetParams()

{

string param ="";

int cd_empresa =Convert.ToInt32(HttpContext.Current.Session["ssnCdEmpresa"]);

string sql ="SELECT TOP 1 * FROM LjConfig WHERE cd_empresa = " + cd_empresa +" AND ISNULL(DATEADD(dd, 01, dt_exclusao), GETDATE()) > GETDATE() ORDER BY dt_inclusao DESC";SqlConnection con =newSqlConnection(ConfigurationManager.ConnectionStrings["StringDeConexao"].ConnectionString);

con.Open();

SqlCommand cmd =newSqlCommand(sql, con);

SqlDataReader dr = cmd.ExecuteReader();

dr.Read();

if (dr.HasRows)

{

param ="-" + dr["cd_empresa"].ToString().Trim();

}

con.Close();

return param;

}

ops... it seems solved...
i change the sql command 'WHERE'... i think it was a problem with the GETDATE... now its working fine...

AND (ISNULL(DATEADD(dd, 01, dt_exclusao), DATEADD(dd, 01, GETDATE())) > GETDATE()) "

0 comments:

Post a Comment