0

我正在尝试收集出价并从列中分配平均值

我收到一个 InvalidCast Exception: Specified cast is not valid。在总和 +=

任何帮助将不胜感激

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
4

3 回答 3

1

最终解决方案是这个

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

在我的应用程序中,列可能包含 Stings,因为用户可以直接输入 SQL。我只使用 Double 类型,但整数是一个选项

于 2016-07-14T12:57:27.807 回答
0

检查这个

int suma = 0;

Dim dc as DataColumn = dt.Columns[0]  //Column to add
For Each row As DataRow In dt.Rows
          suma += int.Parse(row[dc])

问候

于 2012-05-09T05:41:40.207 回答
0

感谢您的好回复

我的修复是

    Try
        TotalSum += Convert.ToInt32(row(Column))
        Console.WriteLine("Calculating TotalSum {0}", TotalSum.ToString)
    Catch
    End Try

尝试添加 Catch Block 以监视 vbNull。我只想要高于 0 的值

于 2012-05-09T15:58:37.127 回答