0
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim cn As New SqlConnection
    Dim ds As New DataSet
    Dim dt As New DataTable
    Dim dfrom As DateTime
    Dim dto As DateTime
    Dim da As New SqlDataAdapter

    dfrom = dtpicker1.Text
    dto = dtpicker2.Text

    cn.ConnectionString = "Data Source=JMI-PC\SQLEXPRESS;Initial Catalog=student_system;User Id=ian;Password=rockstar"
    cn.Open()
    Dim str As String
    Format(dtpicker1.Text, "yyyy-MM-dd")
    Format(dtpicker2.Text, "yyyy-MM-dd")
    str = "select Exam_Date from class1  where Exam_Date= '" & dtpicker1.Text & "' and Exam_Date='" & dtpicker2.Text & "'"

    da = New SqlDataAdapter(str, cn)
    da.Fill(dt)
    DataGridView1.DataSource = dt
    DataGridView1.DataSource = dt


End Sub

i am trying to view data between two dates using the datetimepicker and when i try to run this code i get an error aying " Conversion failed when converting date and/or time from character string." if anyone can show me how its done, i would really appreciate it

1 Answer 1

2

You need to check your date range in your SQL statment

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

Dim cn As New SqlConnection
Dim ds As New DataSet
Dim dt As New DataTable
Dim dfrom As DateTime = dtpicker1.Value
Dim dto As DateTime = dtpicker2.Value


cn.ConnectionString = "Data Source=JMI-PC\SQLEXPRESS;Initial Catalog=student_system;User Id=ian;Password=rockstar"
cn.Open()
Dim str As String = "select Exam_Date from class1  where Exam_Date >= '" & Format(dFrom, "MM-dd-yyyy") & "' and Exam_Date <='" & Format(dto, "MM-dd-yyyy") & "'"

Dim da As SqlDataAdapter = New SqlDataAdapter(str, cn)
da.Fill(dt)
DataGridView1.DataSource = dt

End Sub

Sign up to request clarification or add additional context in comments.

1 Comment

I think it's nicer to use BETWEEN when getting a date range, what do you think? e.g. SELECT Exam_Date FROM class1 where Exam_Date BETWEEN x AND y. Also, your code seems to cause the error 'Value' is not a member of 'Date, remove the value bit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.