Dazza
Dazza

Reputation: 9

Combo box returning error - when casting from a number less than infinity

I have spent a couple of days on this, many searches and I cant find an answer, and yep, I bet it is simple.

I am trying to total the fuel used from an access DB via a vb.net combobox, Fuel_Cb1. In this statement I am receiving an error "when casting from a number less than infinity".

If I change the word bus in the following statement to a known number for example 43, it will return the correct sum amount for that vehicle number. Also if I remove the 'WHERE' statement it will return the sum of the Fill_Liters column correctly.

Dim Query As String = ("select sum (Fill_Liters) FROM Fuel where Bus_ID= '" & bus & "'")

It also will return the "when casting from a number less than infinity" error when using

Dim reader As OleDbDataReader = cmd.ExecuteReader()

While reader.Read()

If I use the the sum statement, if the sum statement isn't use however you can select from the combobox and it will display the last entry in the Fill_Liters column

 Private Sub clickFuelCb1(ByVal sender As Object, ByVal e As EventArgs) 'Handles Cb1.SelectedIndexChanged

    Dim combobox = DirectCast(sender, ComboBox)
    Dim bus
    bus = Fuel_Cb1.Text
    With Fuel_GB1
        .Location = New Point(10, 50)
        .Text = "Fuel statistics for vehicle number " + bus
        .Size = New Size(300, 300)
    End With
    Dim Query As String = ("select sum (Fill_Liters)  FROM Fuel where Bus_ID= '" & bus & "'")
    Using con2 As New OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0; data source=H:\Documents and Settings\Administrator\Desktop\Fuel\Fuel\DB1.accdb;")

        Dim cmd As New OleDb.OleDbCommand(Query, con2)
        con2.Open()
        'Dim reader As OleDbDataReader = cmd.ExecuteReader()
        'While reader.Read()
        Fuel_Lb2.Text = cmd.ExecuteScalar() 'reader.GetString(0)


        'End While
        con2.Close()

    End Using

End Sub

I hope I have enough information here, and look forward to your answers.

Upvotes: 0

Views: 117

Answers (1)

Trevor
Trevor

Reputation: 8004

Sorry this took a little long, but will and should work for you... Your first issue was because of your ticks you had in the where clause. Those are for strings and such, not for numeric data types.Your query string should now be fixed, but your using the commands wrong...

    Dim intSum As Integer = 0

    Using con2
        Using cmd
            con.Open()
            cmd.Connection = con
            cmd.CommandText = "select sum (Fill_Liters) FROM Fuel where Bus_ID= " & bus & """
            intSum = cmd.ExecuteScalar 'Lets get something back...
            con.Close()
        End Using
    End Using

    Fuel_Lb2.Text  = intSum.ToString("C")

*Note: This is Tried and Tested

Upvotes: 0

Related Questions