Sql Query Data Reader Returning True even it's False in C# -


good day!

it's taking me hours why query returns true it's false.

here's code.

public sqldatareader check(bel bel) {         sqlcommand cmd = new sqlcommand();         cmd.connection = dbcon.getcon();         cmd.commandtype = commandtype.text;         cmd.commandtext = "select sum(total) overalltotal table id=@id , datefrom=@from , dateto=@to , status='without'";         cmd.parameters.addwithvalue("@id",bel.clempid);         cmd.parameters.addwithvalue("@from", bel.clpayrollfrom);         cmd.parameters.addwithvalue("@to", bel.clpayrollto);          sqldatareader dr = cmd.executereader();         return dr;     } 

suppose parameterized values are:

@id = 0001 @from = 1/28/2016 @to = 1/29/2016 

here's method calling datareader

sqldatareader drcheck;  drcheck = bal.check(bel); if (drcheck.hasrows == true) {     drcheck.read();     // i'm inside computation of overalltotal }else{     drcheck.close();    // i'm out } drcheck.close(); 

the problem is, when value of "to", instance, 1/30/2016, suppose go false out of true condition not.

please help. in advance

in case don't need reader, can use executescalar:

public decimal? check(bel bel) {     sqlcommand cmd = new sqlcommand();     cmd.connection = dbcon.getcon();     cmd.commandtype = commandtype.text;     cmd.commandtext = "select sum(total) overalltotal table id=@id " +                        "and datefrom=@from , dateto=@to , status='without'";     cmd.parameters.addwithvalue("@id", bel.clempid);     cmd.parameters.addwithvalue("@from", bel.clpayrollfrom);     cmd.parameters.addwithvalue("@to", bel.clpayrollto);      object obj = cmd.executescalar();     decimal? value = null;     if (obj != dbnull.value)         value = convert.todecimal(obj);      return value; }  decimal? total = bal.check(bel); if (total.hasvalue) {     // total.value } else {  } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -