Re: ADO.Net Help - Search Database and populate a textbox with the result for a particular row.

db India
  • 9 years ago
    1. Add a class in your project. 2. add the following code in that class imports system.data imports system.data.oledb public class class1 private conn as oledbconnection private adapt as oledbcommand private dset as dataset private tabl as datatable private sqlstr as string public sub new () conn = new oledbconnection("provider= microsoft.jet.oledb.4.0; data source= product.mdb") 'You have to specify your database name with path instead of product.mdb 'however if you have stored the database in appilcations bin\debug directory there is no 'need to specify path end sub private sub generatesql(field as string, table as string, condition as string ) sqlstr = "select " & field &" from " & table &" where "& condition generaterows() end sub private sub generaterows() adapt = new oloedbdataadapter(sqlstr,conn) dset= new dataset adapt.fill(dset) tabl= new datatable tabl= dset.tables(0) end sub public function getvalue(field as string, table as string, condition as string ) as string generatesql(field,table,condition) return tabl.rows.item(0).item(0) end sub end class 3. open your form and put 3 textboxes and name them accordingly. 4. write the following code in the lostfocus event of txtsearch textbox. dim myobj as new class1 ' assuming "item" as tablename ' use the following format ' for text data type enclose '" & var &"' ' for numeric it is " & var &" ' and for date #" & var &"# txtpart.text = myobj.getvalue("partnumber","item","partnumber ='" & txtsearch.text &"'") txtdescription.text = myobj.getvalue("description","item","partnumber ='" & txtsearch.text &"'")

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Java is to JavaScript what Car is to Carpet.” - Chris Heilmann