0

i am trying to get Sum() of value in my database data using C# but i am having an error Exception error sql type -0 here is my Code.

the problem is here when i am passing value through dr i am always getting exception error

enter image description here

ArgumentException has unhandled Unknown Sql type -0

4

2 回答 2

1

尝试ExecuteScalar

SqlConnection connection = new SqlConnection(myConnectionString);
SqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "SELECT Sum(Income) FROM audittrail_tbl";

var obj = cmd.ExecuteScalar();
int result = obj != null ? (int)obj : 0;
connection.Close();

您应该null在将值转换为int.

于 2015-03-04T09:00:34.363 回答
0

如果你可以使用 linq,你可以这样修复:

label.Text=(from f in audittrail_tbl
            select f).Sum(S=>S.Income).ToString();
于 2015-03-04T09:09:17.393 回答