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 columnid
.
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
Post a Comment