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:
|
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?
Pasan RatnayakePosted Aug 31, 2010, 4:04 AM
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 !
Mark UyPosted Aug 31, 2010, 2:51 AM
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.
Manikavelu VelayuthamPosted Aug 31, 2010, 2:00 AM
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 UyPosted Aug 31, 2010, 1:51 AM
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?
Mark UyPosted Aug 31, 2010, 1:34 AM
Manikavelu VelayuthamPosted Aug 31, 2010, 1:21 AM