c# - insert multiple rows to database looping -
what wrong code achieve insert rows database clear textbox value , enter record.. problem @ first loop data added successfully...but on next iteration loops add empty strings rows of database want accept input textbox , continue iterating...please try read code , me ...it been 2 weeks since trying solve .. again want -user enter number of group members - group members have column ,first name,last name gender,city.etc group members (eg:6 group membrs) add 6 rows of of different column added code add first rows , rest 5 row's empty data sorry bad english..somebody please try think thinking
{{ private void btnaddloan_click(object sender, routedeventargs e) if (txtname.text != "" && txtlname.text != "") { int c=0; int input=int.parse(txttotalnumberofgroupmembers.text); { string connstr = "data source=ger-pc\\pleasegod;initial catalog=acsi;integrated security=true"; sqlconnection con = new sqlconnection(connstr); con.open(); sqlcommand cmd = new sqlcommand("insert customer(firstname,lastname) values(@n,@p)", con); cmd.parameters.addwithvalue("@p", txtname.text); cmd.parameters.addwithvalue("@n", txtlname.text); cmd.executenonquery(); con.close(); lblnotify.content = c + 1 + "" + "members added"; //clear textbox values , wait input txtname.text = ""; txtlname.text = ""; if (txtname.text != "" && txtlname.text != "") { continue; } else { messageboxresult result =messagebox.show("procces","continue adding memebers",messageboxbutton.yesnocancel,messageboximage.warning); //txtname.text = s; //txtlname.text= s1; //messagebox.show(); switch (result) { case messageboxresult.yes: if (txtname.text != "") { } else { } break; case messageboxresult.no: break; case messageboxresult.cancel: break; } } c++; } while (c < input); } else { messagebox.show("plese fill first name , last name"); } }
you're clearing txtname.text , txtlname.text value after insert. in next iteration you're recreating parameters, time text values empty.
txtname.text = ""; txtlname.text = "";
instead of recreating command object, why not execute command object n times. like:
sqlcommand cmd = new sqlcommand("insert customer(firstname,lastname) values(@n,@p)", con); cmd.parameters.addwithvalue("@p", txtname.text); cmd.parameters.addwithvalue("@n", txtlname.text); for(int = 0; < input; i++) { cmd.executenonquery(); }
Comments
Post a Comment