Sami-L
Sami-L

Reputation: 5705

How to filter a linq to sql query by using textbox input

Here is my code:

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        //**************************************
        int aa = textBox1.Text.Length;

        var qry = (from p in dc.Products
                   where p.ProductName.Substring(aa) == textBox1.Text.Trim()
                   select p).ToList();
        productDataGridView.DataSource = qry;
    }

When I enter a letter in the textbox the datagrid become empty

Upvotes: 0

Views: 3563

Answers (2)

Anthony
Anthony

Reputation: 427

never and I say never use == to compare 2 strings always use equals() or contains() if you are comparing 2 strings most likely u will get a false eventhough the 2 strings look exactly the same. The difference is that each of the string point to a different object. Basically everytime u make a string it creates a new object of the class String.

Upvotes: 0

Raphaël Althaus
Raphaël Althaus

Reputation: 60493

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        var searchValue = textBox1.Text.Trim();//you can add a ToUpper();
        var qry = (from p in dc.Products
                   where p.ProductName.StartsWith(searchValue);//you can add a ToUpper() to p.ProductName
                   select p).ToList();
        productDataGridView.DataSource = qry;
    }

Upvotes: 2

Related Questions