Search Data between two dates using VB.Net and sql server RRS feed

  • Question

  • Hi, I'm new to programming. I have created a small program with and I want to show data in the data grid view between two dates interval. I have written code for this function but its not showing the exact date range. kindly looking for someone help to fix this issue.

    my code,

    Imports System.Data.SqlClient
    Public Class Report1
        Dim con As New SqlConnection
        Private Sub SalesReportBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)

        End Sub

        Private Sub Report1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'SalesDataSet.SalesReport' table. You can move, or remove it, as needed.

        End Sub

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

            Me.SalesReportBindingSource.Filter = "Date >= '" & txtFrom.Text & "' and Date <= '" & txtTo.Text & "'"

    End Sub

    End Class

    Wednesday, September 19, 2018 5:16 AM

All replies

  • Hi,

    It is a demo, first populate the DataGridview, then filter

    Imports System.Data.SqlClient
    Public Class Form1
        Dim constr As String = "Data Source = (localdb)\MSSQLLocalDB; Integrated Security = True ;AttachDbFileName= C:\Users\alexl2\Desktop\DataBase\Alex\alex.mdf"
        Dim conn As SqlConnection
        Dim sda As SqlDataAdapter
        Dim dt As DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Using conn = New SqlConnection(constr)
                sda = New SqlDataAdapter("Select * From Login", conn)
                dt = New DataTable()
                DataGridView1.DataSource = dt
            End Using
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim dv As DataView = dt.DefaultView
            'dv.RowFilter = "Data > '08/26/2018' and Data < '08/29/2018' "
            dv.RowFilter = "Data > " & "'" & TextBox1.Text & "'" & "and Data <" & "'" & TextBox2.Text & "'"
            Me.DataGridView1.DataSource = dv
        End Sub
    End Class

    Best Regards,


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Wednesday, September 19, 2018 6:13 AM