Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi..all
I'm newbie in VB.net
Please Help me..
How to search data by textbox and datetimepicker access 2003 database in VB.net

Exp : I want to search for data based on the Part name and two datetimepicker

I try with code like this but the results are not in line with expectations

VB
Private Sub SearchData()
con.Open()
        Dim dt As New DataTable
        Dim ds As New DataSet
        ds.Tables.Add(dt)
        Dim da As New OleDbDataAdapter
        'da = New OleDbDataAdapter(com)
        If cbaktif.Checked = True Then
            da = New OleDbDataAdapter("Select * from AFINITY where Description = '" & TextBox1.Text & "' and " & " Datetgl between #" & DateTimePicker1.Text & "# and #" & DateTimePicker2.Text & "# ", con)
        Else
            da = New OleDbDataAdapter("Select * from AFINITY where Description like '%" & TextBox1.Text & "%'", con)
        End If
        da.Fill(dt)
        DatagridView1.DataSource = dt.DefaultView
        con.Close()
End Sub


Please Correction in my coding

Thanks..
Posted
Updated 29-Oct-11 8:04am
v2

1 solution

I am not sure what your exact problem is. I can imagine that the dates are not formatted to your expectations.
First of all I really recommend making your query parameterized (not doing so may result in performance issues and SQL injection!). Consider the following: Someone types D'Artagnan in your TextBox. The ' in D'Artagnan will mess up your query since it means the end of a string in access.
Instead use this:
VB
Dim cmd As New OleDbCommand("Select * from AFINITY where Description = @Description and Datetgl between @Datetgl1 and @Datetgl2 ", con)
cmd.Parameters.AddWithValue("@Description", TextBox1.Text)
cmd.Parameters.AddWithValue("@Datetgl1", DateTimePicker1.Value)
cmd.Parameters.AddWithValue("@Datetgl2", DateTimePicker2.Value)
Dim da As New OleDbDataAdapter(cmd)

.NET will now automatically replace @Description with the TextBox1.Value, but will also make sure that it is used as parameter as a whole (including anything that would normally break your query like */ and ' ).
Your query is now parameterized, which means queries will be cached and parameters will always be valid. So D'Artagnan will now be a valid entry in your TextBox.
Furthermore I think (though I can't say with certainty) that this approach (and using DateTimePicker.Value) will solve your other problem.
 
Share this answer
 
v2
Comments
caello 30-Oct-11 3:58am    
thank you for the response
but after I tried out the following message "object reference not set to an instance of an object".
sorry...i am very newbie..
please... explain to detail..

Thanks....very thanks..
Sander Rossel 30-Oct-11 6:27am    
Easy, but I'd have to see your code for that.
You are using an Object that you have not instantiated.
For example:
Dim cmd As OleDbCommand ' No New keyword.
cmd.DoSomething ' NullReferenceException because the cmd cannot do something if it is not instantiated.

Dim cmd As New OleDbCommand ' With New keyword.
cmd.DoSomething ' No Exception, the Object exists.
caello 30-Oct-11 9:18am    
<pre lang="vb">Public Class FrSearch
Dim con As New OleDbConnection 'Dim MyConnection As New AccessData.DatabaseConnection
Dim com As OleDbCommand
Dim da As OleDbDataAdapter
Dim objdatareader As OleDbDataReader
Dim strSQL As String
Dim ds As New DataSet
Dim dt As New DataTable
Private Sub FrSearch_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0; data source = ..\ASKI.mdb"

con.Open()
con.Close()
end sub
Private Sub dataDes()

con.Open()
Dim dt As New DataTable
Dim ds As New DataSet
ds.Tables.Add(dt)

'If cbaktif.Checked = True Then
Dim com As New OleDbCommand("Select * from AFINITY where Description = @Description and Tanggal between @Datetgl1 and @Datetgl2 ", con)
com.Parameters.AddWithValue("@Description", TextBox1.Text)
com.Parameters.AddWithValue("@datetgl1", datetgl1.Value)
com.Parameters.AddWithValue("@Datetgl2", datetgl2.Value)
Dim da As New OleDbDataAdapter(com)

'Else
'da = New OleDbDataAdapter("Select * from AFINITY where Description like '%" & TextBox1.Text & "%'", con)
'End If
da.Fill(dt)
DatagridView1.DataSource = dt.DefaultView
con.Close()

End Sub
Private Sub search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles search.Click

Try
con.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0; data source = ..\ASKI.mdb"

dataDes()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
con.Close()


End Sub</pre>
Sander Rossel 30-Oct-11 9:47am    
Where does the Exception occur? Go to Debug -> Exceptions... and tick Common Language Runtime Exceptions Thrown. You should now be able to see at which line exactly the exception occurs. Perhaps datetgl1/2.Value is nothing. The code above looks fine. Although you do create an Object called 'com' at the Class level and then create another Object called 'com' in dataDes. You add Parameters to com and you pass com to your DataAdapter. If it takes the wrong com then that might also be the cause of your troubles.
Same goes for 'da'. You have multiple Objects with the same name defined at different levels. You also instantiate dt and ds at Class level and then create new Objects in dataDes. Lots of redundancy and unused Objects...
caello 30-Oct-11 10:11am    
yes because I do not know if my code is correct or not, because I learn a new one this month, it was learned from there and ask questions here.
btw can you give examples of how the code is correct as my case? ..
thanks very much your advice n correction in my code..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900