LIKE clause issue in c# but works in SQL Server Express

1tuwyuhd  于 2023-11-16  发布在  C#
关注(0)|答案(5)|浏览(193)

I was having this issue of my comboBox value not returning the results I wanted based on a project I was working on.

So the issue is this: I want to search by way of a substring in the comboBox. To clarify,I want a string from the comboBox to return the necessary value based on any part of the string I entered. Currently all it does is populate the comboBox with the items. What I want is after populating the comboBox it should return a string based on any character I type in. So let's say I have the word "stack123" when I type "123" or "k" or any substring, it will narrow the comboBox items and show the values based on the substring entered or just return the word "stack123"

string query = "SELECT * FROM dbo.Carimed WHERE Item_Description LIKE '%" + comboBox1.Text.Trim().Replace("'", "''") + "%'; ";

And I don't know if this helps but this is the full thing:

using System;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace comboBoxTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            fillCari();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        void fillCari()//fill Cari-med dropdown with values
        {
            try
            {
                string connectionString = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Carimed_Inventory;Integrated Security=True";
                SqlConnection con2 = new SqlConnection(connectionString);
                con2.Open();
                string query = "SELECT * FROM dbo.Carimed WHERE Item_Description LIKE '%" + comboBox1.Text.Trim().Replace("'", "''") + "%'; "; 
                SqlCommand cmd2 = new SqlCommand(query, con2);

                SqlDataReader dr2 = cmd2.ExecuteReader();

                while (dr2.Read())
                {
                    string cari_des = dr2.GetString(dr2.GetOrdinal("Item_Description"));
                    comboBox1.Items.Add(cari_des);
                }

                con2.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}

What could be the problem?

jei2mxaa

jei2mxaa1#

Maybe instead of

'%" + comboBox1.Text.Trim().Replace("'", "''") + "%'

You meant to use the value of a textbox? Like:

'%" + textBox1.Text.Trim().Replace("'", "''") + "%'
vjhs03f7

vjhs03f72#

The workaround was using the guide of this user from here . What was done by the author was that of overriding the default combobox setting in winforms. I just found a way to tie it into my code and got it up and running. I will outline how it works

suggestComboBox.DataSource = new List<person>();
suggestComboBox.DisplayMember = "Name";

// then you have to set the PropertySelector like this:
suggestComboBox.PropertySelector = collection => collection.Cast<person>      
().Select(p => p.Name);

// the class Person looks something like this:
class Person
{
  public string Name { get; set; }
  public DateTime DateOfBirth { get; set; }
  public int Height { get; set; }
}</person>

And this is the Custom comboBox implementation:

public class SuggestComboBox : ComboBox
{
  #region fields and properties

private readonly ListBox _suggLb = new ListBox { Visible = false, TabStop = false };
private readonly BindingList<string> _suggBindingList = new BindingList<string>();
private Expression<Func<ObjectCollection, IEnumerable<string>>> _propertySelector;
private Func<ObjectCollection, IEnumerable<string>> _propertySelectorCompiled;
private Expression<Func<string, string, bool>> _filterRule;
private Func<string, bool> _filterRuleCompiled;
private Expression<Func<string, string>> _suggestListOrderRule;
private Func<string, string> _suggestListOrderRuleCompiled;

public int SuggestBoxHeight
{
    get { return _suggLb.Height; }
    set { if (value > 0) _suggLb.Height = value; }
}
/// <summary>
/// If the item-type of the ComboBox is not string,
/// you can set here which property should be used
/// </summary>
public Expression<Func<ObjectCollection, IEnumerable<string>>> PropertySelector
{
    get { return _propertySelector; }
set
{
    if (value == null) return;
    _propertySelector = value;
    _propertySelectorCompiled = value.Compile();
}
}

///<summary>
/// Lambda-Expression to determine the suggested items
/// (as Expression here because simple lamda (func) is not serializable)
/// <para>default: case-insensitive contains search</para>
/// <para>1st string: list item</para>
/// <para>2nd string: typed text</para>
///</summary>
public Expression<Func<string, string, bool>> FilterRule
{
    get { return _filterRule; }
    set
    {
        if (value == null) return;
        _filterRule = value;
        _filterRuleCompiled = item => value.Compile()(item, Text);
    }
}

///<summary>
/// Lambda-Expression to order the suggested items
/// (as Expression here because simple lamda (func) is not serializable)
/// <para>default: alphabetic ordering</para>
///</summary>
public Expression<Func<string, string>> SuggestListOrderRule
{
    get { return _suggestListOrderRule; }
    set
    {
        if (value == null) return;
        _suggestListOrderRule = value;
        _suggestListOrderRuleCompiled = value.Compile();
    }
}

#endregion

/// <summary>
/// ctor
/// </summary>
public SuggestComboBox()
{
    // set the standard rules:
    _filterRuleCompiled = s => s.ToLower().Contains(Text.Trim().ToLower());
    _suggestListOrderRuleCompiled = s => s;
    _propertySelectorCompiled = collection => collection.Cast<string>();

    _suggLb.DataSource = _suggBindingList;
    _suggLb.Click += SuggLbOnClick;

    ParentChanged += OnParentChanged;
}

/// <summary>
/// the magic happens here ;-)
/// </summary>
/// <param name="e"></param>
protected override void OnTextChanged(EventArgs e)
{
    base.OnTextChanged(e);

    if (!Focused) return;

    _suggBindingList.Clear();
    _suggBindingList.RaiseListChangedEvents = false;
    _propertySelectorCompiled(Items)
         .Where(_filterRuleCompiled)
         .OrderBy(_suggestListOrderRuleCompiled)
         .ToList()
         .ForEach(_suggBindingList.Add);
    _suggBindingList.RaiseListChangedEvents = true;
    _suggBindingList.ResetBindings();
    
    _suggLb.Visible = _suggBindingList.Any(); 
    
    if (_suggBindingList.Count == 1 &&  
                _suggBindingList.Single().Length == Text.Trim().Length)
    {
        Text = _suggBindingList.Single();
        Select(0, Text.Length);
        _suggLb.Visible = false;
    }
}

/// <summary>
/// suggest-ListBox is added to parent control
/// (in ctor parent isn't already assigned)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnParentChanged(object sender, EventArgs e)
{
    Parent.Controls.Add(_suggLb);
    Parent.Controls.SetChildIndex(_suggLb, 0);
    _suggLb.Top = Top + Height - 3;
    _suggLb.Left = Left + 3;
    _suggLb.Width = Width - 20;
    _suggLb.Font = new Font("Segoe UI", 9);
}

protected override void OnLostFocus(EventArgs e)
{
    // _suggLb can only getting focused by clicking (because TabStop is off)
    // --> click-eventhandler 'SuggLbOnClick' is called
    if (!_suggLb.Focused)
        HideSuggBox();
    base.OnLostFocus(e);
}
protected override void OnLocationChanged(EventArgs e)
{
base.OnLocationChanged(e);
_suggLb.Top = Top + Height - 3;
_suggLb.Left = Left + 3;
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
_suggLb.Width = Width - 20;
}

private void SuggLbOnClick(object sender, EventArgs eventArgs)
{
    Text = _suggLb.Text;
    Focus();
}

private void HideSuggBox()
{
    _suggLb.Visible = false;
}

protected override void OnDropDown(EventArgs e)
{
    HideSuggBox();
    base.OnDropDown(e);
}

#region keystroke events

/// <summary>
/// if the suggest-ListBox is visible some keystrokes
/// should behave in a custom way
/// </summary>
/// <param name="e"></param>
protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
{
    if (!_suggLb.Visible)
    {
        base.OnPreviewKeyDown(e);
        return;
    }

    switch (e.KeyCode)
    {
        case Keys.Down:
            if (_suggLb.SelectedIndex < _suggBindingList.Count - 1)
                _suggLb.SelectedIndex++;
            return;
        case Keys.Up:
            if (_suggLb.SelectedIndex > 0)
                _suggLb.SelectedIndex--;
            return;
        case Keys.Enter:
            Text = _suggLb.Text;
        Select(0, Text.Length);
        _suggLb.Visible = false;
            return;
        case Keys.Escape:
            HideSuggBox();
            return;
    }

    base.OnPreviewKeyDown(e);
}

private static readonly Keys[] KeysToHandle  = new[] 
            { Keys.Down, Keys.Up, Keys.Enter, Keys.Escape };
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    // the keysstrokes of our interest should not be processed be base class:
    if (_suggLb.Visible && KeysToHandle.Contains(keyData))
        return true;
    return base.ProcessCmdKey(ref msg, keyData);
}

#endregion
}
6ovsh4lw

