|
-
May 2nd, 2007, 07:35 PM
#1
Thread Starter
Hyperactive Member
[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"];
-
May 2nd, 2007, 08:06 PM
#2
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.
-
May 2nd, 2007, 08:30 PM
#3
Thread Starter
Hyperactive Member
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?
-
May 2nd, 2007, 08:36 PM
#4
Re: [RESOLVED] math.min
So, is this a compilation error or a run time error?
-
May 2nd, 2007, 10:23 PM
#5
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|