c# - How can I create an if else statement with this situatiion? -
i want command if enter nothing or null value not execute insert command.im using c# , sql server. code this:
sqlcommand cmd = con.createcommand(); cmd.commandtext = "insert records ([student id], [first name], [last name], [middle initial], gender, address, status, year, email, course, [contact number]) values ( @studentid, @firstname, @lastname , @middleinitial, @gender, @address, @status, @year, @email, @course, @contactnumber)"; sqlparameter p1 = new sqlparameter("@studentid", sqldbtype.nchar); p1.value = textbox1.text; cmd.parameters.add(p1); sqlparameter p2 = new sqlparameter("@firstname", sqldbtype.nvarchar); p2.value = textbox2.text; cmd.parameters.add(p2); sqlparameter p3 = new sqlparameter("@lastname", sqldbtype.nvarchar); p3.value = textbox3.text; cmd.parameters.add(p3); sqlparameter p4 = new sqlparameter("@middleinitial", sqldbtype.nchar); p4.value = combobox1.text; cmd.parameters.add(p4); sqlparameter p5 = new sqlparameter("@gender", sqldbtype.nchar); p5.value = combobox2.text; cmd.parameters.add(p5); sqlparameter p6 = new sqlparameter("@address", sqldbtype.varchar); p6.value = textbox4.text; cmd.parameters.add(p6); sqlparameter p7 = new sqlparameter("@status", sqldbtype.nchar); p7.value = combobox3.text; cmd.parameters.add(p7); sqlparameter p8 = new sqlparameter("@year", sqldbtype.varchar); p8.value = combobox4.text; cmd.parameters.add(p8); sqlparameter p9 = new sqlparameter("@email", sqldbtype.varchar); p9.value = textbox5.text; cmd.parameters.add(p9); sqlparameter p10 = new sqlparameter("@course", sqldbtype.varchar); p10.value = combobox5.text; cmd.parameters.add(p10); sqlparameter p11 = new sqlparameter("@contactnumber", sqldbtype.varchar); p11.value = textbox6.text; cmd.parameters.add(p11); textbox1.text = ""; textbox2.text = ""; textbox3.text = ""; textbox4.text = ""; textbox5.text = ""; textbox6.text = ""; combobox1.text = ""; combobox2.text = ""; combobox3.text = ""; combobox4.text = ""; combobox5.text = ""; messagebox.show("data inserted!", "information ... ", messageboxbuttons.ok, messageboxicon.information, messageboxdefaultbutton.button1); cmd.executenonquery(); con.close();
can generate constraint in table, when can try insert null values, , in program can exception.
other alternative make loop , check values in paramaters on cmd.parameters, , if found 1 null, nothing or blank should not send insert database.
Comments
Post a Comment