mysql - how to loop through a data base for 21 button captions -


ok i'm banging head on keyboard again. form here far did mysql query read 1 field wanted. issue this:

concept. table holds button data, caption, , other stuff latter on. tab button activate on click. <-- later not important right once figure out part i'm asking can figure out i'm sure. so. have 21 buttons. each button needs caption located in database. i'm trying figure out how loop through database caption botton 1, button two, , on. right loading same caption 21 buttons. here code:

private sub frm_mainconsole_load(sender object, e eventargs) handles mybase.load     'todo: line of code loads data 'posdbdataset.button_cat' table. can move, or remove it, needed.     me.button_cattableadapter.fill(me.posdbdataset.button_cat)      'procedures     me.show()     ' variables      dim query string     dim command mysqlcommand     dim reader mysqldatareader     dim targetbutton button      try         'for button 1 through 21         dbconn()         integer = 1 21 step 1             query = "select btn_caption button_cat"             command = new mysqlcommand(query, conn)             reader = command.executereader()             reader.read()             'btn_cat1.text = reader("btn_caption")             'get button controls container             targetbutton = controls("btn_cat" & i)             targetbutton.text = reader("btn_caption")             reader.close()         next         conn.close()     catch ex mysqlexception         messagebox.show(ex.message)             conn.dispose()     end try   end sub 

i appreciate can give me. learn seeing code , manipulating need do.

i'm using visual studio 2015 community , mysql database , writing in visual basic.

you rerunning query during every loop cycle.

do this:

    ''for button 1 through 21     dbconn()      query = "select btn_caption button_cat"     command = new mysqlcommand(query, conn)     reader = command.executereader()      integer = 1 21 step 1          reader.read()         ''btn_cat1.text = reader("btn_caption")         ''get button controls container         targetbutton = controls("btn_cat" & i)         targetbutton.text = reader("btn_caption")      next     reader.close()     conn.close() 

keep in mind code potentially problematic in assumes there 21 results returned database.

if want prevent problems if there fewer 21 records returned, can this:

    ''for button 1 through 21     dbconn()      query = "select btn_caption button_cat"     command = new mysqlcommand(query, conn)     reader = command.executereader()      integer = 1 21 step 1           if not reader.read()              ''we out of records. exit loop.              exit          end if          ''btn_cat1.text = reader("btn_caption")         ''get button controls container         targetbutton = controls("btn_cat" & i)         targetbutton.text = reader("btn_caption")      next     reader.close()     conn.close() 

you can rewrite while loop, need re-implement i counter.


Comments