Member 12802669 Ответов: 1

Функция поиска работает только с одной переменной


I have this Code and it works perfectly, but as soon as I want to write more LIKE Operators my whole gridview changes.

Example: I want:

AND (Number LIKE '%' + @Number + '%') OR (Place LIKE '%' + @Place + '%')

cmd.Parameters.AddWithValue("@Number", txtSearch.Text.Trim()); cmd.Parameters.AddWithValue("@Place ", txtSearch.Text.Trim());

my Gridview ignores the session and on Login you can see every users data.
Login is the username.

but If I keep it like this it works perfectly: 


Что я уже пробовал:

protected void btnFilter_Click(object sender, EventArgs e)
    {

        string name = Session["user"].ToString();


        string constr = ConfigurationManager.ConnectionStrings["strConn"].ConnectionString;

        using (SqlConnection conn = new SqlConnection(constr))
        {

            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = conn;
                cmd.CommandText = @"SELECT * From Product
                                           Inner Join User
                                           ON product.user = user.id
                                           Where user.login = @User
                                           AND Status = @Status
                                           AND (Number LIKE '%' + @Number + '%')";

                cmd.Parameters.AddWithValue("@User", name);
                cmd.Parameters.AddWithValue("@Status", DropDownList1.SelectedValue);
                cmd.Parameters.AddWithValue("@Number", txtSearch.Text.Trim());
             
                using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    ad.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
    }


<asp:GridView ID="GridView1"  DataKeyNames="Number" AllowSorting="true" OnRowDataBound="RowDataBound"  runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" Visible="false" />
            <asp:BoundField DataField="Number" HeaderText="Nummer" SortExpression="Number" />
            <asp:BoundField DataField="Login" HeaderText="Datum" SortExpression="Login" />
 	<asp:BoundField DataField="Date" HeaderText="Datum" SortExpression="Date" />
 

        </Columns>
    </asp:GridView>

1 Ответов

Рейтинг:
1

OriginalGriff

Попробуйте скобки:

AND ((Number LIKE '%' + @Number + '%') OR (Place LIKE '%' + @Place + '%'))