Search value by textbox from .mdb file

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2008
Posts: 63
Reputation: 4advanced is an unknown quantity at this point 
Solved Threads: 10
4advanced 4advanced is offline Offline
Junior Poster in Training

Re: Search value by textbox from .mdb file

 
0
  #11
Feb 5th, 2009
Like Jx_Man says.... use that condition in your select statement. For example like this : SELECT * from UMP WHERE ID = yourID -> 'yourID' is the ID which you are looking for.

A better approach is : use parameterized queries so instead of yourID you can insert at that point @yourID and declare a parameter object, insert the correct value in it and attach the parameter to the commandobject! This way, it's sql-injection safe....
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 78
Reputation: kerek2 is an unknown quantity at this point 
Solved Threads: 0
kerek2 kerek2 is offline Offline
Junior Poster in Training

Re: Search value by textbox from .mdb file

 
0
  #12
Feb 5th, 2009
Can you give a example plz?...because i'm already try but not ok..
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 63
Reputation: 4advanced is an unknown quantity at this point 
Solved Threads: 10
4advanced 4advanced is offline Offline
Junior Poster in Training

Re: Search value by textbox from .mdb file

 
0
  #13
Feb 5th, 2009
Okay, here it is :

  1. Private Sub cmdSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
  2.  
  3. 'variabeles (objects and strings)
  4. Dim _root As String = String.Empty
  5. Dim _Con As OleDb.OleDbConnection = Nothing
  6. Dim _WhereClause As String = String.Empty
  7. Dim _Parameter As OleDbParameter = Nothing
  8. Dim _DataTable As DataTable = Nothing
  9. Dim _Com As OleDbCommand = Nothing
  10. Dim _DataAdapter As OleDbDataAdapter
  11.  
  12. 'fill string variabeles
  13. _root = "yourPathToDatabase\MyDataBase.mdb"
  14. _Con = New OleDb.OleDbConnection(String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User Id=admin;Password=;", _root))
  15.  
  16. 'create objects
  17. _Com = New OleDbCommand
  18. _DataTable = New DataTable("Customers")
  19. _DataAdapter = New OleDbDataAdapter
  20.  
  21. 'check if the searchstring is numeric (if so, the search for an ID has been performed)
  22. If IsNumeric(txtSearch.Text) Then
  23. _WhereClause = "ID=@Parameter"
  24. Else
  25. _WhereClause = "LCASE(Customer_LastName)=@Parameter"
  26. End If
  27.  
  28. Try
  29. 'open the connection to the database
  30. _Con.Open()
  31.  
  32. 'fill the command with the selectquery and attach a parameter
  33. With _Com
  34. .CommandText = String.Format("SELECT * FROM Customers WHERE {0}", _WhereClause)
  35. .Parameters.AddWithValue("@Parameter", txtSearch.Text.ToLower)
  36. .Connection = _Con
  37. End With
  38.  
  39. 'attach command to dataadapter and fill the datatable
  40. _DataAdapter.SelectCommand = _Com
  41. _DataAdapter.Fill(_DataTable)
  42.  
  43. Catch ex As Exception
  44. 'inform the user something has happened
  45. MessageBox.Show(ex.Message)
  46. Finally
  47.  
  48. 'if the connection is open, close it
  49. If _Con.State = ConnectionState.Open Then _Con.Close()
  50. 'dispose and remove objects
  51. _Con.Dispose()
  52. _Con = Nothing
  53. _Com.Dispose()
  54. _Com = Nothing
  55. End Try
  56.  
  57. 'attach the datatable to the datagridview
  58. Me.dgvCustomers.DataSource = _DataTable
  59.  
  60. 'dispose and remove objects
  61. _DataAdapter.Dispose()
  62. _DataAdapter = Nothing
  63. _DataTable.Dispose()
  64. _DataTable = Nothing
  65.  
  66. End Sub
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 78
Reputation: kerek2 is an unknown quantity at this point 
Solved Threads: 0
kerek2 kerek2 is offline Offline
Junior Poster in Training

Re: Search value by textbox from .mdb file

 
0
  #14
Feb 5th, 2009
Thank you.....but i still struck bro.....can u add something to my previous coding?,,,,,....i still not understand...so dumb...plz sir...
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 63
Reputation: 4advanced is an unknown quantity at this point 
Solved Threads: 10
4advanced 4advanced is offline Offline
Junior Poster in Training

Re: Search value by textbox from .mdb file

 
1
  #15
Feb 5th, 2009
No sorry, it's time to find out yourself....
I coded an example and added comments to it, so you know what steps to take.

Now it's up to you.......
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 252
Reputation: ProfessorPC is an unknown quantity at this point 
Solved Threads: 27
ProfessorPC ProfessorPC is offline Offline
Posting Whiz in Training

Re: Search value by textbox from .mdb file

 
0
  #16
Feb 5th, 2009
took a quick, very quick look. to add the condition you need to finish the WHERE
  1. cmd = New OleDbCommand("SELECT * from UMP WHERE ", cn)
  2. 'to
  3. cmd = New OleDbCommand("SELECT * from UMP WHERE field like " & txtbox.text & "%", cn)
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Search value by textbox from .mdb file

 
0
  #17
Feb 5th, 2009
took a quick, very quick look. to add the condition you need to finish the WHERE
cmd = New OleDbCommand("SELECT * from UMP WHERE ", cn)
'to
cmd = New OleDbCommand("SELECT * from UMP WHERE field like " & txtbox.text & "%", cn)
Here's a one more correction. Single quotes are needed around textual parameters
cmd = New OleDbCommand("SELECT * from UMP WHERE field like '" & txtbox.text & "%'", cn)
that is, if the "field" is textual field. If the "field" is numeric (integer), use
cmd = New OleDbCommand("SELECT * from UMP WHERE field =" & CInt(txtbox.text) , cn)
and trap possible errors.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC