Gerolkae Gerolkae - 1 year ago 124
Vb.net Question

How to get the Sum of values from a SQLite Table?

I'm trying to Collect Bids and Distribute the average from a Column

I'm getting an InvalidCast Exception: Specified cast is not valid. on the TotalSum +=

Any Help would be greatly apprciated

Dim sql As String = "SELECT " & Column & " FROM " & Table & " ;"
Dim dt As DataTable = SQLiteDatabase.GetDataTable(sql)
Column = Column.Replace("[", "")
Column = Column.Replace("]", "")
For Each row As DataRow In dt.Rows
TotalSum += DirectCast(row(Column), Integer)
Console.WriteLine("Calculating TotalSum {0}", TotalSum.ToString)
Next row

Answer Source

Final Solution is this

Dim suma As Double = 0.0R
For Each row As DataRow In dt.Rows
    Dim num As Double = 0R
    Double.TryParse(row(Column).ToString, num)
    suma += num
Next row

Columns could potentually contain Stings in my application since the User can input SQL directly. I'm only working with Double types but Integers are an option

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download