Hi akhter,
Refer below sample.
HTML
StartDate :
<asp:TextBox runat="server" ID="txtStartDate" />
<br />
EndDate :
<asp:TextBox runat="server" ID="txtEndDate" />
<asp:Button Text="Filter" runat="server" OnClick="FilterData" />
<br />
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="EnrollNumber" HeaderText="EnrollNumber" />
<asp:BoundField DataField="EmpName" HeaderText="EmpName" />
<asp:BoundField DataField="Date" HeaderText="Date" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string query = "SELECT em.EnrollNumber,em.EmpName,ISNULL(Convert(VARCHAR,alg.A_Date,120),'') Date FROM EmpMaster em LEFT JOIN AttendenceLOG alg ON alg.EnrollNumber = em.EnrollNumber";
DataTable dt = GetData(query, "", "");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void FilterData(object sender, EventArgs e)
{
string query = "SELECT em.EnrollNumber,em.EmpName,ISNULL(Convert(VARCHAR,alg.A_Date,120),'') Date FROM EmpMaster em LEFT JOIN AttendenceLOG alg ON alg.EnrollNumber = em.EnrollNumber WHERE SUBSTRING(CONVERT(VARCHAR, A_Date, 120), 0, 11) BETWEEN @StartDate AND @EndDate OR alg.A_Date IS NULL";
DataTable dt = GetData(query, txtStartDate.Text, txtEndDate.Text);
GridView1.DataSource = dt;
GridView1.DataBind();
}
private DataTable GetData(string query, string startDate, string endDate)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
{
cmd.Parameters.AddWithValue("@StartDate", DateTime.Parse(startDate).ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture));
cmd.Parameters.AddWithValue("@EndDate", DateTime.Parse(endDate).ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture));
}
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim query As String = "SELECT em.EnrollNumber,em.EmpName,ISNULL(Convert(VARCHAR,alg.A_Date,120),'') Date FROM EmpMaster em LEFT JOIN AttendenceLOG alg ON alg.EnrollNumber = em.EnrollNumber"
Dim dt As DataTable = GetData(query, "", "")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub FilterData(ByVal sender As Object, ByVal e As EventArgs)
Dim query As String = "SELECT em.EnrollNumber,em.EmpName,ISNULL(Convert(VARCHAR,alg.A_Date,120),'') Date FROM EmpMaster em LEFT JOIN AttendenceLOG alg ON alg.EnrollNumber = em.EnrollNumber WHERE SUBSTRING(CONVERT(VARCHAR, A_Date, 120), 0, 11) BETWEEN @StartDate AND @EndDate OR alg.A_Date IS NULL"
Dim dt As DataTable = GetData(query, txtStartDate.Text, txtEndDate.Text)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Private Function GetData(ByVal query As String, ByVal startDate As String, ByVal endDate As String) As DataTable
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query, con)
If Not String.IsNullOrEmpty(startDate) AndAlso Not String.IsNullOrEmpty(endDate) Then
cmd.Parameters.AddWithValue("@StartDate", DateTime.Parse(startDate).ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture))
cmd.Parameters.AddWithValue("@EndDate", DateTime.Parse(endDate).ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture))
End If
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Screenshot