6ovsh4lw3#

This sample works to filter the combo items based on the hint text but has an issue when the combobox overwrites the hint with selected text. It re-populates the combobox each time you type another letter.

public Form1()
{
    InitializeComponent();
    PopulateCombo(String.Empty);
}

private void comboBox1_KeyUp(object sender, KeyEventArgs e)
{
    var hint = comboBox1.Text;
    PopulateCombo(hint);
}

private void PopulateCombo(string hint)
{
    comboBox1.Items.Clear();
    var connString = @"Server=.\sqlexpress;Database=NORTHWND;Trusted_Connection=True;";
    using(var con2 = new SqlConnection(connString))
    {
        var query = "select CategoryName from Categories where CategoryName like '%' + @HINT + '%'";
        using (SqlCommand cmd2 = new SqlCommand(query, con2))
        {
            cmd2.Parameters.Add("@HINT", SqlDbType.VarChar);
            cmd2.Parameters["@HINT"].Value = hint.Trim();
            con2.Open();
            var dr2 = cmd2.ExecuteReader();
            while (dr2.Read())
            {
                 comboBox1.Items.Add(dr2.GetString(0));
            }
            //reset cursor to end of hint text
            comboBox1.SelectionStart = comboBox1.Text.Length;
            comboBox1.DroppedDown = true;
        }
    }
}
mv1qrgav

