c# 4.0 - Saving data read from sql database -
i have 4 textboxes , metrogrid, inside textboxes data read them sql database, while saving data in textboxes 1 save , others vanish. please need code search button:
private void btnsearch_click(object sender, eventargs e) { try { using (sqlconnection connection = new sqlconnection(@"server = .\sql2012; database = dbacceptance; integrated security = true; ")) { sqlcommand command = new sqlcommand($"select * acceptance regno = {txtregno.text}", connection); connection.open(); sqldatareader read = command.executereader(); if (read.read()) { txtfullname.text = (read["fullname"].tostring()); txtdepartment.text = (read["department"].tostring()); txtfaculty.text = (read["faculty"].tostring()); txtmodeofstudy.text = (read["modeofstudy"].tostring()); txtprogramme.text = (read["programmeofstudy"].tostring()); } else metroframework.metromessagebox.show(this, "reg no not found...please try again", "message"); read.close(); } } catch (exception ex) { //metroframework.metromessagebox.show(this, ex.message, "message", messageboxbuttons.ok, messageboxicon.error); } }
this code add button:
private void btnadd_click(object sender, eventargs e) { objstate = entitystate.added; pcontainer.enabled = true; registeredbindingsource.add(new registered()); registeredbindingsource.movelast(); txtregno.focus(); }
this code save button:
private void btnsave_click(object sender, eventargs e) { try { registeredbindingsource.endedit(); registered obj = registeredbindingsource.current registered; if (obj != null) { using (idbconnection db = new sqlconnection(configurationmanager.connectionstrings["conn"].connectionstring)) { if (db.state == connectionstate.closed) db.open(); if (objstate == entitystate.added) { dynamicparameters p = new dynamicparameters(); p.add("@studentid", dbtype: dbtype.int32, direction: parameterdirection.output); p.adddynamicparams(new { fullname = txtfullname.text, regno = txtregno.text, coursetitle = obj.coursetitle, coursestatus = obj.coursestatus, courseunit = obj.courseunit, department = txtdepartment.text, currentsemester = obj.currentsemester, currentsession = obj.currentsession, modeofstudy = txtmodeofstudy.text, level = obj.level, programmeofstudy = txtprogramme.text, faculty = txtfaculty.text, finalsemester = obj.finalsemester }); db.execute("sp_studentregisteredcourse_insert", p, commandtype: commandtype.storedprocedure); obj.studentid = p.get<int>("@studentid"); } else { db.execute("sp_studentregisteredcourse_update", new { studentid = obj.studentid,fullname = txtfullname.text,regno = txtregno.text, coursetitle = obj.coursetitle,coursestatus = obj.coursestatus, courseunit = obj.courseunit, department = txtdepartment.text, currentsemester = obj.currentsemester,currentsession = obj.currentsession, modeofstudy = txtmodeofstudy.text, level = obj.level, programmeofstudy = txtprogramme.text,faculty = txtfaculty.text, finalsemester = obj.finalsemester },commandtype:commandtype.storedprocedure); } gridcontainer.refresh(); pcontainer.enabled = false; objstate = entitystate.unchanged; } } } catch (exception ex) { metroframework.metromessagebox.show(this, ex.message, "message", messageboxbuttons.ok, messageboxicon.error); } }
Comments
Post a Comment