Inserting All non empty textboxes into Access with C# -
forgive code, new c#. anyways, trying loop through textbox controls , assign non empty values table. right inserting first textbox control's text each field. thought using foreach assign each textbox's value , can insert table.
also, unsure of how make checks if textboxes left empty, modify insert query accordingly. example, if first row of textboxes filled out none of rest, how insert values fields relevant , not fields?
here code:
private void insertdata() { using (dbconn) { dbconn.open(); using (var dbcmd = new oledbcommand("insert members (husband, birthday, phone, email, address, status, spouse, spouse_phone, spouse_email, " + "anniversary, spouse_status, child1, child1_birthday, child1_email, " + "child2, child2_birthday, child2_email, child3, child3_birthday, child3_email, child4, child4_birthday, child4_email, child5, child5_birthday, child5_email," + "child6, child6_birthday, child6_email, child7, child7_birthday, child7_email) " + "values (@husband, @birthday, @phone, @email, @address, @status, @spouse, @spouse_phone, @spouse_email, @anniversary, @spouse_status," + "@child1, @child1_birthday, @child1_email, " + "@child2, @child2_birthday, @child2_email, @child3, @child3_birthday, @child3_email, @child4, @child4_birthday, @child4_email," + "@child5, @child5_birthday, @child5_email, @child6, @child6_birthday, @child6_email, @child7, @child7_birthday, @child7_email)", dbconn)) { try { insertdbparameters(dbcmd); dbcmd.executenonquery(); } catch (oledbexception ex) { messagebox.show(ex.tostring()); return; } } messagebox.show("record inserted."); } } private void insertdbparameters(oledbcommand cmd) { list<string> checkboxes = new list<string>(); foreach (control cbox in controls) { if (cbox checkbox && ((checkbox)cbox).checked) { checkboxes.add("member"); } else if (cbox checkbox && !((checkbox)cbox).checked) { checkboxes.add("regular attender"); } } foreach (control c in controls) { if (c textbox) { if (!string.isnullorwhitespace(c.text)) { cmd.parameters.addwithvalue("@husband", c.text); cmd.parameters.addwithvalue("@birthday", c.text); cmd.parameters.addwithvalue("@phone", c.text); cmd.parameters.addwithvalue("@email", c.text); cmd.parameters.addwithvalue("@address", c.text); cmd.parameters.addwithvalue("@status", checkboxes[0]); cmd.parameters.addwithvalue("@spouse", c.text); cmd.parameters.addwithvalue("@spouse_phone", c.text); cmd.parameters.addwithvalue("@spouse_email", c.text); cmd.parameters.addwithvalue("@anniversary", c.text); cmd.parameters.addwithvalue("@spouse_status", checkboxes[0]); cmd.parameters.addwithvalue("@child1", c.text); cmd.parameters.addwithvalue("@child1_birthday", c.text); cmd.parameters.addwithvalue("@child1_email", c.text); cmd.parameters.addwithvalue("@child2", c.text); cmd.parameters.addwithvalue("@child2_birthday", c.text); cmd.parameters.addwithvalue("@child2_email", c.text); cmd.parameters.addwithvalue("@child3", c.text); cmd.parameters.addwithvalue("@child3_birthday", c.text); cmd.parameters.addwithvalue("@child3_email", c.text); cmd.parameters.addwithvalue("@child4", c.text); cmd.parameters.addwithvalue("@child4_birthday", c.text); cmd.parameters.addwithvalue("@child4_email", c.text); cmd.parameters.addwithvalue("@child4", c.text); cmd.parameters.addwithvalue("@child4_birthday", c.text); cmd.parameters.addwithvalue("@child4_email", c.text); cmd.parameters.addwithvalue("@child5", c.text); cmd.parameters.addwithvalue("@child5_birthday", c.text); cmd.parameters.addwithvalue("@child5_email", c.text); cmd.parameters.addwithvalue("@child5", c.text); cmd.parameters.addwithvalue("@child5_birthday", c.text); cmd.parameters.addwithvalue("@child5_email", c.text); cmd.parameters.addwithvalue("@child6", c.text); cmd.parameters.addwithvalue("@child6_birthday", c.text); cmd.parameters.addwithvalue("@child6_email", c.text); cmd.parameters.addwithvalue("@child7", c.text); cmd.parameters.addwithvalue("@child7_birthday", c.text); cmd.parameters.addwithvalue("@child7_email", c.text); } } } }
apologies if being unclear, try add more information if saying unclear.
appreciate help.
thanks!
here screenshot of program running if helps.
Comments
Post a Comment