I am working on user module in my vb.net Windows Form application. So I have a SQL table that has three columns with data type "Date". I want to extract records that fall in specified date range and display them in a listview. My form looks like this:
For the apply button, I have written following function:
'FUNCTION: APPLY_DATE_FILTER(): FILTER THE LIST ACCORDING TO SELECTED DATE COLUMN
Private Sub apply_date_filter(ByVal radio_create As RadioButton, ByVal radio_lastlogin As RadioButton, ByVal radio_lastedit As RadioButton, ByVal date1 As DateTimePicker, ByVal date2 As DateTimePicker, ByVal list As ListView)
Dim sel As String
If list.Items.Count = 0 Then
If radio_create.Checked = True Then
sel = "SELECT * FROM user_master WHERE created BETWEEN '" & date1.Text & "' AND '" & date2.Text & "'"
ElseIf radio_lastedit.Checked = True Then
sel = "SELECT * FROM user_master WHERE lastchange BETWEEN '" & date1.Text & "' AND '" & date2.Text & "'"
ElseIf radio_lastlogin.Checked = True Then
sel = "SELECT * FROM user_master WHERE lastlogin BETWEEN '" & date1.Text & "' AND '" & date2.Text & "'"
End If
Dim cnn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\RSMS_DB.mdf;Integrated Security=True;User Instance=True")
Dim da As New SqlDataAdapter(sel, cnn)
Dim ds As New DataSet
da.Fill(ds)
If ds.Tables(0).Rows.Count = 0 Then
MsgBox("There are no users found within the date-range you specified! Please try again with a valid entry!", MsgBoxStyle.OkOnly, "USER NOT FOUND")
Else
For m As Integer = 0 To ds.Tables(0).Rows.Count - 1
Dim lvi As New ListViewItem
lvi.Text = ds.Tables(0).Rows(m)(0).ToString
lvi.SubItems.Add(ds.Tables(0).Rows(m)(1).ToString)
lvi.SubItems.Add(ds.Tables(0).Rows(m)(2).ToString)
lvi.SubItems.Add(ds.Tables(0).Rows(m)(5).ToString)
lvi.SubItems.Add(ds.Tables(0).Rows(m)(7).ToString)
lvi.SubItems.Add(ds.Tables(0).Rows(m)(8).ToString)
lvi.SubItems.Add(ds.Tables(0).Rows(m)(9).ToString)
If ds.Tables(0).Rows(m)(10).ToString = "1" Then
lvi.SubItems.Add("Administrator")
ElseIf ds.Tables(0).Rows(m)(10).ToString = "0" Then
lvi.SubItems.Add("Ordiary")
End If
list.Items.Add(lvi)
Next
End If
End If
End Sub
When I click apply, I get an error at da.Fill(ds) saying, "Conversion failed when converting date and/or time from character string." Thanks in advance. :)