locked
pass like query to filter in c# RRS feed

  • Question

  • User-807418713 posted

    Hello

    This is my code

    SqlCommand cmd = new SqlCommand("Select * from Master where CN='" + DropDownList1.Text + "'", con);

    How to pass like query '%mydropdownlist1%'

    Thanking You

    Thursday, November 14, 2019 10:20 AM

Answers

  • User288213138 posted

    Hi Gopi.MCA,

    I Want To Get First 5-10 Words Filter From Selected Dropdownlist Not Full Dropdownlist Value

    Do you mean that you want to get the 5th to 10th words?, if so you can try to use substring method.  and you can use string.format to insert them into another string.

    The code:

    string ddl = DropDownList1.Text;
                string str = ddl.Substring(4, 6);
                string query = string.Format("Select * from Master where CN ={0}", str);
                SqlConnection con = new SqlConnection(constr);
                SqlCommand cmd = new SqlCommand(query, con);

    The result:

    If I misunderstand your requirement, please post more details information about your requirement.

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 15, 2019 6:49 AM

All replies

  • User475983607 posted

    Hello

    This is my code

    SqlCommand cmd = new SqlCommand("Select * from Master where CN='" + DropDownList1.Text + "'", con);

    How to pass like query '%mydropdownlist1%'

    Thanking You

    Simply update the TSQL to use LIKE.

    SqlCommand cmd = new SqlCommand("Select * from Master where CN Like ' %" + DropDownList1.Text + "%'", con);

    See the TSQL docs for proper syntax.

    https://docs.microsoft.com/en-us/sql/t-sql/language-elements/like-transact-sql?view=sql-server-ver15

    Thursday, November 14, 2019 11:36 AM
  • User-1780421697 posted
                var selectedText = DropDownList1.SelectedItem.Text;
                var selectedValue = DropDownList1.SelectedItem.Value;
    SqlCommand cmd = new SqlCommand("Select * from Master where CN Like '%" +selectedText+"%'", con);
    
    SqlCommand cmd = new SqlCommand("Select * from Master where CN Like '%" +selectedValue+"%'", con);

    Thursday, November 14, 2019 11:52 AM
  • User-807418713 posted

    Hello 

    Good Day

    I Want To Get First 5-10 Words Filter From Selected Dropdownlist Not Full Dropdownlist Value

    Thanking You 

    Thursday, November 14, 2019 6:18 PM
  • User475983607 posted

    I Want To Get First 5-10 Words Filter From Selected Dropdownlist Not Full Dropdownlist Value

    This is a multi-select or the selected test has multiple words?  Can you clarify the problem you are trying to solve?  How about posting sample code that reproduces this issue?

    Thursday, November 14, 2019 6:37 PM
  • User288213138 posted

    Hi Gopi.MCA,

    I Want To Get First 5-10 Words Filter From Selected Dropdownlist Not Full Dropdownlist Value

    Do you mean that you want to get the 5th to 10th words?, if so you can try to use substring method.  and you can use string.format to insert them into another string.

    The code:

    string ddl = DropDownList1.Text;
                string str = ddl.Substring(4, 6);
                string query = string.Format("Select * from Master where CN ={0}", str);
                SqlConnection con = new SqlConnection(constr);
                SqlCommand cmd = new SqlCommand(query, con);

    The result:

    If I misunderstand your requirement, please post more details information about your requirement.

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 15, 2019 6:49 AM
  • User-1780421697 posted

    Here is how you get words from sentence, 

    https://stackoverflow.com/questions/4970538/how-to-get-all-words-of-a-string-in-c

    If you need letters/characters/character then you can use substring/left function to get the number of letters/characters you want.

    https://www.geeksforgeeks.org/c-sharp-substring-method/

    https://www.arclab.com/en/kb/csharp/string-operations-left-mid-right-comparision-csharp-mfc.html

    Friday, November 15, 2019 8:07 AM