I am beginner in ASP.NET Web Development.Now I want to find sum of marks using SQL sum query. but It finds -1 as sum after query execution. Here is my code:
public double GetTotalScore(string regNo)
{
SqlConnection connection=new SqlConnection(ConnectionString);
string query = "select sum(Score) from SaveResult where RegNo='" + regNo + "' group by RegNo";
SqlCommand command = new SqlCommand(query, connection);
connection.Open();
double total = command.ExecuteNonQuery();
connection.Close();
return total;
}
How could i get sum using sql query?
SaveResultlooks like-1bit. If you look at the docs you'll see the return value to ExecuteNonQuery is "The number of rows affected." In the remarks section it explainsFor UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command.and then laterFor all other types of statements, the return value is -1.Since a SELECT isn't a UPDATE, INSERT or DELETE it is a "other type of statement"