c# - assign text value to buttons from database in .net? -


i have 30 buttons nothing in default text value. want ordered data database:

here database format:

id       value                   cat 1       button name               2       name              3                       .           .                     .           .                     .           .                     30      last button               1       button name               b 2       name              b 3                       b .           .                     b .           .                     b .           .                     b 18      last button               b 

now have created function grab id , category select specific name using oledb ms access database.

here function:

 private string getitem(int i, string a) {      //database connection     // database query , table slection id = , cat =     read data in selected column     return string form of column  } 

now whenever form loads, i've assigned following function:

 button1.show();       ...       ...   button30.show();   button1.text = getitem(1, a);   button2.text = getitem(2, a);         ..        ...        ...   button30.text = getitem(30, a); 

again there separate button change category change text value in button:

button1.text = getitem(1, b);   button2.text = getitem(2, b);        .        .        .   button18.text = getitem(18, b);   // because have 18 items hide remaining buttons.   button19.hide();            ...   button30.hide(); 

it works , easy, problem is, slow. whenever form loads takes time fill data buttons , when click button change category happens there too.

is there method in array or that? above code requires multiple time access database resulting slow processing.

another problem is, if have 18 or lesser 30 items in database table, pre made buttons visible default values in it.

in above code overcome using buttonx.hide() while x values doesn't have values..

these manual work , required mass codding.

i grateful know method overcome problem.

there lot of messy code there these things:

  • first dont have butttons available time (this solve problem of having stray buttons default text). instead generate , add them on form code (form.controls.add()).

  • instead of doing multiple roundtrips. have datatable in code keep cache of table , instead fetch datatable. (if data volatile might not of use, still instead of query eachitem query whole table @ once)

  • you can use linq query datatable reduce code size. (and make more maintainable)


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -