user1389384
user1389384

Reputation: 49

Search GridView

I have a guestbook at the moment displaying "comment", "comment_time", "user". I want to add a search box below and then search the gridview by "user" and then bind the data to a new grid view below. I am under strict instructions to have all data interactions passed via a web service.

I have a Web Method at the moment to get all the comments:

[WebMethod]
    public DataSet SearchDatabase(string query)
    {
        DataSet ds = new DataSet();
        string database = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/dvd_forum.accdb;Persist Security Info=True";
        string queryStr = "SELECT * FROM Comments WHERE User LIKE '%query%'";
        OleDbConnection myConn = new OleDbConnection(database);
        OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(queryStr, myConn);
        myConn.Open();
        myDataAdapter.Fill(ds, "Comments");
        myConn.Close();
        return ds;
    }

Here's the front code:

<asp:Label ID="Label3" runat="server" Text="Search"></asp:Label><asp:TextBox ID="TextBoxSearch"
    runat="server"></asp:TextBox><asp:Button ID="ButtonSearch" 
runat="server" Text="Search" onclick="ButtonSearch_Click" />
<asp:GridView ID="GridView2" runat="server">

The code behind:

protected void ButtonSearch_Click(object sender, EventArgs e)
{
    string query = TextBoxSearch.Text;

    localhost.Service1 myws = new localhost.Service1();
    ds = myws.SearchDatabase(query);
    GridView2.DataSource = ds;
    GridView2.DataBind();
}

It just doesn't do anything at all, page refreshes with no new GridView or any other action.

On another side note: I have another input on the same page with a required field validator that doesn't let me search unless I fill the input with some text. How can I resolve this?

Thanks.

Upvotes: 0

Views: 1797

Answers (1)

Damith
Damith

Reputation: 63065

string queryStr = "SELECT * FROM Comments WHERE User LIKE '%"+query+"%'";

Upvotes: 2

Related Questions