How to add filter using asp.net c# in GridView
Display data in GridView and user search data from emp id.
I want to search data by emp_id
namespace vstr_gt_ps_systm
{
public partial class cisf : System.Web.UI.Page
{
public SqlConnection con;
public string constr;
public void connection()
{
constr = ConfigurationManager.ConnectionStrings["constr"].ToString();
con = new SqlConnection(constr);
con.Open();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
rep_bind();
}
if (Session["emp_nm"] == null)
Response.Redirect("emp.aspx");
else
{
String EmplooyeId = Session["emp_id"].ToString();
Label1.Text = " " + EmplooyeId + "";
String emp_nm = Session["emp_nm"].ToString();
Label2.Text = "" + emp_nm + " ";
}
Label1.Visible = false;
}
void rep_bind()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
string query = "select * from vstr where emp_out_flag='N'and cisf_out_flag='N' and emp_id like '" + TextBox1.Text + "%'";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
connection();
string query = "select * from vstr where emp_out_flag='N'and cisf_out_flag='N' and emp_id like '" + TextBox1.Text + "%'";
SqlCommand com = new SqlCommand(query, con);
SqlDataReader dr;
dr = com.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
rep_bind();
GridView1.Visible = true;
TextBox1.Text = "";
Label1.Text = "";
}
else
{
GridView1.Visible = false;
Label3.Visible = true;
Label3.Text = "The search Term " + TextBox1.Text + " Is Not Available in the Records"; ;
}
}
protected void OnSendSession(object sender, EventArgs e)
{
Response.Redirect("~/Default.aspx");
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditButton")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
Response.Redirect("Default.aspx?Id=" + row.Cells[0].Text);
}
}
private static byte[] ConvertHexToBytes(string hex)
{
byte[] bytes = new byte[hex.Length / 2];
for (int i = 0; i < hex.Length; i += 2)
{
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
}
return bytes;
}
[WebMethod(EnableSession = true)]
public static string GetCapturedImage()
{
string url = HttpContext.Current.Session["CapturedImage"].ToString();
HttpContext.Current.Session["CapturedImage"] = null;
return url;
}
protected void Button4_Click(object sender, EventArgs e)
{
Response.Redirect("lgn.aspx");
}
protected void Closed(object sender, EventArgs e)
{
GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
int Id = Convert.ToInt32(GridView1.DataKeys[Convert.ToInt32((sender as Button).CommandArgument)].Values[0]);
string cisf_out_flag = "YES";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UPDATE vstr SET cisf_out_flag = @cisf_out_flag WHERE Id = @Id"))
{
cmd.Parameters.AddWithValue("@Id", Id);
cmd.Parameters.AddWithValue("@cisf_out_flag", cisf_out_flag);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
row.Visible = false;
}
}
}
}
}
Id emp_id emp_nm vst_nm vst_adhr fa_hu_nm gndr crnt_add pur_pos emp_out_flag cisf_out_flag mrk_in_emp mrk_in_cisf mrk_out_emp mark_out_cisf img
20 007221 D.P. Chakarborty Neeraj Kumar 123456789101112 M/S Neeraj Kumar Male Teother Riwa MP Check Cable YES YES 4/30/2022 13:16 4/30/2022 13:16 NULL NULL NULL
21 008845 V.K. Pandey TEST 123456789123456 TEST Male TEST TEST YES YES 4/30/2022 17:13 4/30/2022 17:13 NULL NULL NULL