Combo in Grid - WHERE LIKE

4 posts, 0 answers
  1. Christophe
    Christophe avatar
    12 posts
    Member since:
    May 2015

    Posted 13 May 2015 Link to this post

    Hello,

    I am following this example of Combobox in Radgrid.

    I am having a problem with this part of the code behind: WHERE CompanyName LIKE @CompanyName + '%'"

    protected void RadComboBox1_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
            {
                string sql = "SELECT [SupplierID], [CompanyName], [ContactName], [City] FROM [Suppliers]  WHERE CompanyName LIKE @CompanyName + '%'";
                SqlDataAdapter adapter = new SqlDataAdapter(sql,
                    ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
                adapter.SelectCommand.Parameters.AddWithValue("@CompanyName", e.Text);

    I use Mysql and not Sql Server, and it seems that it does not accept + '%'"

    How can I replace that to state "contains"?

    Thank you

  2. Christophe
    Christophe avatar
    12 posts
    Member since:
    May 2015

    Posted 13 May 2015 Link to this post

    I think this post can be a track, but I don't manage to adapt it to my C#...
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Christophe
    Christophe avatar
    12 posts
    Member since:
    May 2015

    Posted 14 May 2015 Link to this post

    My code is for the moment the following:

    protected void rcbAccountCode_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
    {
        string Mysql = "SELECT * FROM rfaccount where AccountCode LIKE '%" + Str_AccountCode + "%'";
        MySqlDataAdapter adapter = new MySqlDataAdapter(Mysql,
            ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
        adapter.SelectCommand.Parameters.AddWithValue("@AccountCode", e.Text);
     
        DataTable dt = new DataTable();
        adapter.Fill(dt);
     
        RadComboBox comboBox = (RadComboBox)sender;
        // Clear the default Item that has been re-created from ViewState at this point.
        comboBox.Items.Clear();
     
        foreach (DataRow row in dt.Rows)
        {
            RadComboBoxItem item = new RadComboBoxItem();
            item.Text = row["AccountCode"].ToString();
            item.Value = row["AccountId"].ToString();
            item.Attributes.Add("AccountName", row["AccountName"].ToString());
     
            comboBox.Items.Add(item);
     
            item.DataBind();
        }
    }

     I now need to define Str_AccountCode. I wanted to add that:

    string Str_AccountCode = ((RadComboBox)item.FindControl("RadComboBox1")).Text;

    But I don't know where to put it....


     

    dcefe

  5. Christophe
    Christophe avatar
    12 posts
    Member since:
    May 2015

    Posted 16 May 2015 in reply to Christophe Link to this post

    Settled. The following works for me: LIKE CONCAT('%', @AccountCode, '%')

    protected void rcbAccountCode_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
    {
        string Mysql = "SELECT * FROM rfaccount WHERE AccountCode LIKE CONCAT('%', @AccountCode, '%')";
        MySqlDataAdapter adapter = new MySqlDataAdapter(Mysql,
            ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
        adapter.SelectCommand.Parameters.AddWithValue("@AccountCode", e.Text);
      
        DataTable dt = new DataTable();
        adapter.Fill(dt);
      
        RadComboBox comboBox = (RadComboBox)sender;
        // Clear the default Item that has been re-created from ViewState at this point.
        comboBox.Items.Clear();
      
        foreach (DataRow row in dt.Rows)
        {
            RadComboBoxItem item = new RadComboBoxItem();
            item.Text = row["AccountCode"].ToString();
            item.Value = row["AccountId"].ToString();
            item.Attributes.Add("AccountName", row["AccountName"].ToString());
      
            comboBox.Items.Add(item);
      
            item.DataBind();
        }
    }

Back to Top