SqlDataAdapter sda = new SqlDataAdapter("SELECT tblResult.* FROM tblResult where [Student ID]='" + studentid + "'and Semester='"+"First"+"'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
dgvShowResult.DataSource = dt;
int sum = 0;
for (int i = 0; i < dgvShowResult.Rows.Count; i++)
{
sum += Convert.ToInt32(dgvShowResult.Rows[i].Cells[21].Value);
}
Here dgvShowResult is a dataGridView.First i select all result of a student using its ID and then place it in a dataGridView.So i can easily count total marks.