Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts,

Can you please help me with this,

VB
sqL = "SELECT remittance_no, remit_date, messenger, item, item_value, rate.product, rate.product_value FROM remittance INNER JOIN rate ON rate.product=remittance.item where rate.ratecode = '" & txtRateCode.Text & "' And remittance.messenger = '" & txtMessenger.Text & "' order by remit_date asc"


i want to add date filter, i want to use two datetimepicker for my vb windows forms.

Thank you in advance,

raz
Posted
Updated 3-Dec-15 18:54pm
v2
Comments
George Jonsson 4-Dec-15 0:55am    
So what is your problem?

1 solution

Firstly, your code is at risk of SQL Injection[^] - never concatenate user input into sql statements like this.

Use parameterised queries[^] instead.

One of the advantages of using parameters is also not having to worry about single quote marks on date and text fields.

Your code could be changed as follows
Dim sql As String = "SELECT remittance_no, remit_date, messenger, item, item_value, rate.product, rate.product_value "
sql += "FROM remittance INNER JOIN rate ON rate.product=remittance.item "
sql += "where rate.ratecode = @RateCode And remittance.messenger = @Messenger "
sql += "and remit_date between @Date1 and @Date2 "
sql += "order by remit_date asc"
Then assuming you have something like
VB.NET
Dim command As SqlCommand = New SqlCommand()
and the associated connection etc (or just substitute your variable name for command in the code below.
C#
command.Parameters.AddWithValue("@RateCode", txtRateCode.Text)
command.Parameters.AddWithValue("@Messenger", txtRateCode.Text)

command.Parameters.AddWithValue("@Date1", DateTimePicker1.Value)
command.Parameters.AddWithValue("@Date2", DateTimePicker2.Value)

Note that with DateTimePicker controls you can avoid the time element by using
DateTimePicker1.Value.Date
 
Share this answer
 
Comments
craft_trone 4-Dec-15 20:06pm    
This was a great help.
Thank you very much Chill60

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