mercredi 1 avril 2015

In C# How to read data from table and after some calculation save those data in another table?

I an new in C#.I am trying to read data from a table named 'tblAddResult'. In this table I have some columns and i want to make average of 3 columns of tblAddResult and save it to a new table named tblResult. SOme other calculations also wanna do there like Taking 50% of a data of a column of tblAddResult.I did this in this way(code placed below) but i am getting an error "invalid attempt to call read when reader is closed". I dont even know is this the right way to do it if not can anyone help me with the write way or any suggestions regarding this way. I am really helpless for this problem.



private void button1_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("SELECT tblAddResult.* FROM tblAddResult", con);
SqlDataReader sdr = cmd.ExecuteReader();

while (sdr.Read())
{

String subject = sdr.GetString(2);
int january = sdr.GetInt32(3);
int february = sdr.GetInt32(4);
int march = sdr.GetInt32(5);
int average = (january + february + march) / 3;
int average40= average*40/(100);
int marks = sdr.GetInt32(6);
int marks50 = marks * 50 / 100;
int WorkingDay = sdr.GetInt32(7);
int Attandence = sdr.GetInt32(8);
int Attendence10 = Attandence * 10 / 100;
int totalMarks = average40 + marks50 + Attendence10;
string grade = "" ;
if (totalMarks < 51) { grade = "C"; }
else if (totalMarks < 61) { grade = "B"; }
else if (totalMarks < 71) { grade = "A-"; }
else if (totalMarks < 81) { grade = "A"; }
else if (totalMarks < 91) { grade = "A+"; }
else if (totalMarks <= 100) { grade = "A++"; }
con.Close();
con.Open();
SqlCommand comnd = new SqlCommand("INSERT INTO tblResult (Subject, [Full Marks], January, February, March, [Average Class Perfomance], [Earned Marks], [Working Day], Attendence, [Cls Attendence(40%)], [Exam Perfomance(50%)], [Attendence(10%)], [Marks(%)], Grade)VALUES ('"+subject+"','"+"100"+"','"+january+"','"+february+"','"+march+"','"+average+"','"+marks+"','"+WorkingDay+"','"+Attandence+"','"+average40+"','"+marks50+"','"+Attendence10+"','"+totalMarks+"','"+grade+"')",con);
comnd.ExecuteNonQuery();
MessageBox.Show("Successfull!");

}

Aucun commentaire:

Enregistrer un commentaire