Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dear Frnds,

Search data using Texbox and button from gridview.
=================================================
Presently am searching only one field from gridview.
I need to search Id , FirstName, LastName,UserName, Department from Gridview.

This is my code.

C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Admin_UsersLoginHistory : System.Web.UI.Page
{
    string conString = ConfigurationManager.ConnectionStrings["KKIAConnectionString"].ToString();

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (Session["AdminName"] == "")
        //{
        //    Response.Redirect("Default.aspx");
        //}

        if (!IsPostBack)
        {
            GridData();
        }


    }
    
    protected void BtnSearch_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(conString);
        // I need to search Id , FirstName, LastName,UserName, Department from Gridview.
        SqlCommand cmd = new SqlCommand("Select * from UserLogs where UserName='" + TxtSearch.Text + "' ", con);

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);

        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    private void GridData()
    {
        SqlConnection con = new SqlConnection(conString);
        
        SqlCommand cmd = new SqlCommand("Select * from UserLogs", con);

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);

        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}


[edit]SHOUTING removed, Code block added - OriginalGriff[/edit]
Posted
Updated 17-Apr-12 22:28pm
v2
Comments
OriginalGriff 18-Apr-12 4:28am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.

Check This Link From Codeproject
This Is similar quastion

how to search the records using textbox[^]

Enjoy Coding
 
Share this answer
 
Comments
kishore sharma 18-Apr-12 5:27am    
Thanks For Response
Hi,
Check this already answer you before let me know if did work with you
Here[^]
Best Regards
M.Mitwalli
 
Share this answer
 
So modify your SELECT statement:
SQL
SELECT * FROM myTable WHERE FirstName='Joe' AND LastName='Smith' AND Department='Chemistry'
 
Share this answer
 
Comments
Syed SufiyanUddin 18-Apr-12 4:39am    
Boss i have lot of records in gridview. why should i give names like Joe , smith chemistry...please explain me
Syed SufiyanUddin 18-Apr-12 4:41am    
SqlCommand cmd = new SqlCommand("Select * from UserLogs where UserName='" + TxtSearch.Text + "' ", con);

how to search 4 other fields in this
Muralikrishna8811 18-Apr-12 4:48am    
Hi
SELECT * FROM myTable WHERE FirstName='"+TxtSearch.Text+"%' or LastName='"+TxtSearch.Text+"%' or Department='"+TxtSearch.Text+"%'

after you entered text in textbox it should be search all fileds

with single value
OriginalGriff 18-Apr-12 4:48am    
It's an example - use it as a pattern to write your own SqlCommand statement.

BTW: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead
Syed SufiyanUddin 18-Apr-12 5:00am    
Fill: SelectCommand.Connection property has not been initialized.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900