none
ExecuteScalar<type> RRS feed

  • Question

  • Hello!

     

    I have a problem using ExecuteScalar<type> expressions.

    In .Net 8 this syntax is introdused. My old code used this

     

    int i = (int)cmd.ExecuteScalar();

     

    The translated should be:

    int i = cmd.ExecuteScalar<int>();

     

    But without typing:

    int i = cmd.ExecuteScalar<decimal>();

    if fails. Can someone explain?

     

    The sql executed is something like 

    Select Count(*) From Person

    With returning value 0 or 1.

     

    I'm using .Net8 and Oracle

     

    Regards,

    Friday, September 28, 2007 8:19 AM

All replies

  • Hi

     

             Your question i couldn't understand clearly .But as my understanding i give solution

     

     

    u are using oracle  there are using select command to count value .but in oracle database u want to assign selected value to a variable  i don't thing u can use "*" in oracle database to return value.hence do as follow

     

    select count(*) into counts from table name

     

    lets declare counts variable as output parameter  then  read out the variable in .net.

     

     

     

    Regards

    palan

     

    Friday, September 28, 2007 8:53 AM
  • Hi!

     

    The .Net code is like this now:

     

    sSql = "select count(*) from pw_menu where sparentid=: sMenuId";

    using (DbCommand cmd = new DbCommand(sSql))

    {

    cmd.AddParameter(": sMenuId", sMenuid);

    if (cmd.ExecuteScalar<decimal>() > 0)

    {

    _errormsg.InnerText = "...";

    return;

    }

    }

     

     

    Old:

    sSql = "select count(*) from pw_menu where sparentid=: sMenuId";

    using (DbCommand cmd = new DbCommand(sSql))

    {

    cmd.AddParameter(": sMenuId", sMenuid);

    if ((int) cmd.ExecuteScalar() > 0)

    {

    _errormsg.InnerText = "...";

    return;

    }

    }

    Friday, September 28, 2007 8:59 AM
  • Hi 

     

     

         In c# casting can do like

     

       (type)<object>

                                    

     where  u are going to convert the object value to integer so u want to do casting as old method

     

    As the new method wrong because which value going to casting and cmd.ExecuteScalar Its return object value too hence u couldn't return as integer

    cmd.ExecuteScalar<decimal>() > 0

     

     

     

     

    Regards

    palan

     

    Friday, September 28, 2007 9:32 AM