Click here to Skip to main content
15,909,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am trying search my grid view using the following code below and I cannot get the gridview to filter results when I type in a value in text box on the client-side.

Any help would be very much appreciated.
Thank you.
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "")
        {

            String constr = ConfigurationManager.ConnectionStrings["cdwConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("select [description], [role]  from [dbo].[login] where role = @role");
            cmd.Connection = con;
            cmd.Parameters.Add("@role", SqlDbType.NVarChar).Value = TextBox1.Text;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            //TextBox1.Text = "";
        }
    }


ASP.NET
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="search" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Download Data" onclick="btnExportToExcel_Click" />

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White" onrowdatabound="gvrecords_RowDataBound">

<Columns>
<asp:BoundField DataField="description" HeaderText="UserName" />
<asp:BoundField DataField="role" HeaderText="FirstName" />

</Columns>

</asp:GridView>
Posted
Comments
Gitanjali Singh 9-Jan-14 7:41am    
if (TextBox1.Text != String.Empty)
Try this

C#
 if (TextBox1.Text == "")


try this..
C#
if (TextBox1.Text != "")
 
Share this answer
 
Comments
miss786 9-Jan-14 7:49am    
Thank you so much for your reply and pointing out the silly mistake. I got the textbox to filter through your suggestion and updating the query string. Many thanks

if (TextBox1.Text != "")
{

String constr = ConfigurationManager.ConnectionStrings["cdwConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd = new SqlCommand("select [description], [role] from [dbo].[login] where role like '%" + TextBox1.Text + "%'");
cmd.Connection = con;
cmd.Parameters.Add("@role", SqlDbType.VarChar).Value = TextBox1.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
//TextBox1.Text = "";
}
Bojjaiah 9-Jan-14 8:00am    
When you not getting result, you should debug it. then you can get an error what's the mistake it. Based on your error you can proceed weather post a question/you can solve it.
Karthik_Mahalingam 9-Jan-14 8:05am    
welcome miss786 :)
Try this
if (TextBox1.Text != String.Empty)


Quote:
In .Net pre 2.0, "" creates an object while String.Empty creates no object. So it is more efficient to use String.Empty.


Source of information

.Length == 0 is the fastest option, but .Empty makes for slightly cleaner code.

So "" is pretty equivalent to .Empty, but still not as fast as .Length == 0.
 
Share this answer
 
v3
Comments
Karthik_Mahalingam 9-Jan-14 9:24am    
5! correct
change your condition:-
SQL
if (TextBox1.Text == "")

To

SQL
if (TextBox1.Text != "")


then try still not solved. then revert back with improved question.
 
Share this answer
 
Comments
Karthik_Mahalingam 9-Jan-14 9:23am    
5! correct

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