0

i need help. I tried to solve an equation through the records in the table, so i do not know how to use the values in each rows on the table.

Table Image: https://i.stack.imgur.com/fZuVb.jpg

So I need 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.

`

                foreach (DataRow row in ds.Tables[0].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.ToInt16(row[6]) * Convert.ToDouble(row[7]);
                    double avgcost = Convert.ToDouble(row[9]);
                    double avg = ((ybalance * avgcost) + TotalCost) / xbalance;

                    if (Convert.ToInt32(row[7]) == null)
                    {

                        row[8] = ybalance;
                        row[7] = Convert.ToDouble(row[9]);
                        row[9] = Convert.ToDouble(row[9]);


                    }

                    else
                    {
                        row[8] = xbalance;

                        row[9] = avg;


                    }

` Any hints or suggestions?

Thanks :)

  • I meant the question is why did i get DBNull logic error? – user2204191 Mar 16 '14 at 12:39
  • The error: "Object Cannot Be Cast From DBNull To Other Types." – user2204191 Mar 16 '14 at 12:46
  • Because there's null value somewhere in the DB that you are trying to cast to something else. Check on what line exception occurs and figure out what data coming from DB is usesd at that line. – J0HN Mar 16 '14 at 12:52

2 Answers2

0

check for DBNull.Value(Represents a nonexistent value in database) before casting:

if( row[8] != DBNull.Value && row[6] != DBNull.Value )
     int ybalance = Convert.ToInt32(row[8]) - Convert.ToInt32(row[6]);

to understand difference between null and DBNull, read accepted answer of this

Community
  • 1
  • 1
V-SHY
  • 3,925
  • 4
  • 31
  • 47
  • Thanks for your reply. However, it doesn't want to continue to calculate the balance and "if(row[7]==Null);" , as i set break point to "da.Fill(ds);". It's just kept counting the row[6] & row[8]. – user2204191 Mar 16 '14 at 13:20
  • I wonder how you proceed this? `double avgcost = Convert.ToDouble(row[9]);` your row[9] is DBNull then what value you want to set if you find it is DBNull? – V-SHY Mar 16 '14 at 13:38
0

You need to use int.TryParse(). if the value is DBNull.Value then it returns the default integer value zero.

Try This:

int row6;
int rows8;

int.TryParse(row[6],out row6);
int.TryParse(row[8],out row8);

int ybalance = row8 - row6;
int xbalance = row8 + row6;
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67