question about oleDbDataReader HasRows properties

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Claudia Fong

    question about oleDbDataReader HasRows properties



    Hi,

    I'm using the oleDbDataReader HasRows properties to detect wheter my
    table contains a record with the parameter below:


    oleDbConnection 1.Close();
    objreturnst.Par ameters["DEPT_CODE"].Value = comboBox2.Text;
    objreturnst.Par ameters["ST_CODE"].Value = comboBox1.Text;
    oleDbConnection 1.Open();
    OleDbDataReader objreturn = objreturnst.Exe cuteReader();
    if(objreturn.Ha sRows) --> this should be false, but it seems it is
    always true.. :(
    {
    objreturn.Read( );
    retqty = Convert.ToDoubl e(objreturn["Expr1"]);
    }

    The SQL statement is:
    this.objreturns t.CommandText = "SELECT SUM(TRAN_QTY) AS Expr1 FROM
    TRAN2005 WHERE (TRAN_TYPE = \'7\') AND (DEPT_CODE = ?) AND (ST_CODE =
    ?)";

    Since the expression objreturn.HasRo ws isn't false, so it will generate
    the error: Object cannot be cast from DBNull to other types.

    I don't know if there's any mistakes in my code, but can someone help
    me?

    Cheers!

    Claudi

    *** Sent via Developersdex http://www.developersdex.com ***
  • Nathan Baulch

    #2
    Re: question about oleDbDataReader HasRows properties

    > I don't know if there's any mistakes in my code, but can someone help[color=blue]
    > me?[/color]

    A better way to tell if any rows are available is to look at the return
    value of objreturn.Read( ).
    Also, you should probably cast the return value directly to the expected
    type rather than using Convert.ToDoubl e which is a little less efficient.

    using (OleDbDataReade r objreturn = objreturnst.Exe cuteReader())
    {
    if (objreturn.Read ())
    {
    if (objreturn[0] is double)
    retqty = (double) objreturn[0];
    }
    }


    Nathan


    Comment

    Working...