Results 1 to 5 of 5

Thread: [RESOLVED] math.min

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Resolved [RESOLVED] math.min

    hello! anybody can help me?

    SELECT MAX(MV)-MIN(MV) as range_mv,STDEV(MV) as stdev_mv,(SELECT MV_MAX-MV_MIN FROM tblStandard WHERE COMPOUND_NO='" + cboCompoundNo.Text + "')/6*STDEV(MV) as cp_mv,(AVG(MV)-(SELECT MV_MIN FROM tblStandard WHERE COMPOUND_NO ='" + cboCompoundNo.Text + "'))/3*STDEV(MV) as cpl_mv,((SELECT MV_MAX FROM tblStandard WHERE COMPOUND_NO ='" + cboCompoundNo.Text + "')-AVG(MV))/3*STDEV(MV) as cpu_mv FROM tblData WHERE COMPOUND_NO = '" + cboCompoundNo.Text + "' AND MIXING_DATE BETWEEN #" + dtpFrom.Text + "# AND #" + dtpTo.Text + "#";

    above code generate output ok

    getting the MINIMUM and INSERT the value is like this:

    "INSERT INTO tblData(...) VALUES(...," + Math.Min(oDR["cpl_mv"],oDR["cpl_mv"]) + ")";

    error msg: "Cannot convert from object to sbyte"

    i passed the values but the same error occurs
    cpl_mv = oDR["cpl_mv"];
    cpu_mv = oDR["cpu_mv"];

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: math.min

    When you get a field from a DataReader like that it returns an Object reference, regardless of the actual type of the data. That's because the field could contain anything. If the field contains a number and you want to use it as a number then you have to cast it as its actual type. If the fields contain integers then you have to tell the compiler so:
    Code:
    Math.Min((int)oDR["cpl_mv"], (int)oDR["cpl_mv"])
    The same goes for any other type.

    Also, cannot recommend strongly enough that you do NOT build SQL statements using string concatenation. It makes for hard-to-read, error-prone and insecure code. ALWAYS insert variables into a SQL statements using parameters.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: math.min

    ok tnx for your help...
    but i already declare variables

    int cpl_mv,cpu_mv;
    and pass the value of oDR[]
    code is: Math.Min(cpl_mv,cpu_mv)

    what i did is different from casting?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] math.min

    So, is this a compilation error or a run time error?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: [RESOLVED] math.min

    ok jm i've got my error, i'm passing the values without converting like these:
    cpl_mv=oDR["cpl_mv"];
    cpl_mv=oDR["cpl_mv"];

    suppose to be:
    cpl_mv=(int)oDR["cpl_mv"];
    cpl_mv=(int)oDR["cpl_mv"];

    that's why variables are mismatch...

    in run time... nway tnx again...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width