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


i new in c#. trying read data table named 'tbladdresult'. in table have columns , want make average of 3 columns of tbladdresult , save new table named tblresult.

some other calculations wanna there taking 50% of data of column of tbladdresult.

i did in way(code placed below) getting error "invalid attempt call read when reader closed". don't know if right way if not can me write way or suggestions regarding way. helpless problem.

     private void button1_click(object sender, eventargs e)        {         con.open();         cmd = new sqlcommand("select   tbladdresult.*  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 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!");          } 

edit code below, better safety. error occurred because datareader operations work when in opened connection state.

        con.open();         cmd = new sqlcommand("select tbladdresult.*  tbladdresult", con);         sqldatareader sdr = cmd.executereader(system.data.commandbehavior.closeconnection);//this close datareader along connection close.          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++"; }              cmd = new sqlcommand("insert 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);             cmd.executenonquery();                     }         con.close();         messagebox.show("successfull!"); 

edit:

instead of using datareader approach, use datatable , example given below. hope help.

cmd = new sqlcommand("select *  test_table1", con);             sqldataadapter da = new sqldataadapter(cmd);             datatable dt = new datatable();             da.fill(dt);              foreach (datarow sdr in dt.rows)             {                 string subject = sdr[0].tostring();                 int january = convert.toint32(sdr[1]);                 int february = convert.toint32(sdr[2]);                 int march = 0;                 int average = (january + february + march) / 3;                 int average40 = average * 40 / (100);                  cmd = new sqlcommand("insert test_table2 (test_col1, test_col2) values  ('" + subject + "'," + average40 + ")", con);                 con.open();                 cmd.executenonquery();                 con.close();             } 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -