Calculate the retrieved rows in database Visual C#

Posted by Tanya Lertwichaiworawit on Stack Overflow See other posts from Stack Overflow or by Tanya Lertwichaiworawit
Published on 2012-10-14T09:32:40Z Indexed on 2012/10/14 9:36 UTC
Read the original article Hit count: 270

Filed under:
|
|
|

Form1

I am new in Visual C# and would want to know how to calculate the retrieved data from a database.

Using the above GUI, when "Calculate" is click, the program will display the number of students in textBox1, and the average GPA of all students in textBox2.

Here is my database table "Students":

enter image description here

I was able to display the number of students but I'm still confused to how I can calculate the average GPA

Here's my code:

private void button1_Click(object sender, EventArgs e)
        {
            string connection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data  Source=C:\Database1.accdb";
            OleDbConnection connect = new OleDbConnection(connection);

        string sql = "SELECT * FROM Students";
        connect.Open();
        OleDbCommand command = new OleDbCommand(sql, connect);
        DataSet data = new DataSet();
        OleDbDataAdapter adapter = new OleDbDataAdapter(command);
        adapter.Fill(data, "Students");

        textBox1.Text = data.Tables["Students"].Rows.Count.ToString();

        double gpa;
        for (int i = 0; i < data.Tables["Students"].Rows.Count; i++)
        {
            gpa = Convert.ToDouble(data.Tables["Students"].Rows[i][2]);
        }

        connect.Close();
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about database