multiple SQL queries stored as vb.net variable -
i know there few other posts putting sql results vb.net variable couldn't wrap head around how did , how in project.
so i'm trying query database 4 different values , display each value in part of form. going store each value variable , textbox1.text = i
updated code 10/5/2014
private sub lbmembers_selectedindexchanged(sender object, e eventargs) handles lbmembers.selectedindexchanged dim string = lbmembers.selecteditem dim dbprovider = "provider=microsoft.jet.oledb.4.0;" dim dbsource = "data source= c:\members.mdb " dim sqlquery string = "select starttime, endtime, shipcode, cycletime, workpercent, share tblmembers fellow member = @id;" using con = new oledb.oledbconnection(dbprovider & dbsource) using cmd = new oledb.oledbcommand(sqlquery, con) con.open() cmd.parameters.addwithvalue("@id", oledb.oledbtype.varwchar).value = using reader = cmd.executereader() if reader.read() tbtimestart.text = reader.tostring(0) tbtimeend.text = reader.tostring(1) dim j = convert.toint32(reader.tostring(2)) tbtimecycle.text = reader.getint32(3).tostring tbmemberpercent.text = reader.getint32(4) tbmembershare.text = reader.getint32(5) if j = 1 rbpro.checked = true elseif j = 2 rbret.checked = true elseif j = 3 rbcov.checked = true elseif j = 4 rbskiff.checked = true elseif j = 5 rbmack.checked = true elseif j = 6 rbhulk.checked = true else rbpro.checked = false rbret.checked = false rbcov.checked = false rbskiff.checked = false rbmack.checked = false rbhulk.checked = false exit sub end if end if end using con.close() end using end using end sub
the exception
invalidcastexception unhandled specified cast not valid.
pops on line tbtimecycle.text = reader.getint32(3).tostring
the reader reading "system.data.oledb.oledbdatareader" when insert test code "tbgrossisk.text = reader.tostring()" using statement
if syntax of select statement see can request column of table 1 query.
private sub listbox1_selectedindexchanged(sender object, e eventargs) handles lbmembers.selectedindexchanged if string.isnullorwithespace(listbox1.selecteditem.tostring()) messagebox.show("select item listbox") end if dim fellow member string = listbox1.selecteditem dim dbprovider = "provider=microsoft.jet.oledb.4.0;" dim dbsource = "data source= c:\members.mdb " dim sqlquery string = "select starttime, endtime, shipcode, cycletime " & _ "from tblmembers fellow member = @id;" using con = new oledb.oledbconnection(dbprovider & dbsource) using cmd = new oledb.oledbcommand(sqlquery, con) con.open() cmd.parameters.addwithvalue("@id", oledb.oledbtype.varwchar).value = fellow member using reader = cmd.executereader if reader.read() textbox1.text = reader.getstring(0) textbox2.text = reader.getstring(1) dim j = reader.getint32(2) if j = 1 radio1.checked = true elseif j = 2 radio2.checked = true elseif j = 3 radio3.checked = true elseif j = 4 radio4.checked = true elseif j = 5 radio5.checked = true elseif j = 6 radio6.checked = true else exit sub end if textbox4.text = reader.getint32(3).tostring() else messagebox.show("the search '" & fellow member & "' doesn't find data") end if end using end using end using end sub
instead of using executescalar returns first column of first row retrieved utilize oledbdatareader
returned method executereader
. object allows access various column in current record using index (or column name). read more oledbdatareader in msdn
sql vb.net variables ms-access-2007
No comments:
Post a Comment