943,949 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 3613
  • VB.NET RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Feb 5th, 2009
0

Re: Search value by textbox from .mdb file

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....
Reputation Points: 33
Solved Threads: 10
Junior Poster in Training
4advanced is offline Offline
67 posts
since Nov 2008
Feb 5th, 2009
0

Re: Search value by textbox from .mdb file

Can you give a example plz?...because i'm already try but not ok..
Reputation Points: 20
Solved Threads: 0
Junior Poster
kerek2 is offline Offline
113 posts
since Sep 2007
Feb 5th, 2009
0

Re: Search value by textbox from .mdb file

Okay, here it is :

VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 33
Solved Threads: 10
Junior Poster in Training
4advanced is offline Offline
67 posts
since Nov 2008
Feb 5th, 2009
0

Re: Search value by textbox from .mdb file

Thank you.....but i still struck bro.....can u add something to my previous coding?,,,,,....i still not understand...so dumb...plz sir...
Reputation Points: 20
Solved Threads: 0
Junior Poster
kerek2 is offline Offline
113 posts
since Sep 2007
Feb 5th, 2009
1

Re: Search value by textbox from .mdb file

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.......
Reputation Points: 33
Solved Threads: 10
Junior Poster in Training
4advanced is offline Offline
67 posts
since Nov 2008
Feb 5th, 2009
0

Re: Search value by textbox from .mdb file

took a quick, very quick look. to add the condition you need to finish the WHERE
VB.NET Syntax (Toggle Plain Text)
  1. cmd = New OleDbCommand("SELECT * from UMP WHERE ", cn)
  2. 'to
  3. cmd = New OleDbCommand("SELECT * from UMP WHERE field like " & txtbox.text & "%", cn)
Reputation Points: 31
Solved Threads: 29
Posting Whiz in Training
ProfessorPC is offline Offline
270 posts
since Dec 2007
Feb 5th, 2009
0

Re: Search value by textbox from .mdb file

Quote ...
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.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Datagrid MS Access and VB.Net
Next Thread in VB.NET Forum Timeline: DateTime field - How do I assign a time other than current?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC