I am new to C# and i am building an application on result management where I have two tables named 'student' and 'result'. The primary key of student table is foreign key of result table. In the result table result of different subject a student is stored using the foreign key to identify whose result is it.Now i want to calculate the total result of every student and sort it. From where i can identify who is first.I can calculate total result of a single student but i cant understand how can i calculate total result of all student at a time. I use the below code for calculating result of a single student:
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.
Can anyone suggest me with my problem please...

Deepak SainiPosted Apr 7, 2015, 6:48 AM
select sum(number) TotalNo,s.studentname,s.studentid from student s inner join result r on s.studentid=r.studentid group by s.studentid,s.studentname order by TotalNo desc
if u using the grid view your calculate
Nozibulla BasharPosted Apr 7, 2015, 8:00 AM
Posted Apr 7, 2015, 6:52 AM
DataTable DT1 = new DataTable();
DT1.Columns.Add("ID", typeof(int));
DataRow DR = DT1.NewRow();
DR[0] = 1;
DT1.Rows.Add(DR);
DR = DT1.NewRow();
DR[0] = 2;
DT1.Rows.Add(DR);
DR = DT1.NewRow();
DR[0] = 3;
DT1.Rows.Add(DR);
DataTable DT2 = new DataTable();
DT2.Columns.Add("ID", typeof(int));
DT2.Columns.Add("RES", typeof(int));
DR = DT2.NewRow();
DR[0] = 1;
DR[1] = 1;
DT2.Rows.Add(DR);
DR = DT2.NewRow();
DR[0] = 2;
DR[1] = 1;
DT2.Rows.Add(DR);
DR = DT2.NewRow();
DR[0] = 1;
DR[1] = 3;
DT2.Rows.Add(DR);
DataRow[] DRs = null;
foreach (DataRow DR1 in DT1.Rows)
{
DRs = DT2.Select("ID=" + DR1[0]);
Console.WriteLine("Result of ID " + DR1[0].ToString() + " Nos " + DRs.Length);
}
Console.Read();
Kindly mark as answer if it helps you
Nozibulla BasharPosted Apr 7, 2015, 6:49 AM
Thanks
Abhishek KumarPosted Apr 7, 2015, 6:31 AM
Please follow the beautiful articles below.
http://asp.net-informations.com/gridview/gridview-summary.htm
http://www.asp.net/web-forms/overview/data-access/custom-formatting/displaying-summary-information-in-the-gridview-s-footer-vb
Hope this will help
Regards,Abhishek