Hi sambath,
Please take reference to the below code and correct your code.
HTML
<asp:Panel ID="pnlStatus" runat="server">
<asp:CheckBox ID="chkPending" runat="server" Text="Pending" OnCheckedChanged="StatusChanged"
AutoPostBack="true" />
<asp:CheckBox ID="chkComplete" runat="server" Text="Complete" OnCheckedChanged="StatusChanged"
AutoPostBack="true" />
</asp:Panel>
<br />
<br />
<asp:GridView ID="gvStatus" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Status" HeaderText="Status" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid("SELECT * FROM UserStatus");
}
}
private void BindGrid(string query)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
gvStatus.DataSource = dt;
gvStatus.DataBind();
}
}
}
}
protected void StatusChanged(object sender, EventArgs e)
{
string query = "SELECT * FROM UserStatus ";
CheckBox chkPending = (CheckBox)pnlStatus.FindControl("chkPending");
CheckBox chkComplete = (CheckBox)pnlStatus.FindControl("chkComplete");
if (chkPending.Checked || chkComplete.Checked)
{
query += "WHERE Status IN (";
if (chkPending.Checked)
{
query += "'no',''";
}
if (chkComplete.Checked)
{
query += "'','yes'";
}
query += ")";
}
BindGrid(query);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid("SELECT * FROM UserStatus")
End If
End Sub
Private Sub BindGrid(ByVal query As String)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query, con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
gvStatus.DataSource = dt
gvStatus.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub StatusChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim query As String = "SELECT * FROM UserStatus "
Dim chkPending As CheckBox = CType(pnlStatus.FindControl("chkPending"), CheckBox)
Dim chkComplete As CheckBox = CType(pnlStatus.FindControl("chkComplete"), CheckBox)
If chkPending.Checked OrElse chkComplete.Checked Then
query += "WHERE Status IN ("
If chkPending.Checked Then
query += "'no',''"
End If
If chkComplete.Checked Then
query += "'','yes'"
End If
query += ")"
End If
BindGrid(query)
End Sub
Screenshot