vb.net - Search button not responding -


i created search button query ms access database , display results whenever type in id , click search button nothing.

everything else working, copies data vb form , stores in ms access database search button not query database , retrieve data.

below code search button:

private sub button1_click(byval sender system.object, byval e system.eventargs) handles btnsearch.click     dim found boolean      try         cm = new oledb.oledbcommand         cm             .connection = cn             .commandtype = commandtype.text             .commandtext = "select* tblinfo (id = @id & txtfind.text = @id)"             dr = .executereader         end         while dr.read()             found = true             txtfirst1.text = dr("pfirst").tostring             txtmid1.text = dr("pmiddle").tostring             txtlast1.text = dr("plast").tostring             txtadd1.text = dr("paddress").tostring             txtphone1.text = dr("pphone").tostring             txtcontact1.text = dr("pcontact").tostring          end while         cn.close()         exit sub         if found = false msgbox("patient id not found!", msgboxstyle.critical)         dr.close()      catch ex exception      end try 

how can solve this?

the issue attempt pass value of txtfind.text command.

you want use parameters:

with cm     .connection = cn     .commandtype = commandtype.text     .commandtext = "select * tblinfo id = ?"     .parameters.add("@id", oledbtype.[type]).value = txtfind.text     dr = .executereader end 

note have used oledbtype.[type]. want replace data type have specified column id.

it looks have missed opening connection before calling executereader():

cn.open() 

i consider implementing using:

sometimes code requires unmanaged resource, such file handle, com wrapper, or sql connection. using block guarantees disposal of 1 or more such resources when code finished them. makes them available other code use.

here sample code:

using con new oledbconnection(connectionstring),       cmd new oledbcommand("select * tblinfo id = ?", con)      con.open()      cmd.parameters.add("@id", oledbtype.[type]).value = txtfind.text     dr = cmd.executereader()      ...  end using 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -