0

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?

4
  • 3
    Use ExecuteScalar instead of ExecuteNonQuery. Commented May 9, 2016 at 17:45
  • How the table SaveResult looks like Commented May 9, 2016 at 17:45
  • Is there a single line of code that is asp.ne specific - or do you add random tags for technologies you just can not identify because you do not bother to do so? That is not asp.net, it is ado.net. It may be in a web application, it still is ado.net. Commented May 9, 2016 at 17:47
  • 1
    Just a note on the -1 bit. 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 explains For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. and then later For 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" Commented May 9, 2016 at 17:55

2 Answers 2

4

ExecuteNonQuery returns the number of rows affected.

ExecuteScalar is the method you want. It returns a single-value (a "scalar") to you.

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 =  (double)cmd.ExecuteScalar();
        connection.Close();
        return total;
    } 

You still have a problem.

   string query = "select sum(Score) from SaveResult where RegNo='" + regNo + "' group by RegNo";

You need to change to parameterized queries.

   string query = "select sum(Score) from SaveResult where RegNo='@MyParameter' group by RegNo";


        SqlParameter param  = new SqlParameter();
        param.ParameterName = "@MyParameter";
        param.Value         = "myvalue";


        cmd.Parameters.Add(param);

/* now call the ExecuteScalar */

See

http://www.csharp-station.com/Tutorial/AdoDotNet/Lesson06

Sign up to request clarification or add additional context in comments.

5 Comments

Not an answer. Answers have to explain.
what no reference to parameterized queries?
beware of injection RegNo='" + regNo + "'
Rowdy bunch today.
does the OP expect whom ever answers to provide links as well as answers to every example.. I think that this answer provides an excellent starting point and if the OP needs more this is what GOOGLE and or other search engines are out there for.. +1 btw
0
public string 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();
        object total = command.ExecuteScalar();
        connection.Close();
        return Convert.ToString(total);
    }    
}

It works,to getting sum using ExecuteScalar.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.