mv1qrgav4#

If you are using framework 4.0 or upper, then you can try only adding the following line after populate the datasource, that is to say, after filcari() in the constructor.

comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;

and remove the where clause in your query.

cgh8pdjw

cgh8pdjw5#

  1. Create a type for your data
  2. Create a list of that type
  3. Populate the list with data from your database
  4. Sort list using linq extension methods
  5. Bind sorted list to combobox
using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Configuration;
    namespace Combo
    {
      
      class DataItem
      {
        public int Id { get; set; }
        public string Name { get; set; }
      }
      public partial class MainForm : Form
      {
        List<DataItem>dataitems=new List<DataItem>();
        public MainForm()
        {
          
          InitializeComponent();
          textBox1.TextChanged+=textbox1textChanged;
          getDataItems();
          comboBox1.DisplayMember="Name";
          comboBox1.DataSource=dataitems;
            
        }
    
        void textbox1textChanged(object sender, EventArgs e)
        {
          if (!string.IsNullOrWhiteSpace(textBox1.Text))
                {
            comboBox1.DataSource=dataitems.FindAll(d => d.Name.StartsWith(textBox1.Text));
                }
        }
        void getDataItems()
            {
                try
                {
                    using (
                        SqlConnection connection =
                            new SqlConnection(ConfigurationManager.ConnectionStrings["DataItem"].ConnectionString))
                    {
                        if (connection.State == ConnectionState.Closed)
                        {
                            connection.Open();
                            string query = @"SELECT p.ProductId,p.ProductName FROM Product p";
                            var command = new SqlCommand(query, connection) { CommandType = CommandType.Text };
                            var reader = command.ExecuteReader();
                            while (reader.Read())
                            {
                              var data=new DataItem();
                              data.Id=reader.GetInt32(0);
                              data.Name=reader.GetString(1);
                              dataitems.Add(data);
                            }
                            connection.Close();
                        }
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message.ToString(), "Error");
                }
    
    
            }
      }
      
    }

相关问题