Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a table with two columns: step, z-value like this:
step|z-value
1|1
2|4
3|9
4|16

i am trying to loop through the rows of the table to grab the value in the z-value column in order to perform a calculation to insert into a new cell.

something like this:

step|z-value|z^3(this is the new column but to be attached to a DIFFERENT table)
1|1|1
2|4|16
3|9|81
4|16|256

for some reason, i am not grabbing the value in the cell and I'm unsure how to proceed.

thank you for any help.

C#
public DataTable zone2(inputParametersBO ipBO)
{
    //imports original table
    DataTable preAngle = paBCBLL.z2BCPA(ipBO);


    //creates new table for calculations to go into
    DataTable dt = new DataTable();
    DataColumn c1 = new DataColumn("RotatedX^2");
    DataColumn c2 = new DataColumn("RotatedX");
    DataColumn c3 = new DataColumn("RotatedY");
    c1.DataType = System.Type.GetType("System.Decimal");
    c2.DataType = System.Type.GetType("System.Decimal");
    c3.DataType = System.Type.GetType("System.Decimal");
    dt.Columns.Add(c1); dt.Columns.Add(c2); dt.Columns.Add(c3);

    foreach (DataRow row in preAngle.Rows)
    {
        double zVal = (double)row["Z2_BC"];//gets value from col2 of orig table
        double xStep = (double)row["step"];//gets value from col1 of orig table
        double rotatedY = Math.Sin((zVal - ipBO.z2bczS) / (xStep - ipBO.z2bcxS));
        double rotatedX = Math.Cos((zVal - ipBO.z2bczS) / (xStep - ipBO.z2bcxS));
        double rotatedXS = Math.Pow(rotatedX,2);

        DataRow dr = dt.NewRow();
        dr["RotatedX^2"] = rotatedXS;
        dr["RotatedX"] = rotatedX;
        dr["RotatedY"] = rotatedY;
        dt.Rows.Add(dr);
    }
    return dt;
}
Posted
Comments
ZurdoDev 28-Feb-14 11:29am    
Just put a breakpoint, find the line of code causing the error, and see why. Your data is invalid.
memberxxxxxxxxxxxxxxxxx 28-Feb-14 13:53pm    
what i've done so far, and i think this is similar to what you are suggesting, but i'm very much a novice, is to run the code to a specific section using the run to cursor. from there i checked the inputs i have.

so for the datatable preangle, i checked the pabcbll table that i imported and in vs you can look at the datatable. for that particular table, there was only one row with a value of 4.2 for step and a value of 1.09533.....for the zval.

that table is very real and it exists...sorta, but for some reason, the values don't seem to be passed along into the zval and xstep for the calculations.
ZurdoDev 28-Feb-14 13:55pm    
What line of code is causing the error?
Rob Grainger 28-Feb-14 15:16pm    
1. What is the value of ipBO at that stage?
2. You can use typeof(decimal) in place of System.Type.GetType("System.Decimal") (for future reference).
3. Through DataRowExtensions you can use the Field method for strongly-typed access:
Similarly, use SetField to write to the other field:

double zVal = row.Field<double>("Z2_BC");
dr.SetField<decimal>("RotatedX^2", (decimal)rotatedXS);

Note that I made the cast explicit here. I have a suspicion that the row's Item property (used to write in your example) may be failing to do the conversion from double to decimal.
4. Add a check to see if rotatedY, rotatedX or rotatedXS are INF or NaN
<pre lang="c#">if (rotatedY.IsNan() || rotatedY.IsInfinity() ) { /* ... */ }</pre>
5. You could a breakpoint at the third line of the foreach statement, (press F9 when the line is selected), then use F5 to run to that point.
In the Auto window, you can inspect the values - are any of them INF, NaN or zero at this point? If so you're failing to read the data for some reason.
6. It looks like you're using a parameterised query or procedure to initialise the data table. Have you checked that this works independently of the code to read the data? (Sorry if its obvious - just trying to cover all bases here).

Keep me posted - I'll give it more thought and see if I see anything else I'm missing.

Somehow one of your numbers is infinite.

Let iz be ipBo.z2bcxS.

I'd imagine the subexpression (xStep - iz) is yielding infinity because either:
(i) xStep is 0 and so is iz. Or
(ii xStep is equal to iz.

Either way, the results of rotatedX, rotatedY and rotatedXS are now infinity (any operation on infinity typically yields infinity or NaN).

So when you attempt to (implictly) convert it to a decimal it fails, because the decimal type cannot represent infinite numbers of NaN.

Hope this helps.

------------------------

See my comment above (accidentally posted in RyanDev's response) for more suggestions. With consideration It seems likely that the issue is writing a double to a decimal - I don't think the DataRow will, by default, attempt any conversions. The simplest solution to this may be to write using:

C#
dr["RotatedX^2"] = (decimal)rotatedXS;


I'd still make some of the other changes I suggested - using type-safe data access methods can help catch such errors earlier.
 
Share this answer
 
v2
Comments
memberxxxxxxxxxxxxxxxxx 28-Feb-14 12:09pm    
that's kinda the strange part. the table i am importing has only one cell. the step value is 4.2 and the zval is something like 1.09533 something. i know for sure there are values, they just aren't being transferred/read
Hi,

I am not sure what's the input param that you are passing to this method.

But, please make sure the below mathematical functions aren't happening as you are assigning them to a decimal variables.

C#
double rotatedY = Math.Sin((zVal - ipBO.z2bczS) / (xStep - ipBO.z2bcxS));
            double rotatedX = Math.Cos((zVal - ipBO.z2bczS) / (xStep - ipBO.z2bcxS));



The root cause may be due to divide by zero when xStep = ipBO.z2bcxS.

Why don't you add some code with try catch block and see if you see any divide by zero exception occurs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900