Amrit Sharma
Amrit Sharma

Reputation: 1916

Creating Search Function in asp.net

I am trying to make search function in my web application.

Basically, i have one text box and one button. Also i have listbox to render the search results.

I am using follwing query on access which is returning nothing.

SELECT [identifier] FROM [Category3]
WHERE [identifier] LIKE '%' + @name + '%';

The value for @name comes from the text box.

I am looking to search only one column of the table, so i will like to render the return results to the list box.

Do i need to use DataSet to store and render the results.

Any advice will be appreciated.

CODE:

 protected void Button1_Click(object sender, EventArgs e)
        {
            searchDB(TextBox1.Text);

        }



public DataSet searchDB(string identifier)
        {
            DataSet dataSet = new DataSet();
            // Create connection object
            OleDbConnection oleConn = new OleDbConnection(connString);
            try
            {
                oleConn.Open();
                string sql = "SELECT [identifier] WHERE [identifier] LIKE '% + @name + %'";

                OleDbCommand oleComm = new OleDbCommand(sql, oleConn);
                oleComm.Parameters.Add("@name", OleDbType.Char).Value = identifier;

                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql, oleConn);
                dataAdapter.Fill(dataSet, "Category3");
            }
            catch (Exception ex)
            {
                Response.Redirect("Error.aspx");
            }
            finally
            {
                oleConn.Close();
            }
            if (dataSet.Tables.Count <= 0)
                return null;
            else
                return dataSet;
        }

enter image description here

Upvotes: 0

Views: 5395

Answers (2)

chetanya gehlot
chetanya gehlot

Reputation: 564

I Think you should try this

string sql = "SELECT [identifier] WHERE [identifier] LIKE '%' + '@name' + '%' ";

Upvotes: 0

Dev N00B
Dev N00B

Reputation: 218

I think your problem is here

WHERE [identifier] LIKE '%' + @name + '%';

it should be

WHERE [identifier] LIKE '% + @name + %';

shouldnt have single quotes around the % signs

Upvotes: 3

Related Questions