Question

I've been trying to find a solution to this problem all day long but I have been unable to come up with the solution by myself or find it up online.

My problem is the following: I am creating an application in vb.net 2010 I have an Access DataBase connected to the project. This Database has 2 tables one is Representatives and the other one is Demos. I'm trying to provide some useful stats about the Representatives like Total Demos, Total Sells Amount (CPO), Total Recommendations, Total Demos Given and Total Demos Sold. I have been successful in getting the first 3 to work but fail to get the last 2. The problem with this one is that the data entered in to the field is a simple Yes or No and I don't know how to put in to code that if the value of the cell is Yes then to add plus 1 to the counter and if its No to add plus one to a different counter.

Here is the code that I have up to now that works for the first 3 Stats.

'To calculate the total demos when you change the Representative
    Dim row As Cutco_testDataSet.DemosRow

    Dim intTotalItems As Integer
    For Each row In Cutco_testDataSet.Demos.Rows
        intTotalItems += 1
    Next
    lblTotalItems.Text = intTotalItems.ToString

    'Declare the variables for the values to be stored and later displayed
    Dim intTotalCPO As Integer
    Dim intTotalRecs As Integer

    'to look in Demosdgv and add the corresponding data
    For i As Integer = 0 To Cutco_testDataSet.Tables(0).Rows.Count - 1
        intTotalCPO += Cutco_testDataSet.Tables(0).Rows(i).Item("CPO")
        intTotalRecs += Cutco_testDataSet.Tables(0).Rows(i).Item("Rec's")
    Next
    lblTotalCpo.Text = intTotalCPO.ToString("c0")
    lblTotalRecs.Text = intTotalRecs.ToString()

Also I'm having trouble when the value is null it tells me that it cant add DBNull. Is there anyways that the counter would just carry on and ignore if the value has not been entered yet(null) or just count null as 0.

Thanks in advance!

Était-ce utile?

La solution

I am rewriting your code :

'Dim row As Cutco_testDataSet.DemosRow

'Dim intTotalItems As Integer
'For Each row In Cutco_testDataSet.Demos.Rows
'    intTotalItems += 1
'Next
'No need to use above "for loop" for counting total rows.  
lblTotalItems.Text = Cutco_testDataSet.Demos.Rows.Count

'Declare the variables for the values to be stored and later displayed
Dim intTotalCPO As Integer
Dim intTotalRecs As Integer
dim intTotdemosgiven as Integer

'to look in Demosdgv and add the corresponding data
'For i As Integer = 0 To Cutco_testDataSet.Tables(0).Rows.Count - 1
For Each row as Datarow in Cutco_testDataSet.Tables(0).Rows
    intTotalCPO += NullToZero(row("CPO"))
    intTotalRecs += NullToZero(row("Rec's"))

    intTotdemosgiven = intTotdemosgiven + IIf(UCase(nulltostring(row("DemosGiven"))) = "YES", 1, 0) 'I dont know exact field name for Demos given. 

Next
lblTotalCpo.Text = intTotalCPO.ToString("c0")
lblTotalRecs.Text = intTotalRecs.ToString()



Public Function NullToZero(ByRef FieldVal) As Double
'This function converts null or blank values to zero.
    If FieldVal Is Nothing OrElse FieldVal.ToString() = vbNullString Then
        Return 0
    Else
        Return FieldVal
    End If
End Function

Public Function NullToString(ByRef FieldVal) As String
    If FieldVal Is Nothing OrElse IsDBNull(FieldVal) Then
        Return vbNullString
    Else
        Return FieldVal
    End If
End Function

Hope this helps

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top