mysql - Searching a database for a string and taking relevant information VB -
i want able search database product name or product id number , load relevant information regarding product text boxes.
my current code has no build errors doesn't seem return information. small , i'm being idiot cant fathom problem.
private sub btnsearchproducts_click(byval sender system.object, byval e system.eventargs) handles btnsearchproducts.click ds.clear() if txtproductname.text = "" , txtproductid.text = "" messagebox.show("please enter either product id or product name..", "authentication error", messageboxbuttons.ok, messageboxicon.error) else if txtproductid.text <> "" each ch char in txtproductid.text if not char.isdigit(ch) txtproductid.clear() msgbox("please enter valid product id. integers valid input", messageboxbuttons.ok, messageboxicon.error) exit sub end if next end if ' connect db dim conn new system.data.oledb.oledbconnection() conn.connectionstring = "provider=microsoft.ace.oledb.12.0;data source=d:\computing project\database1.accdb" dim command new oledb.oledbcommand try dim sql string = "select * tbl_stock s_id = '" & txtproductid.text & "'or s_productname = '" & txtproductname.text & "'" dim sqlcom new system.data.oledb.oledbcommand(sql) 'open database connection sqlcom.connection = conn conn.open() da = new oledb.oledbdataadapter(sql, conn) da.fill(ds, "stock") dim sqlread system.data.oledb.oledbdatareader = sqlcom.executereader() if sqlread.read() txtproductid.text = cint(ds.tables("stock").rows(0).item(0)) txtproductname.text = ds.tables("stock").rows(0).item(3) txtproductprice.text = "£" & cdec(ds.tables("stock").rows(0).item(2)) else if txtproductid.text <> "" msgbox("sorry, " & txtproductid.text & " not valid product id", messageboxbuttons.ok, messageboxicon.error) txtproductid.text = "" end if if txtproductname.text <> "" msgbox("sorry, " & txtproductname.text & " not valid product name", messageboxbuttons.ok, messageboxicon.error) txtproductname.text = "" end if end if catch ex exception end try end if end sub
any appreciated
a few thoughts this, might point right direction.
- it looks combining 2 different logical processes. filling dataset, trying read same query again. rather calling out "read" if statement. count of rows on ds.tables[0] might resolve immediate issue.
- please have @ parameterized sql queries, building raw user entry allows malicious actions , efforts should taken avoid if can.
- looking @ query, looking exact match on id, or name. might switch operation on name rather exact match.
Comments
Post a Comment