| | |
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:
Solved Threads: 10
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....
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....
•
•
Join Date: Nov 2008
Posts: 63
Reputation:
Solved Threads: 10
Okay, here it is :
VB.NET Syntax (Toggle Plain Text)
Private Sub cmdSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSearch.Click 'variabeles (objects and strings) Dim _root As String = String.Empty Dim _Con As OleDb.OleDbConnection = Nothing Dim _WhereClause As String = String.Empty Dim _Parameter As OleDbParameter = Nothing Dim _DataTable As DataTable = Nothing Dim _Com As OleDbCommand = Nothing Dim _DataAdapter As OleDbDataAdapter 'fill string variabeles _root = "yourPathToDatabase\MyDataBase.mdb" _Con = New OleDb.OleDbConnection(String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User Id=admin;Password=;", _root)) 'create objects _Com = New OleDbCommand _DataTable = New DataTable("Customers") _DataAdapter = New OleDbDataAdapter 'check if the searchstring is numeric (if so, the search for an ID has been performed) If IsNumeric(txtSearch.Text) Then _WhereClause = "ID=@Parameter" Else _WhereClause = "LCASE(Customer_LastName)=@Parameter" End If Try 'open the connection to the database _Con.Open() 'fill the command with the selectquery and attach a parameter With _Com .CommandText = String.Format("SELECT * FROM Customers WHERE {0}", _WhereClause) .Parameters.AddWithValue("@Parameter", txtSearch.Text.ToLower) .Connection = _Con End With 'attach command to dataadapter and fill the datatable _DataAdapter.SelectCommand = _Com _DataAdapter.Fill(_DataTable) Catch ex As Exception 'inform the user something has happened MessageBox.Show(ex.Message) Finally 'if the connection is open, close it If _Con.State = ConnectionState.Open Then _Con.Close() 'dispose and remove objects _Con.Dispose() _Con = Nothing _Com.Dispose() _Com = Nothing End Try 'attach the datatable to the datagridview Me.dgvCustomers.DataSource = _DataTable 'dispose and remove objects _DataAdapter.Dispose() _DataAdapter = Nothing _DataTable.Dispose() _DataTable = Nothing End Sub
•
•
Join Date: Dec 2007
Posts: 252
Reputation:
Solved Threads: 27
took a quick, very quick look. to add the condition you need to finish the WHERE
VB.NET Syntax (Toggle Plain Text)
cmd = New OleDbCommand("SELECT * from UMP WHERE ", cn) 'to cmd = New OleDbCommand("SELECT * from UMP WHERE field like " & txtbox.text & "%", cn)
•
•
•
•
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)
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.
Teme64 @ Windows Developer Blog
![]() |
Similar Threads
Other Threads in the VB.NET Forum
- Previous Thread: Datagrid MS Access and VB.Net
- Next Thread: DateTime field - How do I assign a time other than current?
| Thread Tools | Search this Thread |
.net .net2008 30minutes 2005 2008 access account arithmetic array basic binary bing button buttons center check code combobox component connectionstring crystalreport data database databasesearch datagrid datagridview date design dissertation dissertations dropdownlist excel fade file-dialog filter folder ftp generatetags google hardcopy images input insert intel internet mobile monitor ms net networking objects output panel passingparameters peertopeervideostreaming picturebox picturebox1 port position print printing problem problemwithinstallation project save searchbox searchvb.net select serial shutdown soap survey table tcp temperature text textbox timer timespan toolbox trim update updown user vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web winforms wpf year





