This is a migrated thread and some comments may be shown as answers.

Combo in Grid - WHERE LIKE

3 Answers 52 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Christophe
Top achievements
Rank 1
Christophe asked on 13 May 2015, 07:16 PM

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

3 Answers, 1 is accepted

Sort by
0
Christophe
Top achievements
Rank 1
answered on 13 May 2015, 11:05 PM
I think this post can be a track, but I don't manage to adapt it to my C#...
0
Christophe
Top achievements
Rank 1
answered on 15 May 2015, 01:34 AM

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

0
Christophe
Top achievements
Rank 1
answered on 16 May 2015, 06:53 PM

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();
    }
}

Tags
ComboBox
Asked by
Christophe
Top achievements
Rank 1
Answers by
Christophe
Top achievements
Rank 1
Share this question
or