Question Probleme to search between two dates

mounir_az

Member
Joined
Jan 11, 2009
Messages
18
Programming Experience
1-3
hi,

I have an application vb2005 SQL Server 2005 Express Edition. I want the code to Listre records between two dates.



I use two DateTimePicker for research.

the field (date_traitement) is smalldatetime.



voila my code search:



Dim adp As New SqlDataAdapter ( "SELECT n_dossier as N_Dossier, date_traitement as Date_Traitement, nom_prenom as Nom_Prénom from T_traitement where [date_traitement] between" date1.Value & & " 'and" date2.Value & & "'", con)



but it not it displays the following error:



"The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value."



thank you in advance.
 
Do not use string concatenation to build SQL statements. ALWAYS use parameters to insert varialbles into SQL code, e.g.
VB.NET:
Dim adp As New SqlDataAdapter("SELECT * FROM MyTable WHERE [Date] BETWEEN @StartDate AND @EndDate", con)

With adp.SelectCommand.Parameters
    .AddWithValue("@StartDate", startDatePicker.Value.Date)
    .AddWithValue("@EndDate", endDatePicker.Value.Date)
End With
Note the use of descriptive names for the DateTimePicker controls.
 
Back
Top