Click here to Skip to main content
16,018,318 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
protected void btnSearch_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        con.Open();
        try
        {
            string query = "select * from news where name like '" + txtSearch.Text + "%' or description like '" + txtSearch.Text + "%'  ";
            SqlCommand cmd = new SqlCommand(query, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            gridrecord.DataSource = ds;
            gridrecord.DataBind();
            if (gridrecord.Rows.Count == 0)
            {
                lblmsg.Visible = true;
                lblmsg.Text = " No data found";

            }
        }
        catch (Exception ex)
        { 
        
        }
    }
Posted
Updated 20-Feb-14 17:50pm
v2
Comments
Sergey Alexandrovich Kryukov 20-Feb-14 23:39pm    
Why should we even look at this code dump if you did not even bother to explain what problem do you have with this code?
—SA
King Fisher 20-Feb-14 23:51pm    
getting any error?
Bojjaiah 20-Feb-14 23:54pm    
what's your problem?
or
what's error?
thatraja 21-Feb-14 2:10am    
I know the what's wrongs(yes, many) but you tell first what's the error?

C#
}
catch (Exception ex)
{

}

That's wrong: you swallow any exception, hence you have no chance to get rid of the error.
Also, you do not close the connection in case of an exception. I suggest a using directive:
C#
using(SqlConnection con = new SqlConnection())
{
    con.ConnectionString = ... etc.
}

And
C#
if (gridrecord.Rows.Count == 0)
           {
               lblmsg.Visible = true;
               lblmsg.Text = " No data found";

           }


will never reset that label when a query succeeds after a previous failure.
 
Share this answer
 
better you use Trim() to Avoid Space while using Like Query
SQL
string query = "select * from news where name like '" + txtSearch.Text.Trim() + "%' or description like '" + txtSearch.Text.Trim() + "%' ";
 
Share this answer
 

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