i need help. I tried to solve an equation through the values in each row in the table, so i tried but i didn't get the solution.
However, I supposed to calculate the avg, when the item is y, ( balance = previous balance - y's qty ) , ( price = X's Average, Avg= X's Avg).
While the item is X, ( balance = previous balance + qty) , ( Avg = (previous balance * previous Avg) + (price * qty) / balance.
The table that shows what i supposed to:http://imgur.com/Ch7jDyP
The Code:
Puvlic void Total()
{
try { string x = string.Empty; string ccc = WindowsFormsApplication5.Properties.Settings.Default.CCCConnectionString1; x = "SELECT Distinct * FROM Test$"; //Table Name = Test$ SqlConnection c = new SqlConnection(ccc); DataSet ds = new DataSet(); DataTable dt = new DataTable(); using (SqlDataAdapter da = new SqlDataAdapter(x, c)) { c.Open(); da.SelectCommand.ExecuteNonQuery(); da.Fill(ds); da.Fill(dt); foreach (DataRow row in ds.Rows) { //row[6]=qty //row[7]=price //row[8]=balance //row[9]=avg int ybalance = Convert.ToInt32(row[8]) - Convert.ToInt32(row[6]); int xbalance = Convert.ToInt32(row[8]) + Convert.ToInt32(row[6]); double TotalCost = Convert.ToInt32(row[6]) * Convert.ToDouble(row[7]); double avgcost = Convert.ToDouble(row[9]); double avg = ((ybalance * avgcost) + TotalCost) / xbalance; if (Convert.ToInt32(row[7]) == 0) { row[8] = ybalance; row[7] = avgcost; row[9] = avgcost; } else { row[8] = xbalance; row[9] = avg; } } } c.Close(); label1.Text = "Done"; } catch(Exception f) { label1.Text = "Failed" + "Error: " + f.Message; } } private void button1_Click_1(object sender, EventArgs e) { Total(); }
Firstly, I tried to let them null but it returned DBNull Error. Then i convert the null values to Zero, it executed but the values remained 0s and didn't follow the equations. Also i put the break-point at da.fill(ds) it retrieved values but i couldn't find
them in table data. Any Suggestions or help guys?
Thanks :) ,