i have particular part of inventory interface requires employee select or name combo box , scan product table assigned name of employee.
my curiosity is: when hitting edit
, add
or delete
button knows table perform function in switch - case statement employee name on it. problem is, piece of code long each employee, 9 employees each have switch - case statement.
any advice on how simplify or shorten code? understand in advance parameterized sql failing use. trying accomplish first.
private void btnadd_click(object sender, eventargs e) { activecontrol = txtserialn; if (!string.isnullorempty(txtserialn.text) && !string.isnullorempty(cboempname.text)) switch (cboempname.selecteditem.tostring().trim()) { case "john doe": try { connection.open(); oledbcommand command = new oledbcommand(); command.connection = connection; command.commandtext = "insert johndoe(serialnumber,partnumber,dateentered,customer) values ('" + txtserialn.text + "','" + txtpart.text + "','" + txtdate.text + "','" + txtcustomer.text + "')"; command.executenonquery(); messagebox.show("inventory added".padleft(23)); connection.close(); txtserialn.clear(); txtpart.clear(); txtdate.clear(); txtcustomer.clear(); command.commandtext = "select * johndoe order partnumber"; oledbdataadapter db = new oledbdataadapter(command); datatable dt = new datatable(); db.fill(dt); datagridempparts.datasource = dt; } catch (oledbexception) { string strmsg = "this serial number exists ! , please try again"; messagebox.show(strmsg, "you can't enter same 1 again", messageboxbuttons.ok, messageboxicon.exclamation, messageboxdefaultbutton.button1); connection.close(); } break; } }
i rather put lookup table have columns such employeename, assignedtable , dynamically construct commandtext based on parameter values.