c# - Populating multiple user controls from a database -
i need populate user controls in group box (from database) based on user selects in 1 of controls.
each control populated column table in database. values null, strings, , integers. query return 1 row of data.
the group box contains many different types of controls; however, need populate read text boxes.
below how went solving this. code works, seems clunky me. ie. nested try/catch , nested loops/conditional statements.
is there simpler/better/cleaner way accomplish this?
erpdb.sqlgetdatareader
returns takes sql statement string argument , returns oracledatareader
.
sqlstring
takes user input user control , returns a sql query string multiple columns.
private void populatereadonlycolumns() { oracledatareader dr = erpdb.sqlgetdatareader(sqlstring(userselection)); while (dr.read()) { int = 0; foreach (control control in groupbox1.controls) { if( control textbox) { textbox txtbox = (textbox)control; if (txtbox.readonly == true) { safestring(dr, txtbox, i); i++; } } } } } private void safestring(oracledatareader dr, control control, int index) { try { try { control.text = dr.getstring(index); } catch { control.text = convert.tostring(dr.getint32(index)); } } catch { control.text = ""; } } private string sqlstring(string userselection) { stringbuilder sb = new stringbuilder(); sb.appendline("select description"); sb.appendline(" ,job_title"); sb.appendline(" ,category"); sb.appendline(" ,risk_code"); sb.appendline(" ,exempt_flag"); sb.appendline(" ,pay_grade"); sb.appendline(" emp_jobs"); sb.appendline(" job_code = '" + userselection.trim() + "'"); return sb.tostring(); }
well 1 way can make simpler/cleaner using linq:
int = 0; foreach (textbox txtbox in (from control control in groupbox1.controls control textbox && ((textbox)control).readonly select control)) safestring(dr, txtbox, i++);
Comments
Post a Comment