Calculate the retrieved rows in database Visual C#
- by Tanya Lertwichaiworawit
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":
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();
}