sql - How to extract data from Access db and place it into a text box using vb.net? -
hi guys i'm trying search employee information using sql ms access, , hoping put fname lname , such details in respective textbox correspond specific employee's id number. have managed make sql work don't know how extract files sql statement , place inside .text(text box), can please guide me? thanks
here code far: (updated code) got error message : additional information: executereader: connection property has not been initialized. highlighting reader below. how can fix this? i'm trying extract data , place textbox? thanks
private sub enumtext_selectedindexchanged(sender object, e eventargs) handles enumtext.selectedindexchanged dim dbsource = "data source= c:\databse\company_db.accdb" con.connectionstring = "provider=microsoft.ace.oledb.12.0; data source= c:\databse\company_db.accdb" dim sqlquery string dim sqlcommand new oledbcommand dim sqladapter new oledbdataadapter dim table new datatable dim empnum string dim empfname string dim emplname string dim empdept string dim empstat string dim empyears string empnum = enumtext.text empfname = empfnametext.text emplname = emplnametext.text empdept = depttext.text empstat = stattext.text empyears = yearstext.text sqlquery = "select * tbl_empinfo empid empnum" sqlcommand .commandtext = sqlquery .connection = con .parameters.addwithvalue("empid", empnum) sqladapter .selectcommand = sqlcommand .fill(table) end datagridview1 .datasource = table end end dim path = "data source= c:\databse\company_db.accdb" dim command = "select * tbl_empinfo empid empnum" querydata(path, command) con.close() end sub private sub querydata(pathdb string, command string) using connection new system.data.oledb.oledbconnection("provider=microsoft.ace.oledb.12.0;data source=" & pathdb) using da new system.data.oledb.oledbcommand(command, connection) connection.open() dim reader = da.executereader() if reader.read() empfnametext.text = reader("fname") emplnametext.text = reader("lname") end if connection.close() end using end using end sub
for this, need classes: connection, command, reader. other classes in code in question, redundant , required in complicated simple case presentation.
private sub querydata(pathdb string, command string) using connection new system.data.oledb.oledbconnection("provider=microsoft.ace.oledb.12.0;data source=" & pathdb) using com new system.data.oledb.oledbcommand(command, connection) connection.open() dim reader = com.executereader() if reader.read() textbox1.text = reader("fname") textbox2.text = reader("lname") end if connection.close() end using end using end sub
call method so:
dim path = "c:\databse\company_db.accdb" dim command = "select * tbl_empinfo empid empnum" querydata(path, command)
edit - use parameters:
private sub querydata(pathdb string, command string, id string) using connection new system.data.oledb.oledbconnection("provider=microsoft.ace.oledb.12.0;data source=" & pathdb) using com new system.data.oledb.oledbcommand(command, connection) com.parameters.addwithvalue("", id) connection.open() using reader = com.executereader() if reader.read() textbox1.text = reader("fname") textbox2.text = reader("lname") end if end using connection.close() end using end using end sub
call method:
dim path = "c:\databse\company_db.accdb" dim command = "select * tbl_empinfo empid = ?" dim empnum = "..." querydata(path, command, empnum)
Comments
Post a Comment