i am trying to incorporate a basic search function on my webpage and display the results using a gridview. the thing, the codework on page_load but doesn't when I use a textbox + searchbutton.
heres the code:
Java |
like what i've said, it works if i put it on page_load (using select * from message) but doesn't on button1_click (using the sql statement above) where the condition parameter is based on the textbox input.
any help?

Manikavelu VelayuthamRank #413 · 6K reputationPosted Aug 30, 2010, 9:21 PM EDT
Mark Uy130 reputationPosted Aug 30, 2010, 9:34 PM EDT
Mark Uy130 reputationPosted Aug 30, 2010, 9:51 PM EDT
i'm mystified as to how easy this is using an object data source but by using a codebehind i am lost.
anybody who can guide me here?
Manikavelu VelayuthamRank #413 · 6K reputationPosted Aug 30, 2010, 10:00 PM EDT
or else
just try without filtering once, if it comes, then surely the problem is the text that you passed from the text box.....
try it in various way and tell me if you still not found the answer
Mark Uy130 reputationPosted Aug 30, 2010, 10:51 PM EDT
the grid becomes populated without the "like" condition/filtering if i put it under page_load;
___________________________________________________________________________
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection myConn;
myConn = new SqlConnection(@"Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|\projectDB.mdf;
Integrated Security=True;User Instance=True");
myConn.Open();
string strSQL;
strSQL = "SELECT * FROM Message";
//strSQL = "SELECT * FROM Message WHERE Message LIKE '" + txtSearch.Text + "'";
SqlDataAdapter da = new SqlDataAdapter(strSQL, myConn);
DataSet ds = new DataSet();
da.Fill(ds, "Message");
GridView1.DataSource = ds;
GridView1.DataBind();
myConn.Close();
}
_______________________________________________________________________________________
the very same code doesnt work when put inside a button_click event. with or without the conditions/filtering. just blank.
Pasan Ratnayake100 reputationPosted Aug 31, 2010, 12:04 AM EDT
The error you have made is in the SQL that you are trying to build. The difference between using the 'LIKE ' clause and '=' is that when using 'LIKE ' clause you can specify wildcards. Without a wildcard there is no difference between the two. What happens in your code is that since you have not specified any wildcards the SQL statement tries to match exact the phrase that you type instead of trying to look for the phrase inside the text you are trying to search.
Try including this line of code in your button_click.
Hope this helps !