0

I am calling ExecuteScalar with a valid value, and it worked on my previous one. I'm using using Oracle.ManagedDataAccess.Client;. Yet in this call it is returning null when the value should be 'Q'. Here is the code:

cmd.Parameters.Add(new OracleParameter("Cbrtaxfilcd_Descr", cbrEmployers.Cbrtaxfilcd_Descr));
//cmd.CommandText = "select Code from CBR_TAX_FILING_CODES where DESCR = :Cbrtaxfilcd_Descr";
cmd.CommandText = "select Code from CBR_TAX_FILING_CODES where DESCR = 'Quarterly'";

try
{
    //should be a string
    cbrEmployers.Cbrtaxfilcd_Code = Convert.ToString(cmd.ExecuteScalar());
}
catch (Exception ex)
{
    var message = ex.Message;
    //todo
}
madreflection
  • 4,744
  • 3
  • 19
  • 29
  • `ExecuteScalar` returns `null` where the query returns no rows. If you run the one that's not commented, where `'Quarterly'` is hardcoded, in SQL Developer or Toad, do you really get a row with `'Q'` in the first column? – madreflection Apr 16 '21 at 00:46
  • Side note, if you're not doing so already, I suggest setting `cmd.BindByName = true;` so that the names are bound correctly. Without that, they're bound ordinally and you have to add them in the order they appear in the query if you have more than one parameter. That can lead to confusion. – madreflection Apr 16 '21 at 00:48
  • I do get a Q when I manually run the query. – Charlotte Williams Apr 16 '21 at 00:49
  • Wow. Adding cmd.BindByName = true; fixed the problem. Thank you sooooo much. – Charlotte Williams Apr 16 '21 at 00:51

0 Answers0