views:

28

answers:

2

We have a dataset dsActualCost which fetches query value: MonthName in one column and Cost in another.
We have a code to display data in table format.

 For MonthIndex = 0 To dsMonth.Tables(0).Rows.Count - 1
   Dim tdMonthlycost As New TableCell
   If dsActualCost.Tables(0).Rows.Count > 0 Then
     If IsDBNull(dsActualCost.Tables(0).Rows(MonthIndex).Item(1)) Then         
           tdMonthlycost.Text = 0
      Else
           tdMonthlycost.Text = dsActualCost.Tables(0).Rows(MonthIndex).Item(1)
      End If
    Else
      tdMonthlycost.Text = 0
    End If
       trBody.Cells.Add(tdMonthlycost)
  Next  

This code works fine when
we have data in all rows
we have no data at all

But it gives exception for all other cases. I tried to debug and issue arises when it searches for any row which is not available in DB.

Say the dataset has only 2 rows for Feb month,so when it executes line
IsDBNull(dsActualCost.Tables(0).Rows(2).Item(1))
It throws Db null exception.

please suggest how should i handle it. the loop will run for 12 months.

+1  A: 

Shouldn't you be checking:

If dsActualCost.Tables(0).Rows.Count > 0 And
   MonthIndex < dsActualCost.Tables(0).Rows.Count

Without that check, you could index out of the Rows array if MonthIndex refers to a position that isn't present.

dcp
If the monthIndex loop reaches 2 but dsActualCost dataset has value only for September,October and November(No data for previous months), the loop would display that data in January, February and March. Other months will show 0.
RMN
@RMN - You still need to check to make sure MonthIndex isn't out of range in dsActualCost. You said you were getting an exception, that may be the reason. If you index out of an array, that will cause an exception.
dcp
Yep you are correct and the way you told is also correct. But when i want to put data of Month jan in first cell and april in 4th cell. What kind of loop should i use ? The loop you told will only not allow exception. But when i want to pick up value for a particular month, then how should i proceed ?
RMN
@RMN - If you have data in Jan and Apr, then how many rows are you going to have in dsActualCost.Tables(0)? Would you just have 2 rows, one for Jan and Apr? If that's the case, I think you will need to move your inner loop to another loop and handle it separately.
dcp
@dcp. Thanks for help. My code works now.
RMN
A: 

Here is the code now. Do let me know if you have any suggestions:

Dim Flag As Integer = 0
For MonthIndex = 0 To dsMonth.Tables(0).Rows.Count - 1
Dim tdMonthly As New TableCell
Dim CostMonth As Integer
Dim CostValue As Integer
If (dsActualCost.Tables(0).Rows.Count > 0) Then
If (Flag <> dsActualCost.Tables(0).Rows.Count) Then
CostMonth = dsActualCost.Tables(0).Rows(Flag).Item(1)
CostValue = dsActualCost.Tables(0).Rows(Flag).Item(0)
If (dsMonth.Tables(0).Rows(MonthIndex).Item(0) = CostMonth Or (dsMonth.Tables(0).Rows(MonthIndex).Item(0) - 1 = CostMonth)) Then
tdMonthly.Text = CostValue
Flag = Flag + 1
Else
tdMonthly.Text = 0
End If
Else
tdMonthly.Text = 0
End If
Else
tdMonthly.Text = 0
End If
trBody.Cells.Add(tdMonthly)
Next

The month in dsMonth dataset is an integer and shows only even number( like Jan-2, Feb-4 etc). But the month in dsActual shows either of 1,2 for Jan and 3,4 for feb. That is the reason i had put
If (dsMonth.Tables(0).Rows(MonthIndex).Item(0) = CostMonth Or (dsMonth.Tables(0).Rows(MonthIndex).Item(0) - 1 = CostMonth))

Please let me know your suggestions if any.

RMN