Hi RohitSingh,
Check this example. Now please take its reference and correct your code as per your Mysql code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="EmployeeId" HeaderText="Id" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="BirthDate" HeaderText="Birth Date" DataFormatString="{0:dd-MMM-yyyy}" />
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Filter" runat="server" OnClick="Filter" />
Namespace
C#
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvEmployees.DataSource = GetData();
gvEmployees.DataBind();
}
}
private DataTable GetData()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT EmployeeId,FirstName,LastName,BirthDate FROM Employees";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
protected void Filter(object sender, EventArgs e)
{
DataTable dt = GetData();
DataView dv = dt.DefaultView;
DateTime fromDate = new DateTime(1948, 1, 1);
DateTime toDate = new DateTime(1960, 12, 31);
dv.RowFilter = "BirthDate > #" + fromDate + "# AND BirthDate < #" + toDate + "#";
gvEmployees.DataSource = dv;
gvEmployees.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
gvEmployees.DataSource = GetData()
gvEmployees.DataBind()
End If
End Sub
Private Function GetData() As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT EmployeeId,FirstName,LastName,BirthDate FROM Employees"
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Protected Sub Filter(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = GetData()
Dim dv As DataView = dt.DefaultView
Dim fromDate As DateTime = New DateTime(1948, 1, 1)
Dim toDate As DateTime = New DateTime(1960, 12, 31)
dv.RowFilter = "BirthDate > #" & fromDate & "# AND BirthDate < #" + toDate & "#"
gvEmployees.DataSource = dv
gvEmployees.DataBind()
End Sub
Screenshot
