Hi ashishk,
Please refer below sample.
SQL
CREATE TABLE Test ([Date] NVARCHAR(50))
INSERT INTO Test VALUES('07/01/2018')
INSERT INTO Test VALUES('07/02/2018')
INSERT INTO Test VALUES('07/03/2018')
INSERT INTO Test VALUES('07/04/2018')
INSERT INTO Test VALUES('07/05/2018')
INSERT INTO Test VALUES('07/10/2018')
INSERT INTO Test VALUES('07/20/2018')
INSERT INTO Test VALUES('07/30/2018')
INSERT INTO Test VALUES('07/31/2018')
INSERT INTO Test VALUES('08/01/2018')
INSERT INTO Test VALUES('08/02/2018')
INSERT INTO Test VALUES('08/03/2018')
INSERT INTO Test VALUES('08/04/2018')
Namespaces
C#
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
VB.Net
Imports System.Data.SqlClient
Code
C#
private void btnSearch_Click(object sender, EventArgs e)
{
string fromDate = dateTimePicker1.Value.ToString("MM/dd/yyyy");
string toDate = dateTimePicker2.Value.ToString("MM/dd/yyyy");
DateTime d1 =Convert.ToDateTime(fromDate);
DateTime d2 = Convert.ToDateTime(toDate);
string constr = "Server=.;DataBase=master;UID=sa;PWD=password";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT [Date] FROM Test WHERE Convert(datetime,[Date],110) BETWEEN Convert(datetime,@FromDate,103) AND Convert(datetime,@ToDate,103)", con))
{
cmd.Parameters.AddWithValue("@FromDate", d1);
cmd.Parameters.AddWithValue("@ToDate", d2);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
DataGridView1.DataSource = dt;
}
}
}
}
VB.Net
Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim fromDate As String = dateTimePicker1.Value.ToString("MM/dd/yyyy")
Dim toDate As String = dateTimePicker2.Value.ToString("MM/dd/yyyy")
Dim d1 As DateTime = Convert.ToDateTime(fromDate)
Dim d2 As DateTime = Convert.ToDateTime(toDate)
Dim constr As String = "Server=.;DataBase=master;UID=sa;PWD=password"
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT [Date] FROM Test WHERE Convert(datetime,[Date],110) BETWEEN Convert(datetime,@FromDate,103) AND Convert(datetime,@ToDate,103)", con)
cmd.Parameters.AddWithValue("@FromDate", d1)
cmd.Parameters.AddWithValue("@ToDate", d2)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
DataGridView1.DataSource = dt
End Using
End Using
End Using
End Sub
Screenshot